|
|
@ -22,13 +22,13 @@ import { UNINITIALIZED } from '../../constants.js';
|
|
|
|
* @param {T} value
|
|
|
|
* @param {T} value
|
|
|
|
* @param {boolean} [immutable]
|
|
|
|
* @param {boolean} [immutable]
|
|
|
|
* @param {Set<Function> | null} [owners]
|
|
|
|
* @param {Set<Function> | null} [owners]
|
|
|
|
* @returns {import('./types.js').ProxyStateObject<T> | T}
|
|
|
|
* @returns {import('#client').ProxyStateObject<T> | T}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function proxy(value, immutable = true, owners) {
|
|
|
|
export function proxy(value, immutable = true, owners) {
|
|
|
|
if (typeof value === 'object' && value != null && !is_frozen(value)) {
|
|
|
|
if (typeof value === 'object' && value != null && !is_frozen(value)) {
|
|
|
|
// If we have an existing proxy, return it...
|
|
|
|
// If we have an existing proxy, return it...
|
|
|
|
if (STATE_SYMBOL in value) {
|
|
|
|
if (STATE_SYMBOL in value) {
|
|
|
|
const metadata = /** @type {import('./types.js').ProxyMetadata<T>} */ (value[STATE_SYMBOL]);
|
|
|
|
const metadata = /** @type {import('#client').ProxyMetadata<T>} */ (value[STATE_SYMBOL]);
|
|
|
|
// ...unless the proxy belonged to a different object, because
|
|
|
|
// ...unless the proxy belonged to a different object, because
|
|
|
|
// someone copied the state symbol using `Reflect.ownKeys(...)`
|
|
|
|
// someone copied the state symbol using `Reflect.ownKeys(...)`
|
|
|
|
if (metadata.t === value || metadata.p === value) {
|
|
|
|
if (metadata.t === value || metadata.p === value) {
|
|
|
@ -53,7 +53,7 @@ export function proxy(value, immutable = true, owners) {
|
|
|
|
const proxy = new Proxy(value, state_proxy_handler);
|
|
|
|
const proxy = new Proxy(value, state_proxy_handler);
|
|
|
|
|
|
|
|
|
|
|
|
define_property(value, STATE_SYMBOL, {
|
|
|
|
define_property(value, STATE_SYMBOL, {
|
|
|
|
value: /** @type {import('./types.js').ProxyMetadata} */ ({
|
|
|
|
value: /** @type {import('#client').ProxyMetadata} */ ({
|
|
|
|
s: new Map(),
|
|
|
|
s: new Map(),
|
|
|
|
v: source(0),
|
|
|
|
v: source(0),
|
|
|
|
a: is_array(value),
|
|
|
|
a: is_array(value),
|
|
|
@ -86,7 +86,7 @@ export function proxy(value, immutable = true, owners) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @template {import('./types.js').ProxyStateObject} T
|
|
|
|
* @template {import('#client').ProxyStateObject} T
|
|
|
|
* @param {T} value
|
|
|
|
* @param {T} value
|
|
|
|
* @param {Map<T, Record<string | symbol, any>>} already_unwrapped
|
|
|
|
* @param {Map<T, Record<string | symbol, any>>} already_unwrapped
|
|
|
|
* @returns {Record<string | symbol, any>}
|
|
|
|
* @returns {Record<string | symbol, any>}
|
|
|
@ -138,23 +138,23 @@ function unwrap(value, already_unwrapped) {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function unstate(value) {
|
|
|
|
export function unstate(value) {
|
|
|
|
return /** @type {T} */ (
|
|
|
|
return /** @type {T} */ (
|
|
|
|
unwrap(/** @type {import('./types.js').ProxyStateObject} */ (value), new Map())
|
|
|
|
unwrap(/** @type {import('#client').ProxyStateObject} */ (value), new Map())
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {import('./types.js').Source<number>} signal
|
|
|
|
* @param {import('#client').Source<number>} signal
|
|
|
|
* @param {1 | -1} [d]
|
|
|
|
* @param {1 | -1} [d]
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
function update_version(signal, d = 1) {
|
|
|
|
function update_version(signal, d = 1) {
|
|
|
|
set(signal, signal.v + d);
|
|
|
|
set(signal, signal.v + d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {ProxyHandler<import('./types.js').ProxyStateObject<any>>} */
|
|
|
|
/** @type {ProxyHandler<import('#client').ProxyStateObject<any>>} */
|
|
|
|
const state_proxy_handler = {
|
|
|
|
const state_proxy_handler = {
|
|
|
|
defineProperty(target, prop, descriptor) {
|
|
|
|
defineProperty(target, prop, descriptor) {
|
|
|
|
if (descriptor.value) {
|
|
|
|
if (descriptor.value) {
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
|
|
|
|
|
|
|
|
const s = metadata.s.get(prop);
|
|
|
|
const s = metadata.s.get(prop);
|
|
|
@ -165,7 +165,7 @@ const state_proxy_handler = {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
deleteProperty(target, prop) {
|
|
|
|
deleteProperty(target, prop) {
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const s = metadata.s.get(prop);
|
|
|
|
const s = metadata.s.get(prop);
|
|
|
|
const is_array = metadata.a;
|
|
|
|
const is_array = metadata.a;
|
|
|
@ -198,7 +198,7 @@ const state_proxy_handler = {
|
|
|
|
return Reflect.get(target, STATE_SYMBOL);
|
|
|
|
return Reflect.get(target, STATE_SYMBOL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
let s = metadata.s.get(prop);
|
|
|
|
let s = metadata.s.get(prop);
|
|
|
|
|
|
|
|
|
|
|
@ -229,7 +229,7 @@ const state_proxy_handler = {
|
|
|
|
getOwnPropertyDescriptor(target, prop) {
|
|
|
|
getOwnPropertyDescriptor(target, prop) {
|
|
|
|
const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
|
|
const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
|
|
if (descriptor && 'value' in descriptor) {
|
|
|
|
if (descriptor && 'value' in descriptor) {
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const s = metadata.s.get(prop);
|
|
|
|
const s = metadata.s.get(prop);
|
|
|
|
|
|
|
|
|
|
|
@ -245,7 +245,7 @@ const state_proxy_handler = {
|
|
|
|
if (prop === STATE_SYMBOL) {
|
|
|
|
if (prop === STATE_SYMBOL) {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const has = Reflect.has(target, prop);
|
|
|
|
const has = Reflect.has(target, prop);
|
|
|
|
|
|
|
|
|
|
|
@ -266,7 +266,7 @@ const state_proxy_handler = {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
set(target, prop, value, receiver) {
|
|
|
|
set(target, prop, value, receiver) {
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
let s = metadata.s.get(prop);
|
|
|
|
let s = metadata.s.get(prop);
|
|
|
|
// If we haven't yet created a source for this property, we need to ensure
|
|
|
|
// If we haven't yet created a source for this property, we need to ensure
|
|
|
@ -329,7 +329,7 @@ const state_proxy_handler = {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
ownKeys(target) {
|
|
|
|
ownKeys(target) {
|
|
|
|
/** @type {import('./types.js').ProxyMetadata} */
|
|
|
|
/** @type {import('#client').ProxyMetadata} */
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
const metadata = target[STATE_SYMBOL];
|
|
|
|
|
|
|
|
|
|
|
|
get(metadata.v);
|
|
|
|
get(metadata.v);
|
|
|
|