@zag-js/store 1.34.1 → 1.35.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clone.d.mts +3 -0
- package/dist/clone.d.ts +3 -0
- package/dist/clone.js +77 -0
- package/dist/clone.mjs +52 -0
- package/dist/global.d.mts +4 -0
- package/dist/global.d.ts +4 -0
- package/dist/global.js +44 -0
- package/dist/global.mjs +18 -0
- package/dist/index.d.mts +4 -24
- package/dist/index.d.ts +4 -24
- package/dist/index.js +43 -311
- package/dist/index.mjs +14 -305
- package/dist/proxy-computed.d.mts +10 -0
- package/dist/proxy-computed.d.ts +10 -0
- package/dist/proxy-computed.js +48 -0
- package/dist/proxy-computed.mjs +23 -0
- package/dist/proxy.d.mts +14 -0
- package/dist/proxy.d.ts +14 -0
- package/dist/proxy.js +254 -0
- package/dist/proxy.mjs +225 -0
- package/dist/utils.d.mts +5 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +41 -0
- package/dist/utils.mjs +14 -0
- package/package.json +1 -1
package/dist/clone.d.mts
ADDED
package/dist/clone.d.ts
ADDED
package/dist/clone.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/clone.ts
|
|
21
|
+
var clone_exports = {};
|
|
22
|
+
__export(clone_exports, {
|
|
23
|
+
clone: () => clone
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(clone_exports);
|
|
26
|
+
var import_utils = require("./utils.cjs");
|
|
27
|
+
function set(obj, key, val) {
|
|
28
|
+
if (typeof val.value === "object" && !(0, import_utils.canProxy)(val.value)) val.value = clone(val.value);
|
|
29
|
+
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
|
|
30
|
+
Object.defineProperty(obj, key, val);
|
|
31
|
+
} else obj[key] = val.value;
|
|
32
|
+
}
|
|
33
|
+
function clone(x) {
|
|
34
|
+
if (typeof x !== "object") return x;
|
|
35
|
+
let i = 0, k, list, tmp, str = Object.prototype.toString.call(x);
|
|
36
|
+
if (str === "[object Object]") {
|
|
37
|
+
tmp = Object.create(Object.getPrototypeOf(x) || null);
|
|
38
|
+
} else if (str === "[object Array]") {
|
|
39
|
+
tmp = Array(x.length);
|
|
40
|
+
} else if (str === "[object Set]") {
|
|
41
|
+
tmp = /* @__PURE__ */ new Set();
|
|
42
|
+
x.forEach(function(val) {
|
|
43
|
+
tmp.add(clone(val));
|
|
44
|
+
});
|
|
45
|
+
} else if (str === "[object Map]") {
|
|
46
|
+
tmp = /* @__PURE__ */ new Map();
|
|
47
|
+
x.forEach(function(val, key) {
|
|
48
|
+
tmp.set(clone(key), clone(val));
|
|
49
|
+
});
|
|
50
|
+
} else if (str === "[object Date]") {
|
|
51
|
+
tmp = /* @__PURE__ */ new Date(+x);
|
|
52
|
+
} else if (str === "[object RegExp]") {
|
|
53
|
+
tmp = new RegExp(x.source, x.flags);
|
|
54
|
+
} else if (str === "[object DataView]") {
|
|
55
|
+
tmp = new x.constructor(clone(x.buffer));
|
|
56
|
+
} else if (str === "[object ArrayBuffer]") {
|
|
57
|
+
tmp = x.slice(0);
|
|
58
|
+
} else if (str === "[object Blob]") {
|
|
59
|
+
tmp = x.slice();
|
|
60
|
+
} else if (str.slice(-6) === "Array]") {
|
|
61
|
+
tmp = new x.constructor(x);
|
|
62
|
+
}
|
|
63
|
+
if (tmp) {
|
|
64
|
+
for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
|
|
65
|
+
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
|
|
66
|
+
}
|
|
67
|
+
for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
|
|
68
|
+
if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
|
|
69
|
+
set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return tmp || x;
|
|
73
|
+
}
|
|
74
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
75
|
+
0 && (module.exports = {
|
|
76
|
+
clone
|
|
77
|
+
});
|
package/dist/clone.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// src/clone.ts
|
|
2
|
+
import { canProxy } from "./utils.mjs";
|
|
3
|
+
function set(obj, key, val) {
|
|
4
|
+
if (typeof val.value === "object" && !canProxy(val.value)) val.value = clone(val.value);
|
|
5
|
+
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
|
|
6
|
+
Object.defineProperty(obj, key, val);
|
|
7
|
+
} else obj[key] = val.value;
|
|
8
|
+
}
|
|
9
|
+
function clone(x) {
|
|
10
|
+
if (typeof x !== "object") return x;
|
|
11
|
+
let i = 0, k, list, tmp, str = Object.prototype.toString.call(x);
|
|
12
|
+
if (str === "[object Object]") {
|
|
13
|
+
tmp = Object.create(Object.getPrototypeOf(x) || null);
|
|
14
|
+
} else if (str === "[object Array]") {
|
|
15
|
+
tmp = Array(x.length);
|
|
16
|
+
} else if (str === "[object Set]") {
|
|
17
|
+
tmp = /* @__PURE__ */ new Set();
|
|
18
|
+
x.forEach(function(val) {
|
|
19
|
+
tmp.add(clone(val));
|
|
20
|
+
});
|
|
21
|
+
} else if (str === "[object Map]") {
|
|
22
|
+
tmp = /* @__PURE__ */ new Map();
|
|
23
|
+
x.forEach(function(val, key) {
|
|
24
|
+
tmp.set(clone(key), clone(val));
|
|
25
|
+
});
|
|
26
|
+
} else if (str === "[object Date]") {
|
|
27
|
+
tmp = /* @__PURE__ */ new Date(+x);
|
|
28
|
+
} else if (str === "[object RegExp]") {
|
|
29
|
+
tmp = new RegExp(x.source, x.flags);
|
|
30
|
+
} else if (str === "[object DataView]") {
|
|
31
|
+
tmp = new x.constructor(clone(x.buffer));
|
|
32
|
+
} else if (str === "[object ArrayBuffer]") {
|
|
33
|
+
tmp = x.slice(0);
|
|
34
|
+
} else if (str === "[object Blob]") {
|
|
35
|
+
tmp = x.slice();
|
|
36
|
+
} else if (str.slice(-6) === "Array]") {
|
|
37
|
+
tmp = new x.constructor(x);
|
|
38
|
+
}
|
|
39
|
+
if (tmp) {
|
|
40
|
+
for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
|
|
41
|
+
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
|
|
42
|
+
}
|
|
43
|
+
for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
|
|
44
|
+
if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
|
|
45
|
+
set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return tmp || x;
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
clone
|
|
52
|
+
};
|
package/dist/global.d.ts
ADDED
package/dist/global.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/global.ts
|
|
21
|
+
var global_exports = {};
|
|
22
|
+
__export(global_exports, {
|
|
23
|
+
globalRef: () => globalRef,
|
|
24
|
+
refSet: () => refSet
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(global_exports);
|
|
27
|
+
function glob() {
|
|
28
|
+
if (typeof globalThis !== "undefined") return globalThis;
|
|
29
|
+
if (typeof self !== "undefined") return self;
|
|
30
|
+
if (typeof window !== "undefined") return window;
|
|
31
|
+
if (typeof global !== "undefined") return global;
|
|
32
|
+
}
|
|
33
|
+
function globalRef(key, value) {
|
|
34
|
+
const g = glob();
|
|
35
|
+
if (!g) return value();
|
|
36
|
+
g[key] || (g[key] = value());
|
|
37
|
+
return g[key];
|
|
38
|
+
}
|
|
39
|
+
var refSet = globalRef("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {
|
|
42
|
+
globalRef,
|
|
43
|
+
refSet
|
|
44
|
+
});
|
package/dist/global.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/global.ts
|
|
2
|
+
function glob() {
|
|
3
|
+
if (typeof globalThis !== "undefined") return globalThis;
|
|
4
|
+
if (typeof self !== "undefined") return self;
|
|
5
|
+
if (typeof window !== "undefined") return window;
|
|
6
|
+
if (typeof global !== "undefined") return global;
|
|
7
|
+
}
|
|
8
|
+
function globalRef(key, value) {
|
|
9
|
+
const g = glob();
|
|
10
|
+
if (!g) return value();
|
|
11
|
+
g[key] || (g[key] = value());
|
|
12
|
+
return g[key];
|
|
13
|
+
}
|
|
14
|
+
var refSet = globalRef("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
|
|
15
|
+
export {
|
|
16
|
+
globalRef,
|
|
17
|
+
refSet
|
|
18
|
+
};
|
package/dist/index.d.mts
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
type AsRef = {
|
|
6
|
-
$$valtioRef: true;
|
|
7
|
-
};
|
|
8
|
-
type Path = (string | symbol)[];
|
|
9
|
-
type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown];
|
|
10
|
-
type Snapshot<T> = T;
|
|
11
|
-
declare function proxy<T extends object>(initialObject?: T): T;
|
|
12
|
-
declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void;
|
|
13
|
-
declare function snapshot<T extends object>(proxyObject: T): T;
|
|
14
|
-
declare function ref<T extends object>(obj: T): Ref<T>;
|
|
15
|
-
type Ref<T> = T & AsRef;
|
|
16
|
-
|
|
17
|
-
declare function proxyWithComputed<T extends object, U extends object>(initialObject: T, computedFns: {
|
|
18
|
-
[K in keyof U]: ((snap: Snapshot<T>) => U[K]) | {
|
|
19
|
-
get: (snap: Snapshot<T>) => U[K];
|
|
20
|
-
set?: ((state: T, newValue: U[K]) => void) | undefined;
|
|
21
|
-
};
|
|
22
|
-
}): T & U;
|
|
23
|
-
|
|
24
|
-
export { type Ref, type Snapshot, clone, globalRef, proxy, proxyWithComputed, ref, snapshot, subscribe };
|
|
1
|
+
export { clone } from './clone.mjs';
|
|
2
|
+
export { globalRef } from './global.mjs';
|
|
3
|
+
export { Ref, Snapshot, proxy, ref, snapshot, subscribe } from './proxy.mjs';
|
|
4
|
+
export { proxyWithComputed } from './proxy-computed.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
type AsRef = {
|
|
6
|
-
$$valtioRef: true;
|
|
7
|
-
};
|
|
8
|
-
type Path = (string | symbol)[];
|
|
9
|
-
type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown];
|
|
10
|
-
type Snapshot<T> = T;
|
|
11
|
-
declare function proxy<T extends object>(initialObject?: T): T;
|
|
12
|
-
declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void;
|
|
13
|
-
declare function snapshot<T extends object>(proxyObject: T): T;
|
|
14
|
-
declare function ref<T extends object>(obj: T): Ref<T>;
|
|
15
|
-
type Ref<T> = T & AsRef;
|
|
16
|
-
|
|
17
|
-
declare function proxyWithComputed<T extends object, U extends object>(initialObject: T, computedFns: {
|
|
18
|
-
[K in keyof U]: ((snap: Snapshot<T>) => U[K]) | {
|
|
19
|
-
get: (snap: Snapshot<T>) => U[K];
|
|
20
|
-
set?: ((state: T, newValue: U[K]) => void) | undefined;
|
|
21
|
-
};
|
|
22
|
-
}): T & U;
|
|
23
|
-
|
|
24
|
-
export { type Ref, type Snapshot, clone, globalRef, proxy, proxyWithComputed, ref, snapshot, subscribe };
|
|
1
|
+
export { clone } from './clone.js';
|
|
2
|
+
export { globalRef } from './global.js';
|
|
3
|
+
export { Ref, Snapshot, proxy, ref, snapshot, subscribe } from './proxy.js';
|
|
4
|
+
export { proxyWithComputed } from './proxy-computed.js';
|
package/dist/index.js
CHANGED
|
@@ -1,313 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
g[key] || (g[key] = value());
|
|
16
|
-
return g[key];
|
|
17
|
-
}
|
|
18
|
-
var refSet = globalRef("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
|
|
19
|
-
|
|
20
|
-
// src/utils.ts
|
|
21
|
-
var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x && "props" in x;
|
|
22
|
-
var isVueElement = (x) => typeof x === "object" && x !== null && "__v_isVNode" in x;
|
|
23
|
-
var isDOMElement = (x) => typeof x === "object" && x !== null && "nodeType" in x && typeof x.nodeName === "string";
|
|
24
|
-
var isElement = (x) => isReactElement(x) || isVueElement(x) || isDOMElement(x);
|
|
25
|
-
var isObject = (x) => x !== null && typeof x === "object";
|
|
26
|
-
var canProxy = (x) => isObject(x) && !refSet.has(x) && (Array.isArray(x) || !(Symbol.iterator in x)) && !isElement(x) && !(x instanceof WeakMap) && !(x instanceof WeakSet) && !(x instanceof Error) && !(x instanceof Number) && !(x instanceof Date) && !(x instanceof String) && !(x instanceof RegExp) && !(x instanceof ArrayBuffer) && !(x instanceof Promise) && !(x instanceof File) && !(x instanceof Blob) && !(x instanceof AbortController);
|
|
27
|
-
var isDev = () => process.env.NODE_ENV !== "production";
|
|
28
|
-
|
|
29
|
-
// src/clone.ts
|
|
30
|
-
function set(obj, key, val) {
|
|
31
|
-
if (typeof val.value === "object" && !canProxy(val.value)) val.value = clone(val.value);
|
|
32
|
-
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
|
|
33
|
-
Object.defineProperty(obj, key, val);
|
|
34
|
-
} else obj[key] = val.value;
|
|
35
|
-
}
|
|
36
|
-
function clone(x) {
|
|
37
|
-
if (typeof x !== "object") return x;
|
|
38
|
-
var i = 0, k, list, tmp, str = Object.prototype.toString.call(x);
|
|
39
|
-
if (str === "[object Object]") {
|
|
40
|
-
tmp = Object.create(Object.getPrototypeOf(x) || null);
|
|
41
|
-
} else if (str === "[object Array]") {
|
|
42
|
-
tmp = Array(x.length);
|
|
43
|
-
} else if (str === "[object Set]") {
|
|
44
|
-
tmp = /* @__PURE__ */ new Set();
|
|
45
|
-
x.forEach(function(val) {
|
|
46
|
-
tmp.add(clone(val));
|
|
47
|
-
});
|
|
48
|
-
} else if (str === "[object Map]") {
|
|
49
|
-
tmp = /* @__PURE__ */ new Map();
|
|
50
|
-
x.forEach(function(val, key) {
|
|
51
|
-
tmp.set(clone(key), clone(val));
|
|
52
|
-
});
|
|
53
|
-
} else if (str === "[object Date]") {
|
|
54
|
-
tmp = /* @__PURE__ */ new Date(+x);
|
|
55
|
-
} else if (str === "[object RegExp]") {
|
|
56
|
-
tmp = new RegExp(x.source, x.flags);
|
|
57
|
-
} else if (str === "[object DataView]") {
|
|
58
|
-
tmp = new x.constructor(clone(x.buffer));
|
|
59
|
-
} else if (str === "[object ArrayBuffer]") {
|
|
60
|
-
tmp = x.slice(0);
|
|
61
|
-
} else if (str === "[object Blob]") {
|
|
62
|
-
tmp = x.slice();
|
|
63
|
-
} else if (str.slice(-6) === "Array]") {
|
|
64
|
-
tmp = new x.constructor(x);
|
|
65
|
-
}
|
|
66
|
-
if (tmp) {
|
|
67
|
-
for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
|
|
68
|
-
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
|
|
69
|
-
}
|
|
70
|
-
for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
|
|
71
|
-
if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
|
|
72
|
-
set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return tmp || x;
|
|
76
|
-
}
|
|
77
|
-
var proxyStateMap = globalRef("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap());
|
|
78
|
-
var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version) => {
|
|
79
|
-
const cache = snapCache.get(target);
|
|
80
|
-
if (cache?.[0] === version) {
|
|
81
|
-
return cache[1];
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
82
15
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
Reflect.ownKeys(target).forEach((key) => {
|
|
87
|
-
const value = Reflect.get(target, key);
|
|
88
|
-
if (refSet.has(value)) {
|
|
89
|
-
proxyCompare.markToTrack(value, false);
|
|
90
|
-
snap[key] = value;
|
|
91
|
-
} else if (proxyStateMap.has(value)) {
|
|
92
|
-
snap[key] = snapshot(value);
|
|
93
|
-
} else {
|
|
94
|
-
snap[key] = value;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
return Object.freeze(snap);
|
|
98
|
-
}, proxyCache = /* @__PURE__ */ new WeakMap(), versionHolder = [1, 1], proxyFunction2 = (initialObject) => {
|
|
99
|
-
if (!isObject(initialObject)) {
|
|
100
|
-
throw new Error("object required");
|
|
101
|
-
}
|
|
102
|
-
const found = proxyCache.get(initialObject);
|
|
103
|
-
if (found) {
|
|
104
|
-
return found;
|
|
105
|
-
}
|
|
106
|
-
let version = versionHolder[0];
|
|
107
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
108
|
-
const notifyUpdate = (op, nextVersion = ++versionHolder[0]) => {
|
|
109
|
-
if (version !== nextVersion) {
|
|
110
|
-
version = nextVersion;
|
|
111
|
-
listeners.forEach((listener) => listener(op, nextVersion));
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
let checkVersion = versionHolder[1];
|
|
115
|
-
const ensureVersion = (nextCheckVersion = ++versionHolder[1]) => {
|
|
116
|
-
if (checkVersion !== nextCheckVersion && !listeners.size) {
|
|
117
|
-
checkVersion = nextCheckVersion;
|
|
118
|
-
propProxyStates.forEach(([propProxyState]) => {
|
|
119
|
-
const propVersion = propProxyState[1](nextCheckVersion);
|
|
120
|
-
if (propVersion > version) {
|
|
121
|
-
version = propVersion;
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
return version;
|
|
126
|
-
};
|
|
127
|
-
const createPropListener = (prop) => (op, nextVersion) => {
|
|
128
|
-
const newOp = [...op];
|
|
129
|
-
newOp[1] = [prop, ...newOp[1]];
|
|
130
|
-
notifyUpdate(newOp, nextVersion);
|
|
131
|
-
};
|
|
132
|
-
const propProxyStates = /* @__PURE__ */ new Map();
|
|
133
|
-
const addPropListener = (prop, propProxyState) => {
|
|
134
|
-
if (isDev() && propProxyStates.has(prop)) {
|
|
135
|
-
throw new Error("prop listener already exists");
|
|
136
|
-
}
|
|
137
|
-
if (listeners.size) {
|
|
138
|
-
const remove = propProxyState[3](createPropListener(prop));
|
|
139
|
-
propProxyStates.set(prop, [propProxyState, remove]);
|
|
140
|
-
} else {
|
|
141
|
-
propProxyStates.set(prop, [propProxyState]);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
const removePropListener = (prop) => {
|
|
145
|
-
const entry = propProxyStates.get(prop);
|
|
146
|
-
if (entry) {
|
|
147
|
-
propProxyStates.delete(prop);
|
|
148
|
-
entry[1]?.();
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
const addListener = (listener) => {
|
|
152
|
-
listeners.add(listener);
|
|
153
|
-
if (listeners.size === 1) {
|
|
154
|
-
propProxyStates.forEach(([propProxyState, prevRemove], prop) => {
|
|
155
|
-
if (isDev() && prevRemove) {
|
|
156
|
-
throw new Error("remove already exists");
|
|
157
|
-
}
|
|
158
|
-
const remove = propProxyState[3](createPropListener(prop));
|
|
159
|
-
propProxyStates.set(prop, [propProxyState, remove]);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
const removeListener = () => {
|
|
163
|
-
listeners.delete(listener);
|
|
164
|
-
if (listeners.size === 0) {
|
|
165
|
-
propProxyStates.forEach(([propProxyState, remove], prop) => {
|
|
166
|
-
if (remove) {
|
|
167
|
-
remove();
|
|
168
|
-
propProxyStates.set(prop, [propProxyState]);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
return removeListener;
|
|
174
|
-
};
|
|
175
|
-
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
176
|
-
const handler = {
|
|
177
|
-
deleteProperty(target, prop) {
|
|
178
|
-
const prevValue = Reflect.get(target, prop);
|
|
179
|
-
removePropListener(prop);
|
|
180
|
-
const deleted = Reflect.deleteProperty(target, prop);
|
|
181
|
-
if (deleted) {
|
|
182
|
-
notifyUpdate(["delete", [prop], prevValue]);
|
|
183
|
-
}
|
|
184
|
-
return deleted;
|
|
185
|
-
},
|
|
186
|
-
set(target, prop, value, receiver) {
|
|
187
|
-
const hasPrevValue = Reflect.has(target, prop);
|
|
188
|
-
const prevValue = Reflect.get(target, prop, receiver);
|
|
189
|
-
if (hasPrevValue && (objectIs(prevValue, value) || proxyCache.has(value) && objectIs(prevValue, proxyCache.get(value)))) {
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
removePropListener(prop);
|
|
193
|
-
if (isObject(value)) {
|
|
194
|
-
value = proxyCompare.getUntracked(value) || value;
|
|
195
|
-
}
|
|
196
|
-
let nextValue = value;
|
|
197
|
-
if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else {
|
|
198
|
-
if (!proxyStateMap.has(value) && canProxy(value)) {
|
|
199
|
-
nextValue = proxy(value);
|
|
200
|
-
}
|
|
201
|
-
const childProxyState = !refSet.has(nextValue) && proxyStateMap.get(nextValue);
|
|
202
|
-
if (childProxyState) {
|
|
203
|
-
addPropListener(prop, childProxyState);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
Reflect.set(target, prop, nextValue, receiver);
|
|
207
|
-
notifyUpdate(["set", [prop], value, prevValue]);
|
|
208
|
-
return true;
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
const proxyObject = newProxy(baseObject, handler);
|
|
212
|
-
proxyCache.set(initialObject, proxyObject);
|
|
213
|
-
const proxyState = [baseObject, ensureVersion, createSnapshot, addListener];
|
|
214
|
-
proxyStateMap.set(proxyObject, proxyState);
|
|
215
|
-
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
216
|
-
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
217
|
-
if (desc.get || desc.set) {
|
|
218
|
-
Object.defineProperty(baseObject, key, desc);
|
|
219
|
-
} else {
|
|
220
|
-
proxyObject[key] = initialObject[key];
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
return proxyObject;
|
|
224
|
-
}) => [
|
|
225
|
-
// public functions
|
|
226
|
-
proxyFunction2,
|
|
227
|
-
// shared state
|
|
228
|
-
proxyStateMap,
|
|
229
|
-
refSet,
|
|
230
|
-
// internal things
|
|
231
|
-
objectIs,
|
|
232
|
-
newProxy,
|
|
233
|
-
canProxy,
|
|
234
|
-
snapCache,
|
|
235
|
-
createSnapshot,
|
|
236
|
-
proxyCache,
|
|
237
|
-
versionHolder
|
|
238
|
-
];
|
|
239
|
-
var [proxyFunction] = buildProxyFunction();
|
|
240
|
-
function proxy(initialObject = {}) {
|
|
241
|
-
return proxyFunction(initialObject);
|
|
242
|
-
}
|
|
243
|
-
function subscribe(proxyObject, callback, notifyInSync) {
|
|
244
|
-
const proxyState = proxyStateMap.get(proxyObject);
|
|
245
|
-
if (isDev() && !proxyState) {
|
|
246
|
-
console.warn("Please use proxy object");
|
|
247
|
-
}
|
|
248
|
-
let promise;
|
|
249
|
-
const ops = [];
|
|
250
|
-
const addListener = proxyState[3];
|
|
251
|
-
let isListenerActive = false;
|
|
252
|
-
const listener = (op) => {
|
|
253
|
-
ops.push(op);
|
|
254
|
-
if (notifyInSync) {
|
|
255
|
-
callback(ops.splice(0));
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
if (!promise) {
|
|
259
|
-
promise = Promise.resolve().then(() => {
|
|
260
|
-
promise = void 0;
|
|
261
|
-
if (isListenerActive) {
|
|
262
|
-
callback(ops.splice(0));
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
const removeListener = addListener(listener);
|
|
268
|
-
isListenerActive = true;
|
|
269
|
-
return () => {
|
|
270
|
-
isListenerActive = false;
|
|
271
|
-
removeListener();
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
function snapshot(proxyObject) {
|
|
275
|
-
const proxyState = proxyStateMap.get(proxyObject);
|
|
276
|
-
if (isDev() && !proxyState) {
|
|
277
|
-
console.warn("Please use proxy object");
|
|
278
|
-
}
|
|
279
|
-
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
280
|
-
return createSnapshot(target, ensureVersion());
|
|
281
|
-
}
|
|
282
|
-
function ref(obj) {
|
|
283
|
-
refSet.add(obj);
|
|
284
|
-
return obj;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// src/proxy-computed.ts
|
|
288
|
-
function proxyWithComputed(initialObject, computedFns) {
|
|
289
|
-
const keys = Object.keys(computedFns);
|
|
290
|
-
keys.forEach((key) => {
|
|
291
|
-
if (Object.getOwnPropertyDescriptor(initialObject, key)) {
|
|
292
|
-
throw new Error("object property already defined");
|
|
293
|
-
}
|
|
294
|
-
const computedFn = computedFns[key];
|
|
295
|
-
const { get, set: set2 } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
|
|
296
|
-
const desc = {};
|
|
297
|
-
desc.get = () => get(snapshot(proxyObject));
|
|
298
|
-
if (set2) {
|
|
299
|
-
desc.set = (newValue) => set2(proxyObject, newValue);
|
|
300
|
-
}
|
|
301
|
-
Object.defineProperty(initialObject, key, desc);
|
|
302
|
-
});
|
|
303
|
-
const proxyObject = proxy(initialObject);
|
|
304
|
-
return proxyObject;
|
|
305
|
-
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
306
19
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
clone: () => import_clone.clone,
|
|
24
|
+
globalRef: () => import_global.globalRef,
|
|
25
|
+
proxy: () => import_proxy.proxy,
|
|
26
|
+
proxyWithComputed: () => import_proxy_computed.proxyWithComputed,
|
|
27
|
+
ref: () => import_proxy.ref,
|
|
28
|
+
snapshot: () => import_proxy.snapshot,
|
|
29
|
+
subscribe: () => import_proxy.subscribe
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
var import_clone = require("./clone.cjs");
|
|
33
|
+
var import_global = require("./global.cjs");
|
|
34
|
+
var import_proxy = require("./proxy.cjs");
|
|
35
|
+
var import_proxy_computed = require("./proxy-computed.cjs");
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
clone,
|
|
39
|
+
globalRef,
|
|
40
|
+
proxy,
|
|
41
|
+
proxyWithComputed,
|
|
42
|
+
ref,
|
|
43
|
+
snapshot,
|
|
44
|
+
subscribe
|
|
45
|
+
});
|