Skip to content

Commit 9e77e56

Browse files
committed
refactor(system): inline startTrackEffects, endTrackEffects to effect scope
1 parent 6bf82ad commit 9e77e56

File tree

3 files changed

+25
-58
lines changed

3 files changed

+25
-58
lines changed

src/effect.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { activeEffectScope } from './effectScope.js';
12
import { Dependency, DirtyLevels, IEffect, Link, Subscriber, System } from './system.js';
23

34
export function effect(fn: () => void) {
@@ -28,9 +29,11 @@ export class Effect<T = any> implements IEffect {
2829
Dependency.linkSubscriber(this, System.activeSub!);
2930
return;
3031
}
31-
const activeEffectScopeTrackId = System.activeEffectScopeTrackId;
32-
if (activeEffectScopeTrackId !== 0) {
33-
Dependency.linkSubscriber(this, System.activeEffectScope!);
32+
if (activeEffectScope !== undefined) {
33+
const subsTail = this.subsTail;
34+
if (subsTail === undefined || subsTail.trackId !== activeEffectScope.trackId) {
35+
Dependency.linkSubscriber(this, activeEffectScope);
36+
}
3437
}
3538
}
3639

src/effectScope.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { DirtyLevels, IEffect, Link, Subscriber } from './system.js';
1+
import { DirtyLevels, Link, Subscriber, System } from './system.js';
2+
3+
export let activeEffectScope: EffectScope | undefined = undefined;
24

35
export function effectScope() {
46
return new EffectScope();
57
}
68

7-
export class EffectScope implements IEffect {
8-
nextNotify: IEffect | undefined = undefined;
9-
10-
// Dependency
11-
subs: Link | undefined = undefined;
12-
subsTail: Link | undefined = undefined;
13-
9+
export class EffectScope implements Subscriber {
1410
// Subscriber
1511
deps: Link | undefined = undefined;
1612
depsTail: Link | undefined = undefined;
17-
trackId = 0;
13+
trackId = -(++System.lastTrackId);
1814
dirtyLevel = DirtyLevels.None;
1915
canPropagate = false;
2016

@@ -26,11 +22,18 @@ export class EffectScope implements IEffect {
2622
}
2723

2824
run<T>(fn: () => T) {
29-
const prevSub = Subscriber.startTrackEffects(this);
25+
const prevSub = activeEffectScope;
26+
activeEffectScope = this;
27+
this.trackId = Math.abs(this.trackId);
3028
try {
3129
return fn();
3230
} finally {
33-
Subscriber.endTrackEffects(this, prevSub);
31+
if (prevSub !== undefined) {
32+
activeEffectScope = prevSub;
33+
} else {
34+
activeEffectScope = undefined;
35+
}
36+
this.trackId = -Math.abs(this.trackId);
3437
}
3538
}
3639

src/system.ts

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface Subscriber {
2222

2323
export interface Link {
2424
dep: Dependency | IComputed | IEffect;
25-
sub: IComputed | IEffect;
25+
sub: Subscriber | IComputed | IEffect;
2626
trackId: number;
2727
// Also used as prev update
2828
prevSub: Link | undefined;
@@ -41,9 +41,7 @@ export const enum DirtyLevels {
4141

4242
export namespace System {
4343
export let activeSub: IComputed | IEffect | undefined = undefined;
44-
export let activeEffectScope: IEffect | undefined = undefined;
4544
export let activeTrackId = 0;
46-
export let activeEffectScopeTrackId = 0;
4745
export let batchDepth = 0;
4846
export let lastTrackId = 0;
4947
export let queuedEffects: IEffect | undefined = undefined;
@@ -395,15 +393,15 @@ export namespace Subscriber {
395393
}
396394

397395
export function startTrackDependencies(sub: IComputed | IEffect) {
398-
const newVersion = system.lastTrackId + 1;
396+
const newTrackId = system.lastTrackId + 1;
399397
const prevSub = system.activeSub;
400398

401399
system.activeSub = sub;
402-
system.activeTrackId = newVersion;
403-
system.lastTrackId = newVersion;
400+
system.activeTrackId = newTrackId;
401+
system.lastTrackId = newTrackId;
404402

405403
sub.depsTail = undefined;
406-
sub.trackId = newVersion;
404+
sub.trackId = newTrackId;
407405
sub.dirtyLevel = DirtyLevels.None;
408406

409407
return prevSub;
@@ -431,43 +429,6 @@ export namespace Subscriber {
431429
sub.trackId = -sub.trackId;
432430
}
433431

434-
export function startTrackEffects(sub: IEffect) {
435-
const newVersion = system.lastTrackId + 1;
436-
const prevSub = system.activeEffectScope;
437-
438-
system.activeEffectScope = sub;
439-
system.activeEffectScopeTrackId = newVersion;
440-
system.lastTrackId = newVersion;
441-
442-
sub.depsTail = undefined;
443-
sub.trackId = newVersion;
444-
sub.dirtyLevel = DirtyLevels.None;
445-
446-
return prevSub;
447-
}
448-
449-
export function endTrackEffects(sub: IEffect, prevSub: IEffect | undefined) {
450-
if (prevSub !== undefined) {
451-
system.activeEffectScope = prevSub;
452-
system.activeEffectScopeTrackId = prevSub.trackId;
453-
} else {
454-
system.activeEffectScope = undefined;
455-
system.activeEffectScopeTrackId = 0;
456-
}
457-
458-
const depsTail = sub.depsTail;
459-
if (depsTail !== undefined) {
460-
if (depsTail.nextDep !== undefined) {
461-
clearTrack(depsTail.nextDep);
462-
depsTail.nextDep = undefined;
463-
}
464-
} else if (sub.deps !== undefined) {
465-
clearTrack(sub.deps);
466-
sub.deps = undefined;
467-
}
468-
sub.trackId = -sub.trackId;
469-
}
470-
471432
export function clearTrack(link: Link) {
472433
do {
473434
const dep = link.dep;

0 commit comments

Comments
 (0)