diff --git a/packages/svelte/src/internal/client/proxy/proxy.js b/packages/svelte/src/internal/client/proxy/proxy.js index a9a92457cf..cd17049628 100644 --- a/packages/svelte/src/internal/client/proxy/proxy.js +++ b/packages/svelte/src/internal/client/proxy/proxy.js @@ -21,7 +21,6 @@ import { READONLY_SYMBOL } from './readonly.js'; /** @typedef {Record & { [STATE_SYMBOL]: Metadata }} StateObject */ export const STATE_SYMBOL = Symbol('$state'); -export const UNSTATE_SYMBOL = Symbol('unstate'); const object_prototype = Object.prototype; const array_prototype = Array.prototype; @@ -56,43 +55,35 @@ export function proxy(value) { * @returns {Record} */ function unwrap(value, already_unwrapped = new Map()) { - if (typeof value === 'object' && value != null && !is_frozen(value)) { + if (typeof value === 'object' && value != null && !is_frozen(value) && STATE_SYMBOL in value) { const unwrapped = already_unwrapped.get(value); if (unwrapped !== undefined) { return unwrapped; } - if (STATE_SYMBOL in value) { - if (is_array(value)) { - /** @type {Record} */ - const array = []; - already_unwrapped.set(value, array); - for (const element of value) { - array.push(unwrap(element, already_unwrapped)); - } - return array; - } else { - /** @type {Record} */ - const obj = {}; - const keys = object_keys(value); - const descriptors = get_descriptors(value); - already_unwrapped.set(value, obj); - for (const key of keys) { - if (descriptors[key].get) { - define_property(obj, key, descriptors[key]); - } else { - /** @type {T} */ - const property = value[key]; - obj[key] = unwrap(property, already_unwrapped); - } + if (is_array(value)) { + /** @type {Record} */ + const array = []; + already_unwrapped.set(value, array); + for (const element of value) { + array.push(unwrap(element, already_unwrapped)); + } + return array; + } else { + /** @type {Record} */ + const obj = {}; + const keys = object_keys(value); + const descriptors = get_descriptors(value); + already_unwrapped.set(value, obj); + for (const key of keys) { + if (descriptors[key].get) { + define_property(obj, key, descriptors[key]); + } else { + /** @type {T} */ + const property = value[key]; + obj[key] = unwrap(property, already_unwrapped); } - return obj; } - } - const unstate_fn = /** @type {undefined | ((this: StateObject) => T)} */ ( - value[UNSTATE_SYMBOL] - ); - if (typeof unstate_fn === 'function') { - return unstate_fn.call(value); + return obj; } } return value; diff --git a/packages/svelte/src/internal/index.js b/packages/svelte/src/internal/index.js index 000def896e..4d3205b824 100644 --- a/packages/svelte/src/internal/index.js +++ b/packages/svelte/src/internal/index.js @@ -45,7 +45,7 @@ export * from './client/each.js'; export * from './client/render.js'; export * from './client/validate.js'; export { raf } from './client/timing.js'; -export { proxy, readonly, unstate, UNSTATE_SYMBOL } from './client/proxy/proxy.js'; +export { proxy, readonly, unstate } from './client/proxy/proxy.js'; export { create_custom_element } from './client/custom-element.js'; diff --git a/packages/svelte/src/main/main-client.js b/packages/svelte/src/main/main-client.js index 7a659e33e5..f53173bb66 100644 --- a/packages/svelte/src/main/main-client.js +++ b/packages/svelte/src/main/main-client.js @@ -262,6 +262,5 @@ export { tick, untrack, unstate, - onDestroy, - UNSTATE_SYMBOL + onDestroy } from '../internal/index.js'; diff --git a/packages/svelte/tests/runtime-runes/samples/unstate-class/_config.js b/packages/svelte/tests/runtime-runes/samples/unstate-class/_config.js deleted file mode 100644 index b4cc25a1eb..0000000000 --- a/packages/svelte/tests/runtime-runes/samples/unstate-class/_config.js +++ /dev/null @@ -1,12 +0,0 @@ -import { test } from '../../test'; - -export default test({ - html: ``, - - async test({ assert, target }) { - const btn = target.querySelector('button'); - - await btn?.click(); - assert.htmlEqual(target.innerHTML, ``); - } -}); diff --git a/packages/svelte/tests/runtime-runes/samples/unstate-class/main.svelte b/packages/svelte/tests/runtime-runes/samples/unstate-class/main.svelte deleted file mode 100644 index 2251121f61..0000000000 --- a/packages/svelte/tests/runtime-runes/samples/unstate-class/main.svelte +++ /dev/null @@ -1,22 +0,0 @@ - - - diff --git a/sites/svelte-5-preview/src/routes/docs/content/01-api/05-functions.md b/sites/svelte-5-preview/src/routes/docs/content/01-api/05-functions.md index 2e50414044..26410e1774 100644 --- a/sites/svelte-5-preview/src/routes/docs/content/01-api/05-functions.md +++ b/sites/svelte-5-preview/src/routes/docs/content/01-api/05-functions.md @@ -42,31 +42,4 @@ To remove reactivity from objects and arrays created with `$state`, use `unstate This is handy when you want to pass some state to an external library or API that doesn't expect a reactive object – such as `structuredClone`. -For classes that might contain `$state`, the `UNSTATE_SYMBOL` can be imported and applied as a class method to enable deep unwrapped of reactive state -through class components: - -```svelte - -``` - > Note that `unstate` will return a new object from the input when removing reactivity. If the object passed isn't reactive, it will be returned as is.