fix: ensure derived is always added as a consumer

fixes #10097
derived-consumer-fix
Simon Holthausen 11 months ago
parent 1798e58300
commit 6c00666b27

@ -84,10 +84,12 @@ export let current_untracking = false;
/** Exists to opt out of the mutation validation for stores which may be set for the first time during a derivation */ /** Exists to opt out of the mutation validation for stores which may be set for the first time during a derivation */
let ignore_mutation_validation = false; let ignore_mutation_validation = false;
// If we are working with a get() chain that has no active container, /**
// to prevent memory leaks, we skip adding the consumer. * If we are working with a get() chain that has no active container,
* to prevent memory leaks, we skip adding the consumer.
*/
let current_skip_consumer = false; let current_skip_consumer = false;
// Handle collecting all signals which are read during a specific time frame /** Handle collecting all signals which are read during a specific time frame */
let is_signals_recorded = false; let is_signals_recorded = false;
let captured_signals = new Set(); let captured_signals = new Set();
@ -343,7 +345,8 @@ function execute_signal_fn(signal) {
current_consumer = signal; current_consumer = signal;
current_block = signal.b; current_block = signal.b;
current_component_context = signal.x; current_component_context = signal.x;
current_skip_consumer = current_effect === null && (signal.f & UNOWNED) !== 0; current_skip_consumer =
current_effect === null && (signal.f & UNOWNED) !== 0 && (signal.f & DERIVED) === 0;
current_untracking = false; current_untracking = false;
// Render effects are invoked when the UI is about to be updated - run beforeUpdate at that point // Render effects are invoked when the UI is about to be updated - run beforeUpdate at that point

@ -128,6 +128,27 @@ describe('signals', () => {
}; };
}); });
test('effect with derived using new derived every time', () => {
const log: number[] = [];
let count = $.source(0);
const read = () => {
const x = $.derived(() => ({ count: $.get(count) }));
return $.get(x);
};
const derivedCount = $.derived(() => read().count);
$.user_effect(() => {
log.push($.get(derivedCount));
});
return () => {
$.flushSync(() => $.set(count, 1));
$.flushSync(() => $.set(count, 2));
assert.deepEqual(log, [0, 1, 2]);
};
});
test('https://perf.js.hyoo.ru/#!bench=9h2as6_u0mfnn', () => { test('https://perf.js.hyoo.ru/#!bench=9h2as6_u0mfnn', () => {
let res: number[] = []; let res: number[] = [];

Loading…
Cancel
Save