Skip to content

Commit da19c44

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

File tree

3 files changed

+29
-46
lines changed

3 files changed

+29
-46
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: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { DirtyLevels, IEffect, Link, Subscriber } from './system.js';
1+
import { DirtyLevels, IEffect, Link, Subscriber, System } from './system.js';
2+
3+
export let activeEffectScope: IEffect | undefined = undefined;
24

35
export function effectScope() {
46
return new EffectScope();
@@ -14,7 +16,7 @@ export class EffectScope implements IEffect {
1416
// Subscriber
1517
deps: Link | undefined = undefined;
1618
depsTail: Link | undefined = undefined;
17-
trackId = 0;
19+
readonly trackId = ++System.lastTrackId;
1820
dirtyLevel = DirtyLevels.None;
1921
canPropagate = false;
2022

@@ -26,11 +28,28 @@ export class EffectScope implements IEffect {
2628
}
2729

2830
run<T>(fn: () => T) {
29-
const prevSub = Subscriber.startTrackEffects(this);
31+
const prevSub = activeEffectScope;
32+
activeEffectScope = this;
33+
this.depsTail = undefined;
34+
this.dirtyLevel = DirtyLevels.None;
3035
try {
3136
return fn();
3237
} finally {
33-
Subscriber.endTrackEffects(this, prevSub);
38+
if (prevSub !== undefined) {
39+
activeEffectScope = prevSub;
40+
} else {
41+
activeEffectScope = undefined;
42+
}
43+
const depsTail = this.depsTail as Link | undefined;
44+
if (depsTail !== undefined) {
45+
if (depsTail.nextDep !== undefined) {
46+
Subscriber.clearTrack(depsTail.nextDep);
47+
depsTail.nextDep = undefined;
48+
}
49+
} else if (this.deps !== undefined) {
50+
Subscriber.clearTrack(this.deps);
51+
this.deps = undefined;
52+
}
3453
}
3554
}
3655

src/system.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -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)