@@ -2,8 +2,11 @@ export * from './system.js';
2
2
3
3
import { createReactiveSystem , Dependency , Subscriber , SubscriberFlags } from './system.js' ;
4
4
5
+ interface EffectScope extends Subscriber {
6
+ isScope : true ;
7
+ }
8
+
5
9
interface Effect extends Subscriber , Dependency {
6
- isScope ?: true ;
7
10
fn ( ) : void ;
8
11
}
9
12
@@ -47,27 +50,19 @@ const {
47
50
endTracking ( computed ) ;
48
51
}
49
52
} ,
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 ) ;
60
56
} else {
61
- processPendingInnerEffects ( e , e . flags ) ;
57
+ return notifyEffect ( e ) ;
62
58
}
63
- return true ;
64
59
} ,
65
60
} ) ;
66
61
const pauseStack : ( Subscriber | undefined ) [ ] = [ ] ;
67
62
68
63
let batchDepth = 0 ;
69
64
let activeSub : Subscriber | undefined ;
70
- let activeScope : Subscriber | undefined ;
65
+ let activeScope : EffectScope | undefined ;
71
66
72
67
//#region Public functions
73
68
export function startBatch ( ) {
@@ -130,19 +125,13 @@ export function effect<T>(fn: () => T): () => void {
130
125
}
131
126
132
127
export function effectScope < T > ( fn : ( ) => T ) : ( ) => void {
133
- const e : Effect = {
134
- fn,
135
- subs : undefined ,
136
- subsTail : undefined ,
128
+ const e : EffectScope = {
137
129
deps : undefined ,
138
130
depsTail : undefined ,
139
131
flags : SubscriberFlags . Effect ,
140
132
isScope : true ,
141
133
} ;
142
- if ( activeSub !== undefined ) {
143
- link ( e , activeSub ) ;
144
- }
145
- runEffectScope ( e ) ;
134
+ runEffectScope ( e , fn ) ;
146
135
return effectStop . bind ( e ) ;
147
136
}
148
137
//#endregion
@@ -160,17 +149,39 @@ function runEffect(e: Effect): void {
160
149
}
161
150
}
162
151
163
- function runEffectScope ( e : Effect ) : void {
152
+ function runEffectScope ( e : EffectScope , fn : ( ) => void ) : void {
164
153
const prevSub = activeScope ;
165
154
activeScope = e ;
166
155
startTracking ( e ) ;
167
156
try {
168
- e . fn ( ) ;
157
+ fn ( ) ;
169
158
} finally {
170
159
activeScope = prevSub ;
171
160
endTracking ( e ) ;
172
161
}
173
162
}
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
+ }
174
185
//#endregion
175
186
176
187
//#region Bound functions
0 commit comments