Skip to content

fix: ensure derived effects are recreated when needed #16595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const REACTION_IS_UPDATING = 1 << 21;
export const ASYNC = 1 << 22;

export const ERROR_VALUE = 1 << 23;
export const HAS_EFFECTS = 1 << 24;

export const STATE_SYMBOL = Symbol('$state');
export const LEGACY_PROPS = Symbol('legacy props');
Expand Down
5 changes: 4 additions & 1 deletion packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
EFFECT_PRESERVED,
STALE_REACTION,
USER_EFFECT,
ASYNC
ASYNC,
HAS_EFFECTS
} from '#client/constants';
import * as e from '../errors.js';
import { DEV } from 'esm-env';
Expand Down Expand Up @@ -156,6 +157,8 @@ function create_effect(type, fn, sync, push = true) {
) {
var derived = /** @type {Derived} */ (active_reaction);
(derived.effects ??= []).push(effect);
// Mark the derived as having effects
derived.f |= HAS_EFFECTS;
}
}

Expand Down
10 changes: 9 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
DISCONNECTED,
REACTION_IS_UPDATING,
STALE_REACTION,
ERROR_VALUE
ERROR_VALUE,
HAS_EFFECTS
} from './constants.js';
import { old_values } from './reactivity/sources.js';
import {
Expand Down Expand Up @@ -671,6 +672,13 @@ export function get(signal) {

if (is_dirty(derived)) {
update_derived(derived);
} else if (
(derived.f & HAS_EFFECTS) !== 0 &&
(derived.effects === null || derived.effects.length === 0)
) {
// If the derived once had effects but they're now missing (destroyed),
// we need to re-execute to recreate them
update_derived(derived);
}
}

Expand Down
46 changes: 46 additions & 0 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1441,4 +1441,50 @@ describe('signals', () => {
assert.deepEqual(log, ['inner destroyed', 'inner destroyed']);
};
});

test('derived effects reconnect correctly', () => {
const log: string[] = [];
let a: Derived<number>;

return () => {
const destroy1 = effect_root(() => {
a = derived(() => {
user_effect(() => {
log.push('effect-executed');
});
return 42;
});
});

const destroy2 = effect_root(() => {
render_effect(() => {
$.get(a);
});
});

assert.equal(log.length, 0);
assert.equal(a?.effects?.length, 1);

destroy2();
flushSync();

assert.equal(a?.effects, null);
assert.equal(log.length, 0);

const destroy3 = effect_root(() => {
render_effect(() => {
const value = $.get(a);
assert.equal(value, 42);
});
});

flushSync();

assert.equal(log.length, 1);
assert.equal(a?.effects?.length, 1);

destroy3();
destroy1();
};
});
});
Loading