Skip to content

Commit 1aa61c1

Browse files
committed
fix HMR accessors again
1 parent ef6bde6 commit 1aa61c1

File tree

1 file changed

+22
-16
lines changed
  • packages/svelte/src/internal/client

1 file changed

+22
-16
lines changed

packages/svelte/src/internal/client/hmr.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { key, comment } from './render.js';
1+
import { key } from './render.js';
22
import { source, set, get } from './runtime.js';
33
import { current_hydration_fragment } from './hydration.js';
44
import { child_frag } from './operations.js';
5+
import { proxy } from './proxy/proxy.js';
56

67
function find_surrounding_ssr_commments() {
78
if (!current_hydration_fragment?.[0]) return null;
@@ -61,8 +62,6 @@ export function hmr(hot_data, new_component) {
6162

6263
// @ts-ignore
6364
hot_data.proxy = function ($$anchor, ...args) {
64-
let accessors = /** @type {ComponentReturn} */ ({});
65-
6665
// During hydration the root component will receive a null $$anchor. The
6766
// following is a hack to get our `key` a node to render to, all while
6867
// avoiding it to "consume" the SSR marker.
@@ -77,29 +76,36 @@ export function hmr(hot_data, new_component) {
7776
}
7877
}
7978

79+
const accessors_proxy = proxy(/** @type {import('./proxy/proxy.js').StateObject} */ ({}));
80+
/** @type {Set<string>} */
81+
const accessors_keys = new Set();
82+
8083
key(
8184
$$anchor,
8285
() => get(component_signal),
8386
($$anchor) => {
8487
const component = get(component_signal);
8588
// @ts-ignore
86-
accessors = component($$anchor, ...args);
87-
}
88-
);
89+
const new_accessors = component($$anchor, ...args);
8990

90-
return new Proxy(
91-
{},
92-
{
93-
get(_, p) {
94-
return accessors?.[p];
95-
},
96-
set(_, p, value) {
97-
// @ts-ignore (we actually want to crash on undefined, like non HMR code would do)
98-
accessors[p] = value;
99-
return true;
91+
const removed_keys = new Set(accessors_keys);
92+
93+
if (new_accessors) {
94+
for (const [key, value] of Object.entries(new_accessors)) {
95+
accessors_proxy[key] = value;
96+
accessors_keys.add(key);
97+
removed_keys.delete(key);
98+
}
99+
}
100+
101+
for (const key of removed_keys) {
102+
accessors_keys.delete(key);
103+
accessors_proxy[key] = undefined;
100104
}
101105
}
102106
);
107+
108+
return accessors_proxy;
103109
};
104110
}
105111

0 commit comments

Comments
 (0)