Skip to content

Commit 42c8d69

Browse files
committed
keep derived cache, but clear it in mark_reactions
1 parent 47ea6a5 commit 42c8d69

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,11 @@ export class Batch {
286286
this.previous.set(source, value);
287287
}
288288

289+
// Don't save errors in `batch_values`, or they won't be thrown in `runtime.js#get`
289290
if ((source.f & ERROR_VALUE) === 0) {
290291
this.current.set(source, source.v);
292+
batch_values?.set(source, source.v);
291293
}
292-
293-
// The value is now the newest known value for this source, therefore remove it from batch_values
294-
batch_values?.delete(source);
295294
}
296295

297296
activate() {

packages/svelte/src/internal/client/reactivity/deriveds.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import { equals, safe_equals } from './equality.js';
2727
import * as e from '../errors.js';
2828
import * as w from '../warnings.js';
29-
import { async_effect, destroy_effect, teardown } from './effects.js';
29+
import { async_effect, destroy_effect, effect_tracking, teardown } from './effects.js';
3030
import { eager_effects, internal_set, set_eager_effects, source } from './sources.js';
3131
import { get_stack } from '../dev/tracing.js';
3232
import { async_mode_flag, tracing_mode_flag } from '../../flags/index.js';
@@ -368,17 +368,11 @@ export function update_derived(derived) {
368368
// During time traveling we don't want to reset the status so that
369369
// traversal of the graph in the other batches still happens
370370
if (batch_values !== null) {
371-
// Delete the value as the current one is now the latest.
372-
// Deleting instead of updating handles the case where a derived
373-
// is subsequently indirectly updated in the same batch — without
374-
// deleting here we would incorrectly get the old value from `batch_values`
375-
// instead of recomputing it. The one drawback is that it's now a bit
376-
// more inefficient to get the value of that derived again in the same batch,
377-
// as it has to check is_dirty all the way up the graph all the time.
378-
// TODO if that turns out to be a performance problem, we could try
379-
// to save the current status of the derived in a map and restore it
380-
// before leaving the batch.
381-
batch_values.delete(derived);
371+
// only cache the value if we're in a tracking context, otherwise we won't
372+
// clear the cache in `mark_reactions` when dependencies are updated
373+
if (effect_tracking()) {
374+
batch_values.set(derived, derived.v);
375+
}
382376
} else {
383377
var status = (derived.f & CONNECTED) === 0 ? MAYBE_DIRTY : CLEAN;
384378
set_signal_status(derived, status);

packages/svelte/src/internal/client/reactivity/sources.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import * as e from '../errors.js';
3434
import { legacy_mode_flag, tracing_mode_flag } from '../../flags/index.js';
3535
import { get_stack, tag_proxy } from '../dev/tracing.js';
3636
import { component_context, is_runes } from '../context.js';
37-
import { Batch, eager_block_effects, schedule_effect } from './batch.js';
37+
import { Batch, batch_values, eager_block_effects, schedule_effect } from './batch.js';
3838
import { proxy } from '../proxy.js';
3939
import { execute_derived } from './deriveds.js';
4040

@@ -334,12 +334,17 @@ function mark_reactions(signal, status) {
334334
}
335335

336336
if ((flags & DERIVED) !== 0) {
337+
var derived = /** @type {Derived} */ (reaction);
338+
339+
batch_values?.delete(derived);
340+
337341
if ((flags & WAS_MARKED) === 0) {
338342
// Only connected deriveds can be reliably unmarked right away
339343
if (flags & CONNECTED) {
340344
reaction.f |= WAS_MARKED;
341345
}
342-
mark_reactions(/** @type {Derived} */ (reaction), MAYBE_DIRTY);
346+
347+
mark_reactions(derived, MAYBE_DIRTY);
343348
}
344349
} else if (not_dirty) {
345350
if ((flags & BLOCK_EFFECT) !== 0) {

0 commit comments

Comments
 (0)