From 0ca43b0b736238a28d8db289a6762a1a87f9e763 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Wed, 27 Mar 2024 01:14:14 +0000 Subject: [PATCH] revise revise-this --- .../client/dom/elements/bindings/this.js | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/packages/svelte/src/internal/client/dom/elements/bindings/this.js b/packages/svelte/src/internal/client/dom/elements/bindings/this.js index 545ca60d2..b959df61c 100644 --- a/packages/svelte/src/internal/client/dom/elements/bindings/this.js +++ b/packages/svelte/src/internal/client/dom/elements/bindings/this.js @@ -22,38 +22,40 @@ function is_bound_this(bound_value, element_or_component) { * @returns {void} */ export function bind_this(element_or_component, update, get_value, get_parts) { - /** @type {unknown[]} */ - var old_parts; + effect(() => { + /** @type {unknown[]} */ + var old_parts; - /** @type {unknown[]} */ - var parts; + /** @type {unknown[]} */ + var parts; - render_effect(() => { - old_parts = parts; - // We only track changes to the parts, not the value itself to avoid unnecessary reruns. - parts = get_parts?.() || []; + render_effect(() => { + old_parts = parts; + // We only track changes to the parts, not the value itself to avoid unnecessary reruns. + parts = get_parts?.() || []; - untrack(() => { - if (element_or_component !== get_value(...parts)) { - update(element_or_component, ...parts); - // If this is an effect rerun (cause: each block context changes), then nullfiy the binding at - // the previous position if it isn't already taken over by a different effect. - if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) { - update(null, ...old_parts); + untrack(() => { + if (element_or_component !== get_value(...parts)) { + update(element_or_component, ...parts); + // If this is an effect rerun (cause: each block context changes), then nullfiy the binding at + // the previous position if it isn't already taken over by a different effect. + if (old_parts && is_bound_this(get_value(...old_parts), element_or_component)) { + update(null, ...old_parts); + } } - } + }); }); - }); - // Defer to the next tick so that all updates can be reconciled first. - // This solves the case where one variable is shared across multiple this-bindings. - effect(() => { - return () => { - effect(() => { - if (parts && is_bound_this(get_value(...parts), element_or_component)) { - update(null, ...parts); - } - }); - }; + // Defer to the next tick so that all updates can be reconciled first. + // This solves the case where one variable is shared across multiple this-bindings. + effect(() => { + return () => { + effect(() => { + if (parts && is_bound_this(get_value(...parts), element_or_component)) { + update(null, ...parts); + } + }); + }; + }); }); }