@@ -40,6 +40,7 @@ import {
4040} from './context.js' ;
4141import { handle_error , invoke_error_boundary } from './error-handling.js' ;
4242import { snapshot } from '../shared/clone.js' ;
43+ import { UNINITIALIZED } from '../../constants.js' ;
4344
4445let is_flushing = false ;
4546
@@ -818,7 +819,13 @@ export function get(signal) {
818819 if ( is_derived ) {
819820 derived = /** @type {Derived } */ ( signal ) ;
820821
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+
822829 old_values . set ( derived , value ) ;
823830
824831 return value ;
@@ -828,6 +835,24 @@ export function get(signal) {
828835 return signal . v ;
829836}
830837
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+
831856/**
832857 * Like `get`, but checks for `undefined`. Used for `var` declarations because they can be accessed before being declared
833858 * @template V
0 commit comments