From 4b9c4e1f16e7f2bfe2372170ffb80013031ccdf8 Mon Sep 17 00:00:00 2001 From: rixo Date: Sun, 7 Jan 2024 04:51:39 +0100 Subject: [PATCH] fix accessors in HMR --- packages/svelte/src/internal/client/hmr.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/svelte/src/internal/client/hmr.js b/packages/svelte/src/internal/client/hmr.js index 3c73b168dc..c922f37675 100644 --- a/packages/svelte/src/internal/client/hmr.js +++ b/packages/svelte/src/internal/client/hmr.js @@ -3,7 +3,7 @@ import { source, set, get } from './runtime.js'; /** * @template {any[]} ComponentArgs - * @template {Record | undefined} ComponentReturn + * @template {Record | undefined} ComponentReturn * @template {(...args: ComponentArgs) => ComponentReturn} Component * * @param {{ @@ -20,8 +20,7 @@ export function hmr(hot_data, component) { // @ts-ignore hot_data.proxy = function (target, ...args) { - /** @type {ComponentReturn} */ - let current_accessors; + const accessors = source(/** @type {ComponentReturn} */ ({})); key( target, @@ -29,7 +28,8 @@ export function hmr(hot_data, component) { ($$anchor) => { const current_component = get(component_signal); // @ts-ignore - current_accessors = current_component($$anchor, ...args); + const new_accessors = current_component($$anchor, ...args); + set(accessors, new_accessors); } ); @@ -37,14 +37,11 @@ export function hmr(hot_data, component) { {}, { get(_, p) { - // we actually want to crash if no accessors, because no HMR code would crash - // @ts-ignore - return current_accessors[p]; + return get(accessors)?.[p]; }, set(_, p, value) { - // we actually want to crash if no accessors, because no HMR code would crash - // @ts-ignore - current_accessors[p] = value; + // @ts-ignore (we actually want to crash on undefined, like non HMR code would do) + get(accessors)[p] = value; return true; } }