10000 feat: allow `$state` in return statements by Ocean-OS · Pull Request #15589 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

feat: allow $state in return statements #15589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
rewrite logic
  • Loading branch information
Ocean-OS committed Apr 30, 2025
commit 8ebcd913ec831562d2d343eeaf3c4ed84a66c7d7
31 changes: 21 additions & 10 deletions packages/svelte/src/internal/client/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@ import * as w from './warnings.js';
import { get_stack } from './dev/tracing.js';
import { tracing_mode_flag } from '../flags/index.js';

/**
* @param {unknown} value
* @returns {boolean}
*/
function should_proxy(value) { BC1E
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
return false;
}
const prototype = get_prototype_of(value);
if (prototype !== object_prototype && prototype !== array_prototype) {
return false;
}
return true;
}

/**
* @template T
* @param {T} value
* @returns {T}
*/
export function proxy(value) {
// if non-proxyable, or is already a proxy, return `value`
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
return value;
}

const prototype = get_prototype_of(value);

if (prototype !== object_prototype && prototype !== array_prototype) {
if (!should_proxy(value)) {
return value;
}

Expand Down Expand Up @@ -289,10 +298,12 @@ export function proxy(value) {
* @returns {T | void}
*/
export function return_proxy(value) {
const res = proxy(value);
if (res !== value || (typeof value === 'object' && value !== null && STATE_SYMBOL in value)) {
if (
!should_proxy(value) &&
!(typeof value === 'object' && value !== null && STATE_SYMBOL in value)
) {
// if the argument passed was already a proxy, we don't warn
return res;
return proxy(value);
}
w.state_return_not_proxyable();
}
Expand Down
Loading
0