Skip to content

Commit 2e608ff

Browse files
committed
feat(effect): handle nested effects
1 parent 827737a commit 2e608ff

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/effect.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function effect(fn: () => void) {
88
}
99

1010
export class Effect implements IEffect, Subscriber {
11+
scope = currentEffectScope;
1112
nextNotify = undefined;
1213

1314
// Subscriber
@@ -32,7 +33,12 @@ export class Effect implements IEffect, Subscriber {
3233

3334
run() {
3435
const lastActiveSub = Subscriber.startTrack(this);
35-
this.fn();
36+
if (this.scope !== undefined) {
37+
this.scope.run(this.fn);
38+
}
39+
else {
40+
this.fn();
41+
}
3642
Subscriber.endTrack(this, lastActiveSub);
3743
}
3844
}

lib/effectScope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export class EffectScope {
66
subs: Subscriber[] = [];
77

88
run<T>(fn: () => T) {
9-
const original = currentEffectScope;
9+
const lastEffectScope = currentEffectScope;
1010
try {
1111
currentEffectScope = this;
1212
return fn();
1313
} finally {
14-
currentEffectScope = original;
14+
currentEffectScope = lastEffectScope;
1515
}
1616
}
1717

0 commit comments

Comments
 (0)