Skip to content

fix: remove memory leak, make beforeUpdate etc work correctly #10570

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 5 commits into from
Feb 20, 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
5 changes: 5 additions & 0 deletions .changeset/kind-dots-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: remove memory leak
5 changes: 5 additions & 0 deletions .changeset/real-pandas-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: call beforeUpdate/afterUpdate callbacks when props are mutated
34 changes: 9 additions & 25 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,6 @@ export function derived(init) {
create_computation_signal(flags | CLEAN, UNINITIALIZED, current_block)
);
signal.i = init;
bind_signal_to_component_context(signal);
signal.e = default_equals;
if (current_consumer !== null) {
push_reference(current_consumer, signal);
Expand All @@ -1335,26 +1334,7 @@ export function derived_safe_equal(init) {
*/
/*#__NO_SIDE_EFFECTS__*/
export function source(initial_value) {
const source = create_source_signal(SOURCE | CLEAN, initial_value);
bind_signal_to_component_context(source);
return source;
}

/**
* This function binds a signal to the component context, so that we can fire
* beforeUpdate/afterUpdate callbacks at the correct time etc
* @param {import('./types.js').Signal} signal
*/
function bind_signal_to_component_context(signal) {
if (current_component_context === null || !current_component_context.r) return;

const signals = current_component_context.d;

if (signals) {
signals.push(signal);
} else {
current_component_context.d = [signal];
}
return create_source_signal(SOURCE | CLEAN, initial_value);
}

/**
Expand All @@ -1366,6 +1346,13 @@ function bind_signal_to_component_context(signal) {
export function mutable_source(initial_value) {
const s = source(initial_value);
s.e = safe_equal;

// bind the signal to the component context, in case we need to
// track updates to trigger beforeUpdate/afterUpdate callbacks
if (current_component_context) {
(current_component_context.d ??= []).push(s);
}

return s;
}

Expand Down Expand Up @@ -1971,10 +1958,7 @@ function observe_all(context) {
for (const signal of context.d) get(signal);
}

const props = get_descriptors(context.s);
for (const descriptor of Object.values(props)) {
if (descriptor.get) descriptor.get();
}
deep_read(context.s);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<svelte:options runes={false} />

<script>
import { beforeUpdate } from 'svelte';
import { logs } from './logs.js'

export let object;

beforeUpdate(() => {
logs.push('changed');
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
import { logs } from './logs.js';

export default test({
html: `<button>clicks: 0</button>`,

test({ assert, target }) {
const btn = target.querySelector('button');

flushSync(() => btn?.click());

assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`);
assert.deepEqual(logs, ['changed', 'changed']);
},

after_test() {
logs.length = 0;
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {any[]} */
export const logs = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import Child from './Child.svelte';
let object = $state({ count: 0 })
</script>

<button onclick={() => object.count += 1}>
clicks: {object.count}
</button>

<Child {object} />