@@ -10,7 +10,8 @@ import {
10
10
INERT ,
11
11
RENDER_EFFECT ,
12
12
ROOT_EFFECT ,
13
- USER_EFFECT
13
+ USER_EFFECT ,
14
+ MAYBE_DIRTY
14
15
} from '#client/constants' ;
15
16
import { async_mode_flag } from '../../flags/index.js' ;
16
17
import { deferred , define_property } from '../../shared/utils.js' ;
@@ -146,6 +147,18 @@ export class Batch {
146
147
*/
147
148
#block_effects = [ ] ;
148
149
150
+ /**
151
+ * Deferred effects (which run after async work has completed) that are DIRTY
152
+ * @type {Effect[] }
153
+ */
154
+ #dirty_effects = [ ] ;
155
+
156
+ /**
157
+ * Deferred effects that are MAYBE_DIRTY
158
+ * @type {Effect[] }
159
+ */
160
+ #maybe_dirty_effects = [ ] ;
161
+
149
162
/**
150
163
* A set of branches that still exist, but will be destroyed when this batch
151
164
* is committed — we skip over these during `process`
@@ -221,10 +234,9 @@ export class Batch {
221
234
222
235
this . #deferred?. resolve ( ) ;
223
236
} else {
224
- // otherwise mark effects clean so they get scheduled on the next run
225
- for ( const e of this . #render_effects) set_signal_status ( e , CLEAN ) ;
226
- for ( const e of this . #effects) set_signal_status ( e , CLEAN ) ;
227
- for ( const e of this . #block_effects) set_signal_status ( e , CLEAN ) ;
237
+ this . #defer_effects( this . #render_effects) ;
238
+ this . #defer_effects( this . #effects) ;
239
+ this . #defer_effects( this . #block_effects) ;
228
240
}
229
241
230
242
if ( current_values ) {
@@ -307,6 +319,21 @@ export class Batch {
307
319
}
308
320
}
309
321
322
+ /**
323
+ * @param {Effect[] } effects
324
+ */
325
+ #defer_effects( effects ) {
326
+ for ( const e of effects ) {
327
+ const target = ( e . f & DIRTY ) !== 0 ? this . #dirty_effects : this . #maybe_dirty_effects;
328
+ target . push ( e ) ;
329
+
330
+ // mark as clean so they get scheduled if they depend on pending async state
331
+ set_signal_status ( e , CLEAN ) ;
332
+ }
333
+
334
+ effects . length = 0 ;
335
+ }
336
+
310
337
/**
311
338
* Associate a change to a given source with the current
312
339
* batch, noting its previous and current values
@@ -384,18 +411,13 @@ export class Batch {
384
411
this . #pending -= 1 ;
385
412
386
413
if ( this . #pending === 0 ) {
387
- for ( const e of this . #render_effects ) {
414
+ for ( const e of this . #dirty_effects ) {
388
415
set_signal_status ( e , DIRTY ) ;
389
416
schedule_effect ( e ) ;
390
417
}
391
418
392
- for ( const e of this . #effects) {
393
- set_signal_status ( e , DIRTY ) ;
394
- schedule_effect ( e ) ;
395
- }
396
-
397
- for ( const e of this . #block_effects) {
398
- set_signal_status ( e , DIRTY ) ;
419
+ for ( const e of this . #maybe_dirty_effects) {
420
+ set_signal_status ( e , MAYBE_DIRTY ) ;
399
421
schedule_effect ( e ) ;
400
422
}
401
423
0 commit comments