@@ -40,6 +40,7 @@ import {
40
40
} from './context.js' ;
41
41
import { handle_error , invoke_error_boundary } from './error-handling.js' ;
42
42
import { snapshot } from '../shared/clone.js' ;
43
+ import { UNINITIALIZED } from '../../constants.js' ;
43
44
44
45
let is_flushing = false ;
45
46
@@ -818,7 +819,13 @@ export function get(signal) {
818
819
if ( is_derived ) {
819
820
derived = /** @type {Derived } */ ( signal ) ;
820
821
821
- var value = ( derived . f & CLEAN ) !== 0 ? execute_derived ( derived ) : derived . v ;
822
+ var value = derived . v ;
823
+
824
+ // if the derived is dirty, or depends on the values that just changed, re-execute
825
+ if ( ( derived . f & CLEAN ) !== 0 || depends_on_old_values ( derived ) ) {
826
+ value = execute_derived ( derived ) ;
827
+ }
828
+
822
829
old_values . set ( derived , value ) ;
823
830
824
831
return value ;
@@ -828,6 +835,24 @@ export function get(signal) {
828
835
return signal . v ;
829
836
}
830
837
838
+ /** @param {Derived } derived */
839
+ function depends_on_old_values ( derived ) {
840
+ if ( derived . v === UNINITIALIZED ) return true ; // we don't know, so assume the worst
841
+ if ( derived . deps === null ) return false ;
842
+
843
+ for ( const dep of derived . deps ) {
844
+ if ( old_values . has ( dep ) ) {
845
+ return true ;
846
+ }
847
+
848
+ if ( ( dep . f & DERIVED ) !== 0 && depends_on_old_values ( /** @type {Derived } */ ( dep ) ) ) {
849
+ return true ;
850
+ }
851
+ }
852
+
853
+ return false ;
854
+ }
855
+
831
856
/**
832
857
* Like `get`, but checks for `undefined`. Used for `var` declarations because they can be accessed before being declared
833
858
* @template V
0 commit comments