Skip to content

Commit 96e5edc

Browse files
committed
WIP
1 parent 36bfef9 commit 96e5edc

File tree

1 file changed

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

1 file changed

+22
-36
lines changed

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

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,35 @@ function identity(fn) {
2828
return fn;
2929
}
3030

31-
/**
32-
* @param {ValueOptions | undefined} options
33-
* @returns {ValueOptions | undefined}
34-
*/
35-
function clone_options(options) {
36-
return options != null
37-
? {
38-
onchange: options.onchange
39-
}
40-
: undefined;
41-
}
42-
4331
/** @type {ProxyMetadata | null} */
4432
var parent_metadata = null;
4533

4634
/**
4735
* @template T
4836
* @param {T} value
49-
* @param {ValueOptions} [_options]
37+
* @param {ValueOptions} [options]
5038
* @param {Source<T>} [prev] dev mode only
5139
* @returns {T}
5240
*/
53-
export function proxy(value, _options, prev) {
41+
export function proxy(value, options, prev) {
5442
// if non-proxyable, or is already a proxy, return `value`
5543
if (typeof value !== 'object' || value === null) {
5644
return value;
5745
}
5846

59-
var options = clone_options(_options);
47+
var onchange = options?.onchange;
6048

6149
if (STATE_SYMBOL in value) {
6250
// @ts-ignore
63-
value[PROXY_ONCHANGE_SYMBOL](options?.onchange);
51+
value[PROXY_ONCHANGE_SYMBOL](onchange);
6452
return value;
6553
}
6654

67-
if (options?.onchange) {
55+
if (onchange) {
6856
// if there's an onchange we actually store that but override the value
6957
// to store every other onchange that new proxies might add
70-
var onchanges = new Set([options.onchange]);
71-
options.onchange = () => {
58+
var onchanges = new Set([onchange]);
59+
onchange = () => {
7260
for (let onchange of onchanges) {
7361
onchange();
7462
}
@@ -118,7 +106,7 @@ export function proxy(value, _options, prev) {
118106
// mutations to the array are properly synced with our proxy
119107
sources.set(
120108
'length',
121-
source(/** @type {any[]} */ (value).length, clone_options(options), stack)
109+
source(/** @type {any[]} */ (value).length, onchange && { onchange }, stack)
122110
);
123111
}
124112

@@ -165,7 +153,7 @@ export function proxy(value, _options, prev) {
165153
var s = sources.get(prop);
166154

167155
if (s === undefined) {
168-
s = with_parent(() => source(descriptor.value, clone_options(options), stack));
156+
s = with_parent(() => source(descriptor.value, onchange && { onchange }, stack));
169157
sources.set(prop, s);
170158
} else {
171159
set(
@@ -184,7 +172,7 @@ export function proxy(value, _options, prev) {
184172
if (prop in target) {
185173
sources.set(
186174
prop,
187-
with_parent(() => source(UNINITIALIZED, clone_options(options), stack))
175+
with_parent(() => source(UNINITIALIZED, onchange && { onchange }, stack))
188176
);
189177
}
190178
} else {
@@ -201,7 +189,7 @@ export function proxy(value, _options, prev) {
201189
// when we delete a property if the source is a proxy we remove the current onchange from
202190
// the proxy `onchanges` so that it doesn't trigger it anymore
203191
if (typeof s.v === 'object' && s.v !== null && STATE_SYMBOL in s.v) {
204-
s.v[PROXY_ONCHANGE_SYMBOL](options?.onchange, true);
192+
s.v[PROXY_ONCHANGE_SYMBOL](onchange, true);
205193
}
206194
set(s, UNINITIALIZED);
207195
update_version(version);
@@ -227,14 +215,12 @@ export function proxy(value, _options, prev) {
227215
// we either add or remove the passed in value
228216
// to the onchanges array or we set every source onchange
229217
// to the passed in value (if it's undefined it will make the chain stop)
230-
if (options?.onchange != null && value && !remove) {
218+
if (onchange != null && value && !remove) {
231219
onchanges?.add?.(value);
232-
} else if (options?.onchange != null && value) {
220+
} else if (onchange != null && value) {
233221
onchanges?.delete?.(value);
234222
} else {
235-
options = {
236-
onchange: value
237-
};
223+
onchange = value;
238224
for (let [, s] of sources) {
239225
if (s.o) {
240226
s.o.onchange = value;
@@ -249,7 +235,7 @@ export function proxy(value, _options, prev) {
249235

250236
// create a source, but only if it's an own property and not a prototype property
251237
if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
252-
let opt = clone_options(options);
238+
let opt = onchange && { onchange };
253239
s = with_parent(() =>
254240
source(proxy(exists ? target[prop] : UNINITIALIZED, opt), opt, stack)
255241
);
@@ -281,7 +267,7 @@ export function proxy(value, _options, prev) {
281267

282268
if (
283269
is_proxied_array &&
284-
options?.onchange != null &&
270+
onchange != null &&
285271
array_methods.includes(/** @type {string} */ (prop))
286272
) {
287273
return batch_onchange(v);
@@ -330,7 +316,7 @@ export function proxy(value, _options, prev) {
330316
(active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
331317
) {
332318
if (s === undefined) {
333-
let opt = clone_options(options);
319+
let opt = onchange && { onchange };
334320
s = with_parent(() => source(has ? proxy(target[prop], opt) : UNINITIALIZED, opt, stack));
335321
sources.set(prop, s);
336322
}
@@ -362,14 +348,14 @@ export function proxy(value, _options, prev) {
362348
other_s.v !== null &&
363349
STATE_SYMBOL in other_s.v
364350
) {
365-
other_s.v[PROXY_ONCHANGE_SYMBOL](options?.onchange, true);
351+
other_s.v[PROXY_ONCHANGE_SYMBOL](onchange, true);
366352
}
367353
set(other_s, UNINITIALIZED);
368354
} else if (i in target) {
369355
// If the item exists in the original, we need to create a uninitialized source,
370356
// else a later read of the property would result in a source being created with
371357
// the value of the original item at that index.
372-
other_s = with_parent(() => source(UNINITIALIZED, clone_options(options), stack));
358+
other_s = with_parent(() => source(UNINITIALIZED, onchange && { onchange }, stack));
373359
sources.set(i + '', other_s);
374360
}
375361
}
@@ -381,7 +367,7 @@ export function proxy(value, _options, prev) {
381367
// object property before writing to that property.
382368
if (s === undefined) {
383369
if (!has || get_descriptor(target, prop)?.writable) {
384-
const opt = clone_options(options);
370+
const opt = onchange && { onchange };
385371
s = with_parent(() => source(undefined, opt, stack));
386372
set(
387373
s,
@@ -394,11 +380,11 @@ export function proxy(value, _options, prev) {
394380
// when we set a property if the source is a proxy we remove the current onchange from
395381
// the proxy `onchanges` so that it doesn't trigger it anymore
396382
if (typeof s.v === 'object' && s.v !== null && STATE_SYMBOL in s.v) {
397-
s.v[PROXY_ONCHANGE_SYMBOL](options?.onchange, true);
383+
s.v[PROXY_ONCHANGE_SYMBOL](onchange, true);
398384
}
399385
set(
400386
s,
401-
with_parent(() => proxy(value, clone_options(options)))
387+
with_parent(() => proxy(value, onchange && { onchange }))
402388
);
403389
}
404390
})();

0 commit comments

Comments
 (0)