Skip to content

Commit

Permalink
fix: effect scope issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jan 20, 2025
1 parent 161cdd8 commit 08b275d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/runtime/createClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ export function createClass<TFactory extends InstanceFactory>(
const ReactiveClass = class extends ReactiveInstance<TFactory> {
constructor(...args: Parameters<TFactory>) {
super()
this[EffectScope.symbol].enter()
const scope = this[EffectScope.symbol]
scope.enter()
try {
const self = unnest(copyDescriptors(this, factory(...args)))
this[EffectScope.symbol].autoSetup()
return self
var self = unnest(copyDescriptors(this, factory(...args)))
} finally {
this[EffectScope.symbol].leave()
scope.leave()
}
scope.autoSetup()
return self
}
}
Object.defineProperty(ReactiveClass, 'name', {
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import type { ReactiveClass } from './instance'
let activeScope: EffectScope | null = null

export class EffectScope {
parentScope: EffectScope | undefined
setupEffects: (() => Cleanup)[] | undefined
updateEffects: ((...args: any[]) => void)[] | undefined
cleanupEffects: Cleanup[] | undefined

enter() {
if (activeScope) {
this.parentScope = activeScope
}
activeScope = this
}
leave() {
activeScope = null
activeScope = this.parentScope ?? null
}

setup() {
Expand Down

0 comments on commit 08b275d

Please sign in to comment.