Skip to content
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

fix: avoid stack overflows from derived recursion #12739

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: avoid stack overflows from derived recursion
  • Loading branch information
trueadm committed Aug 5, 2024
commit 2ba716c5717bcf9e4a1620d60f5b808f581609bb
5 changes: 5 additions & 0 deletions .changeset/thick-chefs-change.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
'svelte': patch
---

fix: avoid stack overflows from derived recursion
13 changes: 9 additions & 4 deletions packages/svelte/src/internal/client/reactivity/deriveds.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,8 @@ import {
} from '../runtime.js';
import { equals, safe_equals } from './equality.js';

export let updating_derived = false;
/** @type {Derived[]} */
export let updating_deriveds = [];

/**
* @template V
Expand Down Expand Up @@ -84,11 85,15 @@ function destroy_derived_children(derived) {
* @returns {void}
*/
export function update_derived(derived) {
var previous_updating_derived = updating_derived;
updating_derived = true;
// If we're already updating this derived (recursively) then bail-out
// of re-calling the derived again to prevent a stack-overflow.
if (updating_deriveds.includes(derived)) {
return;
}
updating_deriveds.push(derived);
destroy_derived_children(derived);
var value = update_reaction(derived);
updating_derived = previous_updating_derived;
updating_deriveds.pop();

var status =
(current_skip_reaction || (derived.f & UNOWNED) !== 0) && derived.deps !== null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
html: `<p>Is even</p><button>clicks: 0</button><button>disable</button>`,

test({ assert, target }) {
const [b1, b2] = target.querySelectorAll('button');

b1?.click();
flushSync();

assert.htmlEqual(target.innerHTML, `<p>Is odd</p><button>clicks: 1</button><button>disable</button>`);

b2?.click();
flushSync();

assert.htmlEqual(target.innerHTML, `<p>Is odd</p><button>clicks: 1</button><button>disable</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 1,22 @@
<script>
let count = $state(0);
let disabled = $state(false);

let even = $derived.by(() => {
if (disabled) return definitely_even;

return count % 2 === 0;
});


let definitely_even = $derived(even);

function increment() {
count = 1;
}
</script>

<p>Is {even ? 'even' : 'odd'}</p>

<button onclick={increment}>clicks: {count}</button>
<button onclick={() => disabled = true}>disable</button>
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
html: `<p>Is even</p><button>clicks: 0</button><button>disable</button>`,

test({ assert, target }) {
const [b1, b2] = target.querySelectorAll('button');

b1?.click();
flushSync();

assert.htmlEqual(target.innerHTML, `<p>Is odd</p><button>clicks: 1</button><button>disable</button>`);

b2?.click();
flushSync();

assert.htmlEqual(target.innerHTML, `<p>Is odd</p><button>clicks: 1</button><button>disable</button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
<script>
let count = $state(0);
let disabled = $state(false);

let even = $derived.by(() => {
if (disabled) return even;

return count % 2 === 0;
})

function increment() {
count = 1;
}
</script>

<p>Is {even ? 'even' : 'odd'}</p>

<button onclick={increment}>clicks: {count}</button>
<button onclick={() => disabled = true}>disable</button>
Loading