From ce7eaf07d3135167f61913d93006ea536ef01b61 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 26 Mar 2024 15:41:08 -0400 Subject: [PATCH] remove ondestroy --- .../client/dom/elements/bindings/this.js | 59 +++++++++---------- .../src/internal/client/reactivity/effects.js | 10 +--- .../src/internal/client/reactivity/types.d.ts | 2 - 3 files changed, 32 insertions(+), 39 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 c91fac434a..dfc0261e19 100644 --- a/packages/svelte/src/internal/client/dom/elements/bindings/this.js +++ b/packages/svelte/src/internal/client/dom/elements/bindings/this.js @@ -1,5 +1,5 @@ import { STATE_SYMBOL } from '../../../constants.js'; -import { effect } from '../../../reactivity/effects.js'; +import { effect, render_effect } from '../../../reactivity/effects.js'; import { untrack } from '../../../runtime.js'; /** @@ -22,39 +22,38 @@ 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; - var e = 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); + } } - } + }); }); - }); - // Add effect teardown (likely causes: if block became false, each item removed, component unmounted). - // In these cases we need to nullify the binding only if we detect that the value is still the same. - // If not, that means that another effect has now taken over the binding. - e.ondestroy = () => { - // 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(() => { - if (parts && is_bound_this(get_value(...parts), element_or_component)) { - update(null, ...parts); - } - }); - }; + return () => { + // 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(() => { + if (parts && is_bound_this(get_value(...parts), element_or_component)) { + update(null, ...parts); + } + }); + }; + }); } diff --git a/packages/svelte/src/internal/client/reactivity/effects.js b/packages/svelte/src/internal/client/reactivity/effects.js index c6bcdc22ed..7cedb5327d 100644 --- a/packages/svelte/src/internal/client/reactivity/effects.js +++ b/packages/svelte/src/internal/client/reactivity/effects.js @@ -51,7 +51,6 @@ function create_effect(type, fn, sync, init = true) { deriveds: null, teardown: null, ctx: current_component_context, - ondestroy: null, transitions: null }; @@ -253,16 +252,13 @@ export function destroy_effect(effect) { remove(effect.dom); } - effect.ondestroy?.(); - - // @ts-expect-error - effect.fn = - effect.effects = + effect.effects = effect.teardown = - effect.ondestroy = effect.ctx = effect.dom = effect.deps = + // @ts-expect-error + effect.fn = null; } diff --git a/packages/svelte/src/internal/client/reactivity/types.d.ts b/packages/svelte/src/internal/client/reactivity/types.d.ts index a28b0fefbc..2b2563dbf3 100644 --- a/packages/svelte/src/internal/client/reactivity/types.d.ts +++ b/packages/svelte/src/internal/client/reactivity/types.d.ts @@ -40,8 +40,6 @@ export interface Effect extends Reaction { dom: Dom | null; /** The associated component context */ ctx: null | ComponentContext; - /** Stuff to do when the effect is destroyed */ - ondestroy: null | (() => void); /** The effect function */ fn: () => void | (() => void); /** The teardown function returned from the effect function */