Skip to content

Commit 8ebcd91

Browse files
committed
rewrite logic
1 parent d28fe7e commit 8ebcd91

File tree

1 file changed

+21
-10
lines changed
  • packages/svelte/src/internal/client

1 file changed

+21
-10
lines changed

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

+21-10
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@ import * as w from './warnings.js';
1616
import { get_stack } from './dev/tracing.js';
1717
import { tracing_mode_flag } from '../flags/index.js';
1818

19+
/**
20+
* @param {unknown} value
21+
* @returns {boolean}
22+
*/
23+
function should_proxy(value) {
24+
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
25+
return false;
26+
}
27+
const prototype = get_prototype_of(value);
28+
if (prototype !== object_prototype && prototype !== array_prototype) {
29+
return false;
30+
}
31+
return true;
32+
}
33+
1934
/**
2035
* @template T
2136
* @param {T} value
2237
* @returns {T}
2338
*/
2439
export function proxy(value) {
2540
// if non-proxyable, or is already a proxy, return `value`
26-
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
27-
return value;
28-
}
29-
30-
const prototype = get_prototype_of(value);
31-
32-
if (prototype !== object_prototype && prototype !== array_prototype) {
41+
if (!should_proxy(value)) {
3342
return value;
3443
}
3544

@@ -289,10 +298,12 @@ export function proxy(value) {
289298
* @returns {T | void}
290299
*/
291300
export function return_proxy(value) {
292-
const res = proxy(value);
293-
if (res !== value || (typeof value === 'object' && value !== null && STATE_SYMBOL in value)) {
301+
if (
302+
!should_proxy(value) &&
303+
!(typeof value === 'object' && value !== null && STATE_SYMBOL in value)
304+
) {
294305
// if the argument passed was already a proxy, we don't warn
295-
return res;
306+
return proxy(value);
296307
}
297308
w.state_return_not_proxyable();
298309
}

0 commit comments

Comments
 (0)