Skip to content

chore: document and test signals edge case #10228

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

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
6 changes: 5 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ export let current_effect = null;
/** @type {null | import('./types.js').Signal[]} */
let current_dependencies = null;
let current_dependencies_index = 0;
/** @type {null | import('./types.js').Signal[]} */
/**
* Tracks writes that the effect it's executed in doesn't listen to yet,
* so that the dependency can be added to the effect later on if it then reads it
* @type {null | import('./types.js').Signal[]}
*/
let current_untracked_writes = null;
/** @type {null | import('./types.js').SignalDebug} */
let last_inspected_signal = null;
Expand Down
27 changes: 24 additions & 3 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import * as $ from '../../src/internal/client/runtime';
* @param fn A function that returns a function because we first need to setup all the signals
* and then execute the test in order to simulate a real component
*/
function run_test(runes: boolean, fn: () => () => void) {
function run_test(runes: boolean, fn: (runes: boolean) => () => void) {
return () => {
// Create a component context to test runes vs legacy mode
$.push({}, runes);
// Create a render context so that effect validations etc don't fail
let execute: any;
const signal = $.render_effect(
() => {
execute = fn();
execute = fn(runes);
},
null,
true,
Expand All @@ -26,7 +26,7 @@ function run_test(runes: boolean, fn: () => () => void) {
};
}

function test(text: string, fn: () => any) {
function test(text: string, fn: (runes: boolean) => any) {
it(`${text} (legacy mode)`, run_test(false, fn));
it(`${text} (runes mode)`, run_test(true, fn));
}
Expand Down Expand Up @@ -228,4 +228,25 @@ describe('signals', () => {
assert.deepEqual(count.c, null);
};
});

test('schedules rerun when writing to signal before reading it', (runes) => {
if (!runes) return () => {};

const value = $.source({ count: 0 });
$.user_effect(() => {
$.set(value, { count: 0 });
$.get(value);
});

return () => {
let errored = false;
try {
$.flushSync();
} catch (e: any) {
assert.include(e.message, 'ERR_SVELTE_TOO_MANY_UPDATES');
errored = true;
}
assert.equal(errored, true);
};
});
});