@@ -349,6 +349,15 @@ function execute_signal_fn(signal) {
349
349
350
350
if ( current_dependencies !== null ) {
351
351
let i ;
352
+ if ( dependencies !== null ) {
353
+ for ( i = current_dependencies_index ; i < dependencies . length ; i ++ ) {
354
+ const dependency = dependencies [ i ] ;
355
+ if ( ! current_dependencies . includes ( dependency ) ) {
356
+ remove_consumer ( signal , dependency , false ) ;
357
+ }
358
+ }
359
+ }
360
+
352
361
if ( dependencies !== null && current_dependencies_index > 0 ) {
353
362
dependencies . length = current_dependencies_index + current_dependencies . length ;
354
363
for ( i = 0 ; i < current_dependencies . length ; i ++ ) {
@@ -373,7 +382,7 @@ function execute_signal_fn(signal) {
373
382
}
374
383
}
375
384
} else if ( dependencies !== null && current_dependencies_index < dependencies . length ) {
376
- remove_consumer ( signal , current_dependencies_index , false ) ;
385
+ remove_consumers ( signal , current_dependencies_index , false ) ;
377
386
dependencies . length = current_dependencies_index ;
378
387
}
379
388
return res ;
@@ -389,43 +398,54 @@ function execute_signal_fn(signal) {
389
398
}
390
399
}
391
400
401
+ /**
402
+ * @template V
403
+ * @param {import('./types.js').ComputationSignal<V> } signal
404
+ * @param {import('./types.js').Signal<V> } dependency
405
+ * @param {boolean } remove_unowned
406
+ * @returns {void }
407
+ */
408
+ function remove_consumer ( signal , dependency , remove_unowned ) {
409
+ const consumers = dependency . c ;
410
+ let consumers_length = 0 ;
411
+ if ( consumers !== null ) {
412
+ consumers_length = consumers . length - 1 ;
413
+ const index = consumers . indexOf ( signal ) ;
414
+ if ( index !== - 1 ) {
415
+ if ( consumers_length === 0 ) {
416
+ dependency . c = null ;
417
+ } else {
418
+ // Swap with last element and then remove.
419
+ consumers [ index ] = consumers [ consumers_length ] ;
420
+ consumers . pop ( ) ;
421
+ }
422
+ }
423
+ }
424
+ if ( remove_unowned && consumers_length === 0 && ( dependency . f & UNOWNED ) !== 0 ) {
425
+ // If the signal is unowned then we need to make sure to change it to dirty.
426
+ set_signal_status ( dependency , DIRTY ) ;
427
+ remove_consumers (
428
+ /** @type {import('./types.js').ComputationSignal<V> } **/ ( dependency ) ,
429
+ 0 ,
430
+ true
431
+ ) ;
432
+ }
433
+ }
434
+
392
435
/**
393
436
* @template V
394
437
* @param {import('./types.js').ComputationSignal<V> } signal
395
438
* @param {number } start_index
396
439
* @param {boolean } remove_unowned
397
440
* @returns {void }
398
441
*/
399
- function remove_consumer ( signal , start_index , remove_unowned ) {
442
+ function remove_consumers ( signal , start_index , remove_unowned ) {
400
443
const dependencies = signal . d ;
401
444
if ( dependencies !== null ) {
402
445
let i ;
403
446
for ( i = start_index ; i < dependencies . length ; i ++ ) {
404
447
const dependency = dependencies [ i ] ;
405
- const consumers = dependency . c ;
406
- let consumers_length = 0 ;
407
- if ( consumers !== null ) {
408
- consumers_length = consumers . length - 1 ;
409
- const index = consumers . indexOf ( signal ) ;
410
- if ( index !== - 1 ) {
411
- if ( consumers_length === 0 ) {
412
- dependency . c = null ;
413
- } else {
414
- // Swap with last element and then remove.
415
- consumers [ index ] = consumers [ consumers_length ] ;
416
- consumers . pop ( ) ;
417
- }
418
- }
419
- }
420
- if ( remove_unowned && consumers_length === 0 && ( dependency . f & UNOWNED ) !== 0 ) {
421
- // If the signal is unowned then we need to make sure to change it to dirty.
422
- set_signal_status ( dependency , DIRTY ) ;
423
- remove_consumer (
424
- /** @type {import('./types.js').ComputationSignal<V> } **/ ( dependency ) ,
425
- 0 ,
426
- true
427
- ) ;
428
- }
448
+ remove_consumer ( signal , dependency , remove_unowned ) ;
429
449
}
430
450
}
431
451
}
@@ -445,7 +465,7 @@ function destroy_references(signal) {
445
465
if ( ( reference . f & IS_EFFECT ) !== 0 ) {
446
466
destroy_signal ( reference ) ;
447
467
} else {
448
- remove_consumer ( reference , 0 , true ) ;
468
+ remove_consumers ( reference , 0 , true ) ;
449
469
reference . d = null ;
450
470
}
451
471
}
@@ -1103,7 +1123,7 @@ export function destroy_signal(signal) {
1103
1123
const destroy = signal . y ;
1104
1124
const flags = signal . f ;
1105
1125
destroy_references ( signal ) ;
1106
- remove_consumer ( signal , 0 , true ) ;
1126
+ remove_consumers ( signal , 0 , true ) ;
1107
1127
signal . i =
1108
1128
signal . r =
1109
1129
signal . y =
0 commit comments