Skip to content

Commit 7dfb217

Browse files
committed
refactor: enhance effect handling with dedicated EffectScope interface
1 parent 9538b9e commit 7dfb217

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/index.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ export * from './system.js';
22

33
import { createReactiveSystem, Dependency, Subscriber, SubscriberFlags } from './system.js';
44

5+
interface EffectScope extends Subscriber {
6+
isScope: true;
7+
}
8+
59
interface Effect extends Subscriber, Dependency {
6-
isScope?: true;
710
fn(): void;
811
}
912

@@ -47,27 +50,19 @@ const {
4750
endTracking(computed);
4851
}
4952
},
50-
notifyEffect(e: Effect) {
51-
const flags = e.flags;
52-
if (
53-
!e.isScope
54-
&& (
55-
flags & SubscriberFlags.Dirty
56-
|| (flags & SubscriberFlags.PendingComputed && updateDirtyFlag(e, flags))
57-
)
58-
) {
59-
runEffect(e);
53+
notifyEffect(e: Effect | EffectScope) {
54+
if ('isScope' in e) {
55+
return notifyEffectScope(e);
6056
} else {
61-
processPendingInnerEffects(e, e.flags);
57+
return notifyEffect(e);
6258
}
63-
return true;
6459
},
6560
});
6661
const pauseStack: (Subscriber | undefined)[] = [];
6762

6863
let batchDepth = 0;
6964
let activeSub: Subscriber | undefined;
70-
let activeScope: Subscriber | undefined;
65+
let activeScope: EffectScope | undefined;
7166

7267
//#region Public functions
7368
export function startBatch() {
@@ -130,19 +125,13 @@ export function effect<T>(fn: () => T): () => void {
130125
}
131126

132127
export function effectScope<T>(fn: () => T): () => void {
133-
const e: Effect = {
134-
fn,
135-
subs: undefined,
136-
subsTail: undefined,
128+
const e: EffectScope = {
137129
deps: undefined,
138130
depsTail: undefined,
139131
flags: SubscriberFlags.Effect,
140132
isScope: true,
141133
};
142-
if (activeSub !== undefined) {
143-
link(e, activeSub);
144-
}
145-
runEffectScope(e);
134+
runEffectScope(e, fn);
146135
return effectStop.bind(e);
147136
}
148137
//#endregion
@@ -160,17 +149,39 @@ function runEffect(e: Effect): void {
160149
}
161150
}
162151

163-
function runEffectScope(e: Effect): void {
152+
function runEffectScope(e: EffectScope, fn: () => void): void {
164153
const prevSub = activeScope;
165154
activeScope = e;
166155
startTracking(e);
167156
try {
168-
e.fn();
157+
fn();
169158
} finally {
170159
activeScope = prevSub;
171160
endTracking(e);
172161
}
173162
}
163+
164+
function notifyEffect(e: Effect): boolean {
165+
const flags = e.flags;
166+
if (
167+
flags & SubscriberFlags.Dirty
168+
|| (flags & SubscriberFlags.PendingComputed && updateDirtyFlag(e, flags))
169+
) {
170+
runEffect(e);
171+
} else {
172+
processPendingInnerEffects(e, e.flags);
173+
}
174+
return true;
175+
}
176+
177+
function notifyEffectScope(e: EffectScope): boolean {
178+
const flags = e.flags;
179+
if (flags & SubscriberFlags.PendingEffect) {
180+
processPendingInnerEffects(e, e.flags);
181+
return true;
182+
}
183+
return false;
184+
}
174185
//#endregion
175186

176187
//#region Bound functions

0 commit comments

Comments
 (0)