-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement effect scope as subscriber
- Loading branch information
1 parent
828a674
commit daa8572
Showing
6 changed files
with
95 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
import { Subscriber } from './system'; | ||
|
||
export let currentEffectScope: EffectScope | undefined = undefined; | ||
import { DirtyLevels, Subscriber } from './system'; | ||
|
||
export function effectScope() { | ||
return new EffectScope(); | ||
} | ||
|
||
export class EffectScope { | ||
subs: Subscriber[] = []; | ||
export class EffectScope implements Subscriber { | ||
// Subscriber | ||
deps = undefined; | ||
depsTail = undefined; | ||
versionOrDirtyLevel = DirtyLevels.NotDirty; | ||
|
||
run<T>(fn: () => T) { | ||
const lastEffectScope = currentEffectScope; | ||
try { | ||
currentEffectScope = this; | ||
return fn(); | ||
} finally { | ||
currentEffectScope = lastEffectScope; | ||
} | ||
const prevActiveSub = Subscriber.startScopeTrack(this); | ||
const res = fn(); | ||
Subscriber.endScopeTrack(this, prevActiveSub); | ||
return res; | ||
} | ||
|
||
stop() { | ||
for (const sub of this.subs) { | ||
Subscriber.clearTrack(sub); | ||
} | ||
Subscriber.clearTrack(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters