From a7bc518f16d0501f2dc9169df8c56626a825f973 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 14 Apr 2025 11:28:13 -0400 Subject: [PATCH] WIP --- packages/svelte/src/internal/client/proxy.js | 16 +++++++++++++++- .../src/internal/client/reactivity/sources.js | 9 --------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/svelte/src/internal/client/proxy.js b/packages/svelte/src/internal/client/proxy.js index 6e82a9bd6b..3df5db751b 100644 --- a/packages/svelte/src/internal/client/proxy.js +++ b/packages/svelte/src/internal/client/proxy.js @@ -37,6 +37,8 @@ export function proxy(value, onchange) { return create_proxy(value, onchange ? [onchange] : []); } +let batching = false; + /** * @template T * @param {T} value @@ -165,6 +167,8 @@ export function create_proxy(value, onchanges) { if (prop === PROXY_ONCHANGE_SYMBOL) { return (/** @type {(Array<() => void>)} */ callbacks, /** @type {boolean} */ remove) => { + if (callbacks === onchanges) return; + if (remove) { let i = callbacks.length; while (i--) { @@ -258,6 +262,10 @@ export function create_proxy(value, onchanges) { var s = sources.get(prop); var has = prop in target; + if (is_proxied_array && prop === 'length') { + batching = true; + } + // if we are changing the length of the array we batch all the changes // to the sources and the original value by calling batch_onchange and immediately // invoking it...otherwise we just invoke an identity function @@ -312,7 +320,13 @@ export function create_proxy(value, onchanges) { } })(); - run_all(onchanges); + if (is_proxied_array && prop === 'length') { + batching = false; + } + + if (!batching) { + run_all(onchanges); + } var descriptor = Reflect.getOwnPropertyDescriptor(target, prop); diff --git a/packages/svelte/src/internal/client/reactivity/sources.js b/packages/svelte/src/internal/client/reactivity/sources.js index 13eb445a0f..e1d3b61275 100644 --- a/packages/svelte/src/internal/client/reactivity/sources.js +++ b/packages/svelte/src/internal/client/reactivity/sources.js @@ -252,15 +252,6 @@ export function internal_set(source, value) { inspect_effects.clear(); } - - var onchange = source.o; - if (onchange) { - if (onchange_batch) { - onchange_batch.add(onchange); - } else { - onchange(); - } - } } return value;