@@ -29,8 +29,7 @@ import {
29
29
ROOT_EFFECT ,
30
30
LEGACY_DERIVED_PROP ,
31
31
DISCONNECTED ,
32
- STATE_FROZEN_SYMBOL ,
33
- INSPECT_EFFECT
32
+ STATE_FROZEN_SYMBOL
34
33
} from './constants.js' ;
35
34
import { flush_tasks } from './dom/task.js' ;
36
35
import { add_owner } from './dev/ownership.js' ;
@@ -63,8 +62,6 @@ export function set_is_destroying_effect(value) {
63
62
is_destroying_effect = value ;
64
63
}
65
64
66
- export let inspect_effects = new Set ( ) ;
67
-
68
65
// Handle effect queues
69
66
70
67
/** @type {import('#client').Effect[] } */
@@ -164,77 +161,44 @@ export function is_runes() {
164
161
*/
165
162
export function check_dirtiness ( reaction ) {
166
163
var flags = reaction . f ;
167
- var is_dirty = ( flags & DIRTY ) !== 0 ;
168
164
169
- if ( is_dirty ) {
165
+ if ( ( flags & DIRTY ) !== 0 ) {
170
166
return true ;
171
167
}
172
168
173
- var is_unowned = ( flags & UNOWNED ) !== 0 ;
174
- var is_disconnected = ( flags & DISCONNECTED ) !== 0 ;
175
-
176
169
if ( ( flags & MAYBE_DIRTY ) !== 0 ) {
177
170
var dependencies = reaction . deps ;
178
171
179
172
if ( dependencies !== null ) {
180
- var length = dependencies . length ;
181
- var reactions ;
173
+ var is_unowned = ( flags & UNOWNED ) !== 0 ;
182
174
183
- for ( var i = 0 ; i < length ; i ++ ) {
175
+ for ( var i = 0 ; i < dependencies . length ; i ++ ) {
184
176
var dependency = dependencies [ i ] ;
185
177
186
- if ( ! is_dirty && check_dirtiness ( /** @type {import('#client').Derived } */ ( dependency ) ) ) {
187
- update_derived ( /** @type {import('#client').Derived } ** / ( dependency ) ) ;
178
+ if ( check_dirtiness ( /** @type {import('#client').Derived } */ ( dependency ) ) ) {
179
+ update_derived ( /** @type {import('#client').Derived } */ ( dependency ) ) ;
188
180
}
189
181
190
- if ( ( reaction . f & DIRTY ) !== 0 ) {
191
- // `reaction` might now be dirty, as a result of calling `update_derived`
182
+ if ( dependency . version > reaction . version ) {
192
183
return true ;
193
184
}
194
185
195
186
if ( is_unowned ) {
196
- // If we're working with an unowned derived signal, then we need to check
197
- // if our dependency write version is higher. If it is then we can assume
198
- // that state has changed to a newer version and thus this unowned signal
199
- // is also dirty.
200
- if ( dependency . version > /** @type {import('#client').Derived } */ ( reaction ) . version ) {
201
- return true ;
202
- }
203
-
187
+ // TODO is there a more logical place to do this work?
204
188
if ( ! current_skip_reaction && ! dependency ?. reactions ?. includes ( reaction ) ) {
205
189
// If we are working with an unowned signal as part of an effect (due to !current_skip_reaction)
206
190
// and the version hasn't changed, we still need to check that this reaction
207
191
// if linked to the dependency source – otherwise future updates will not be caught.
208
192
( dependency . reactions ??= [ ] ) . push ( reaction ) ;
209
193
}
210
- } else if ( is_disconnected ) {
211
- // It might be that the derived was was dereferenced from its dependencies but has now come alive again.
212
- // In thise case, we need to re-attach it to the graph and mark it dirty if any of its dependencies have
213
- // changed since.
214
- if ( dependency . version > /** @type {import('#client').Derived } */ ( reaction ) . version ) {
215
- is_dirty = true ;
216
- }
217
-
218
- reactions = dependency . reactions ;
219
- if ( reactions === null ) {
220
- dependency . reactions = [ reaction ] ;
221
- } else if ( ! reactions . includes ( reaction ) ) {
222
- reactions . push ( reaction ) ;
223
- }
224
194
}
225
195
}
226
196
}
227
197
228
- // Unowned signals are always maybe dirty, as we instead check their dependency versions.
229
- if ( ! is_unowned ) {
230
- set_signal_status ( reaction , CLEAN ) ;
231
- }
232
- if ( is_disconnected ) {
233
- reaction . f ^= DISCONNECTED ;
234
- }
198
+ set_signal_status ( reaction , CLEAN ) ;
235
199
}
236
200
237
- return is_dirty ;
201
+ return false ;
238
202
}
239
203
240
204
/**
@@ -490,6 +454,8 @@ export function update_effect(effect) {
490
454
execute_effect_teardown ( effect ) ;
491
455
var teardown = update_reaction ( effect ) ;
492
456
effect . teardown = typeof teardown === 'function' ? teardown : null ;
457
+
458
+ effect . version = current_version ;
493
459
} catch ( error ) {
494
460
handle_error ( /** @type {Error } */ ( error ) , effect , current_component_context ) ;
495
461
} finally {
@@ -798,11 +764,31 @@ export function get(signal) {
798
764
}
799
765
}
800
766
801
- if (
802
- ( flags & DERIVED ) !== 0 &&
803
- check_dirtiness ( /** @type {import('#client').Derived } */ ( signal ) )
804
- ) {
805
- update_derived ( /** @type {import('#client').Derived } **/ ( signal ) ) ;
767
+ if ( ( flags & DERIVED ) !== 0 ) {
768
+ var derived = /** @type {import('#client').Derived } */ ( signal ) ;
769
+
770
+ if ( check_dirtiness ( derived ) ) {
771
+ update_derived ( derived ) ;
772
+ }
773
+
774
+ if ( ( flags & DISCONNECTED ) !== 0 ) {
775
+ // reconnect to the graph
776
+ deps = derived . deps ;
777
+
778
+ if ( deps !== null ) {
779
+ for ( var i = 0 ; i < deps . length ; i ++ ) {
780
+ var dep = deps [ i ] ;
781
+ var reactions = dep . reactions ;
782
+ if ( reactions === null ) {
783
+ dep . reactions = [ derived ] ;
784
+ } else if ( ! reactions . includes ( derived ) ) {
785
+ reactions . push ( derived ) ;
786
+ }
787
+ }
788
+ }
789
+
790
+ derived . f ^= DISCONNECTED ;
791
+ }
806
792
}
807
793
808
794
return signal . v ;
@@ -845,52 +831,6 @@ export function invalidate_inner_signals(fn) {
845
831
}
846
832
}
847
833
848
- /**
849
- * @param {import('#client').Value } signal
850
- * @param {number } status should be DIRTY or MAYBE_DIRTY
851
- * @param {boolean } force_schedule
852
- * @returns {void }
853
- */
854
- export function mark_reactions ( signal , status , force_schedule ) {
855
- var reactions = signal . reactions ;
856
- if ( reactions === null ) return ;
857
-
858
- var runes = is_runes ( ) ;
859
- var length = reactions . length ;
860
-
861
- for ( var i = 0 ; i < length ; i ++ ) {
862
- var reaction = reactions [ i ] ;
863
- var flags = reaction . f ;
864
-
865
- if ( DEV && ( flags & INSPECT_EFFECT ) !== 0 ) {
866
- inspect_effects . add ( reaction ) ;
867
- continue ;
868
- }
869
-
870
- // We skip any effects that are already dirty. Additionally, we also
871
- // skip if the reaction is the same as the current effect (except if we're not in runes or we
872
- // are in force schedule mode).
873
- if ( ( flags & DIRTY ) !== 0 || ( ( ! force_schedule || ! runes ) && reaction === current_effect ) ) {
874
- continue ;
875
- }
876
-
877
- set_signal_status ( reaction , status ) ;
878
-
879
- // If the signal a) was previously clean or b) is an unowned derived, then mark it
880
- if ( ( flags & ( CLEAN | UNOWNED ) ) !== 0 ) {
881
- if ( ( flags & DERIVED ) !== 0 ) {
882
- mark_reactions (
883
- /** @type {import('#client').Derived } */ ( reaction ) ,
884
- MAYBE_DIRTY ,
885
- force_schedule
886
- ) ;
887
- } else {
888
- schedule_effect ( /** @type {import('#client').Effect } */ ( reaction ) ) ;
889
- }
890
- }
891
- }
892
- }
893
-
894
834
/**
895
835
* Use `untrack` to prevent something from being treated as an `$effect`/`$derived` dependency.
896
836
*
0 commit comments