File tree Expand file tree Collapse file tree 3 files changed +35
-38
lines changed
tests/runtime-runes/samples/derived-cleanup-old-value Expand file tree Collapse file tree 3 files changed +35
-38
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ import {
2626} from './constants.js' ;
2727import { flush_tasks } from './dom/task.js' ;
2828import { internal_set , old_values } from './reactivity/sources.js' ;
29- import { destroy_derived_effects , update_derived } from './reactivity/deriveds.js' ;
29+ import { destroy_derived_effects , execute_derived , update_derived } from './reactivity/deriveds.js' ;
3030import * as e from './errors.js' ;
3131
3232import { tracing_mode_flag } from '../flags/index.js' ;
@@ -764,7 +764,7 @@ export function get(signal) {
764764 }
765765 }
766766
767- if ( is_derived ) {
767+ if ( is_derived && ! is_destroying_effect ) {
768768 derived = /** @type {Derived } */ ( signal ) ;
769769
770770 if ( check_dirtiness ( derived ) ) {
@@ -805,8 +805,19 @@ export function get(signal) {
805805 }
806806 }
807807
808- if ( is_destroying_effect && old_values . has ( signal ) ) {
809- return old_values . get ( signal ) ;
808+ if ( is_destroying_effect ) {
809+ if ( old_values . has ( signal ) ) {
810+ return old_values . get ( signal ) ;
811+ }
812+
813+ if ( is_derived ) {
814+ derived = /** @type {Derived } */ ( signal ) ;
815+
816+ var value = execute_derived ( derived ) ;
817+ old_values . set ( derived , value ) ;
818+
819+ return value ;
820+ }
810821 }
811822
812823 return signal . v ;
Original file line number Diff line number Diff line change @@ -3,37 +3,20 @@ import { test } from '../../test';
33
44export default test ( {
55 async test ( { assert, logs, target } ) {
6- /** @type {HTMLButtonElement | null } */
7- const increment_btn = target . querySelector ( '#increment' ) ;
8- /** @type {HTMLButtonElement | null } */
9- const overwrite_btn = target . querySelector ( '#overwrite' ) ;
6+ const [ increment ] = target . querySelectorAll ( 'button' ) ;
107
11- // Initial state: count=1, derived_value=1
8+ flushSync ( ( ) => increment . click ( ) ) ;
9+ flushSync ( ( ) => increment . click ( ) ) ;
10+ flushSync ( ( ) => increment . click ( ) ) ;
1211
13- // Click to increment count: count=2, derived_value=4
14- flushSync ( ( ) => {
15- increment_btn ?. click ( ) ;
16- } ) ;
17-
18- // Click to increment count: count=3, derived_value=9
19- flushSync ( ( ) => {
20- increment_btn ?. click ( ) ;
21- } ) ;
22-
23- // Click to overwrite derived_value: count=3, derived_value=7
24- flushSync ( ( ) => {
25- overwrite_btn ?. click ( ) ;
26- } ) ;
27-
28- // Should log old value during cleanup (4) and new value during setup (9)
2912 assert . deepEqual ( logs , [
30- '$effect : 1' ,
31- '$effect teardown : 1' ,
32- '$effect: 4 ' ,
33- '$effect teardown : 4' ,
34- '$effect: 9 ' ,
35- '$effect teardown : 9' ,
36- '$effect: 7 '
13+ 'count : 1' ,
14+ 'squared : 1' ,
15+ 'count: 2 ' ,
16+ 'squared : 4' ,
17+ 'count: 3 ' ,
18+ 'squared : 9' ,
19+ 'count: 4 '
3720 ] ) ;
3821 }
3922} ) ;
Original file line number Diff line number Diff line change 11<script >
22 let count = $state (1 );
3- let derived_value = $derived (count * count);
3+ let squared = $derived (count * count);
44
55 $effect (() => {
6- console .log (` $effect: ${ derived_value} ` );
6+ console .log (` count: ${ count} ` );
7+
78 return () => {
8- console .log (` $effect teardown : ${ derived_value } ` );
9+ console .log (` squared : ${ squared } ` );
910 };
1011 });
1112 </script >
1213
13- <button id ="increment" onclick ={() => count ++ }>Increment s</button >
14- <button id ="overwrite" onclick ={() => (derived_value = 7 )}>Overwrite derived_value</button >
14+ <button onclick ={() => count ++ }>increment</button >
1515
1616<p >count: {count }</p >
17- <p >derived_value: {derived_value }</p >
17+
18+ {#if count % 2 === 0 }
19+ <p id ="squared" >squared: {squared }</p >
20+ {/if }
You can’t perform that action at this time.
0 commit comments