@zag-js/store 0.2.6 → 0.2.8
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/chunk-4HNO6REI.mjs +253 -0
- package/dist/chunk-GKQGATOG.mjs +23 -0
- package/dist/chunk-LQKXSU6I.mjs +28 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +283 -8
- package/dist/index.mjs +12 -3
- package/dist/proxy-computed.d.ts +10 -0
- package/dist/proxy-computed.js +256 -0
- package/dist/proxy-computed.mjs +7 -0
- package/dist/proxy.d.ts +17 -0
- package/dist/proxy.js +281 -0
- package/dist/proxy.mjs +14 -0
- package/dist/subscribe-key.d.ts +4 -0
- package/dist/subscribe-key.js +282 -0
- package/dist/subscribe-key.mjs +7 -0
- package/package.json +2 -3
|
@@ -0,0 +1,256 @@
|
|
|
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/proxy-computed.ts
|
|
21
|
+
var proxy_computed_exports = {};
|
|
22
|
+
__export(proxy_computed_exports, {
|
|
23
|
+
proxyWithComputed: () => proxyWithComputed
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(proxy_computed_exports);
|
|
26
|
+
|
|
27
|
+
// src/proxy.ts
|
|
28
|
+
var import_proxy_compare = require("proxy-compare");
|
|
29
|
+
var isDev = process.env.NODE_ENV !== "production";
|
|
30
|
+
var isObject = (x) => typeof x === "object" && x !== null;
|
|
31
|
+
var proxyStateMap = /* @__PURE__ */ new WeakMap();
|
|
32
|
+
var refSet = /* @__PURE__ */ new WeakSet();
|
|
33
|
+
var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), canProxy = (x) => isObject(x) && !refSet.has(x) && (Array.isArray(x) || !(Symbol.iterator in 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), defaultHandlePromise = (promise) => {
|
|
34
|
+
switch (promise.status) {
|
|
35
|
+
case "fulfilled":
|
|
36
|
+
return promise.value;
|
|
37
|
+
case "rejected":
|
|
38
|
+
throw promise.reason;
|
|
39
|
+
default:
|
|
40
|
+
throw promise;
|
|
41
|
+
}
|
|
42
|
+
}, snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version, handlePromise = defaultHandlePromise) => {
|
|
43
|
+
const cache = snapCache.get(target);
|
|
44
|
+
if (cache?.[0] === version) {
|
|
45
|
+
return cache[1];
|
|
46
|
+
}
|
|
47
|
+
const snap = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
48
|
+
(0, import_proxy_compare.markToTrack)(snap, true);
|
|
49
|
+
snapCache.set(target, [version, snap]);
|
|
50
|
+
Reflect.ownKeys(target).forEach((key) => {
|
|
51
|
+
const value = Reflect.get(target, key);
|
|
52
|
+
if (refSet.has(value)) {
|
|
53
|
+
(0, import_proxy_compare.markToTrack)(value, false);
|
|
54
|
+
snap[key] = value;
|
|
55
|
+
} else if (value instanceof Promise) {
|
|
56
|
+
Object.defineProperty(snap, key, {
|
|
57
|
+
get() {
|
|
58
|
+
return handlePromise(value);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
} else if (proxyStateMap.has(value)) {
|
|
62
|
+
snap[key] = snapshot(value, handlePromise);
|
|
63
|
+
} else {
|
|
64
|
+
snap[key] = value;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return Object.freeze(snap);
|
|
68
|
+
}, proxyCache = /* @__PURE__ */ new WeakMap(), versionHolder = [1, 1], proxyFunction2 = (initialObject) => {
|
|
69
|
+
if (!isObject(initialObject)) {
|
|
70
|
+
throw new Error("object required");
|
|
71
|
+
}
|
|
72
|
+
const found = proxyCache.get(initialObject);
|
|
73
|
+
if (found) {
|
|
74
|
+
return found;
|
|
75
|
+
}
|
|
76
|
+
let version = versionHolder[0];
|
|
77
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
78
|
+
const notifyUpdate = (op, nextVersion = ++versionHolder[0]) => {
|
|
79
|
+
if (version !== nextVersion) {
|
|
80
|
+
version = nextVersion;
|
|
81
|
+
listeners.forEach((listener) => listener(op, nextVersion));
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
let checkVersion = versionHolder[1];
|
|
85
|
+
const ensureVersion = (nextCheckVersion = ++versionHolder[1]) => {
|
|
86
|
+
if (checkVersion !== nextCheckVersion && !listeners.size) {
|
|
87
|
+
checkVersion = nextCheckVersion;
|
|
88
|
+
propProxyStates.forEach(([propProxyState]) => {
|
|
89
|
+
const propVersion = propProxyState[1](nextCheckVersion);
|
|
90
|
+
if (propVersion > version) {
|
|
91
|
+
version = propVersion;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return version;
|
|
96
|
+
};
|
|
97
|
+
const createPropListener = (prop) => (op, nextVersion) => {
|
|
98
|
+
const newOp = [...op];
|
|
99
|
+
newOp[1] = [prop, ...newOp[1]];
|
|
100
|
+
notifyUpdate(newOp, nextVersion);
|
|
101
|
+
};
|
|
102
|
+
const propProxyStates = /* @__PURE__ */ new Map();
|
|
103
|
+
const addPropListener = (prop, propProxyState) => {
|
|
104
|
+
if (isDev && propProxyStates.has(prop)) {
|
|
105
|
+
throw new Error("prop listener already exists");
|
|
106
|
+
}
|
|
107
|
+
if (listeners.size) {
|
|
108
|
+
const remove = propProxyState[3](createPropListener(prop));
|
|
109
|
+
propProxyStates.set(prop, [propProxyState, remove]);
|
|
110
|
+
} else {
|
|
111
|
+
propProxyStates.set(prop, [propProxyState]);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const removePropListener = (prop) => {
|
|
115
|
+
const entry = propProxyStates.get(prop);
|
|
116
|
+
if (entry) {
|
|
117
|
+
propProxyStates.delete(prop);
|
|
118
|
+
entry[1]?.();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const addListener = (listener) => {
|
|
122
|
+
listeners.add(listener);
|
|
123
|
+
if (listeners.size === 1) {
|
|
124
|
+
propProxyStates.forEach(([propProxyState, prevRemove], prop) => {
|
|
125
|
+
if (isDev && prevRemove) {
|
|
126
|
+
throw new Error("remove already exists");
|
|
127
|
+
}
|
|
128
|
+
const remove = propProxyState[3](createPropListener(prop));
|
|
129
|
+
propProxyStates.set(prop, [propProxyState, remove]);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const removeListener = () => {
|
|
133
|
+
listeners.delete(listener);
|
|
134
|
+
if (listeners.size === 0) {
|
|
135
|
+
propProxyStates.forEach(([propProxyState, remove], prop) => {
|
|
136
|
+
if (remove) {
|
|
137
|
+
remove();
|
|
138
|
+
propProxyStates.set(prop, [propProxyState]);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
return removeListener;
|
|
144
|
+
};
|
|
145
|
+
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
146
|
+
const handler = {
|
|
147
|
+
deleteProperty(target, prop) {
|
|
148
|
+
const prevValue = Reflect.get(target, prop);
|
|
149
|
+
removePropListener(prop);
|
|
150
|
+
const deleted = Reflect.deleteProperty(target, prop);
|
|
151
|
+
if (deleted) {
|
|
152
|
+
notifyUpdate(["delete", [prop], prevValue]);
|
|
153
|
+
}
|
|
154
|
+
return deleted;
|
|
155
|
+
},
|
|
156
|
+
set(target, prop, value, receiver) {
|
|
157
|
+
const hasPrevValue = Reflect.has(target, prop);
|
|
158
|
+
const prevValue = Reflect.get(target, prop, receiver);
|
|
159
|
+
if (hasPrevValue && (objectIs(prevValue, value) || proxyCache.has(value) && objectIs(prevValue, proxyCache.get(value)))) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
removePropListener(prop);
|
|
163
|
+
if (isObject(value)) {
|
|
164
|
+
value = (0, import_proxy_compare.getUntracked)(value) || value;
|
|
165
|
+
}
|
|
166
|
+
let nextValue = value;
|
|
167
|
+
if (Object.getOwnPropertyDescriptor(target, prop)?.set) {
|
|
168
|
+
} else if (value instanceof Promise) {
|
|
169
|
+
value.then((v) => {
|
|
170
|
+
value.status = "fulfilled";
|
|
171
|
+
value.value = v;
|
|
172
|
+
notifyUpdate(["resolve", [prop], v]);
|
|
173
|
+
}).catch((e) => {
|
|
174
|
+
value.status = "rejected";
|
|
175
|
+
value.reason = e;
|
|
176
|
+
notifyUpdate(["reject", [prop], e]);
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
if (!proxyStateMap.has(value) && canProxy(value)) {
|
|
180
|
+
nextValue = proxy(value);
|
|
181
|
+
}
|
|
182
|
+
const childProxyState = !refSet.has(nextValue) && proxyStateMap.get(nextValue);
|
|
183
|
+
if (childProxyState) {
|
|
184
|
+
addPropListener(prop, childProxyState);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
188
|
+
notifyUpdate(["set", [prop], value, prevValue]);
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
const proxyObject = newProxy(baseObject, handler);
|
|
193
|
+
proxyCache.set(initialObject, proxyObject);
|
|
194
|
+
const proxyState = [baseObject, ensureVersion, createSnapshot, addListener];
|
|
195
|
+
proxyStateMap.set(proxyObject, proxyState);
|
|
196
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
197
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
198
|
+
if (desc.get || desc.set) {
|
|
199
|
+
Object.defineProperty(baseObject, key, desc);
|
|
200
|
+
} else {
|
|
201
|
+
proxyObject[key] = initialObject[key];
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
return proxyObject;
|
|
205
|
+
}) => [
|
|
206
|
+
// public functions
|
|
207
|
+
proxyFunction2,
|
|
208
|
+
// shared state
|
|
209
|
+
proxyStateMap,
|
|
210
|
+
refSet,
|
|
211
|
+
// internal things
|
|
212
|
+
objectIs,
|
|
213
|
+
newProxy,
|
|
214
|
+
canProxy,
|
|
215
|
+
defaultHandlePromise,
|
|
216
|
+
snapCache,
|
|
217
|
+
createSnapshot,
|
|
218
|
+
proxyCache,
|
|
219
|
+
versionHolder
|
|
220
|
+
];
|
|
221
|
+
var [proxyFunction] = buildProxyFunction();
|
|
222
|
+
function proxy(initialObject = {}) {
|
|
223
|
+
return proxyFunction(initialObject);
|
|
224
|
+
}
|
|
225
|
+
function snapshot(proxyObject, handlePromise) {
|
|
226
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
227
|
+
if (isDev && !proxyState) {
|
|
228
|
+
console.warn("Please use proxy object");
|
|
229
|
+
}
|
|
230
|
+
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
231
|
+
return createSnapshot(target, ensureVersion(), handlePromise);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// src/proxy-computed.ts
|
|
235
|
+
function proxyWithComputed(initialObject, computedFns) {
|
|
236
|
+
const keys = Object.keys(computedFns);
|
|
237
|
+
keys.forEach((key) => {
|
|
238
|
+
if (Object.getOwnPropertyDescriptor(initialObject, key)) {
|
|
239
|
+
throw new Error("object property already defined");
|
|
240
|
+
}
|
|
241
|
+
const computedFn = computedFns[key];
|
|
242
|
+
const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
|
|
243
|
+
const desc = {};
|
|
244
|
+
desc.get = () => get(snapshot(proxyObject));
|
|
245
|
+
if (set) {
|
|
246
|
+
desc.set = (newValue) => set(proxyObject, newValue);
|
|
247
|
+
}
|
|
248
|
+
Object.defineProperty(initialObject, key, desc);
|
|
249
|
+
});
|
|
250
|
+
const proxyObject = proxy(initialObject);
|
|
251
|
+
return proxyObject;
|
|
252
|
+
}
|
|
253
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
254
|
+
0 && (module.exports = {
|
|
255
|
+
proxyWithComputed
|
|
256
|
+
});
|
package/dist/proxy.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type AsRef = {
|
|
2
|
+
$$valtioRef: true;
|
|
3
|
+
};
|
|
4
|
+
type Path = (string | symbol)[];
|
|
5
|
+
type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown] | [op: "resolve", path: Path, value: unknown] | [op: "reject", path: Path, error: unknown];
|
|
6
|
+
type AnyFunction = (...args: any[]) => any;
|
|
7
|
+
type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<any> ? Awaited<T> : {
|
|
8
|
+
readonly [K in keyof T]: Snapshot<T[K]>;
|
|
9
|
+
};
|
|
10
|
+
type HandlePromise = <P extends Promise<any>>(promise: P) => Awaited<P>;
|
|
11
|
+
declare function proxy<T extends object>(initialObject?: T): T;
|
|
12
|
+
declare function getVersion(proxyObject: unknown): number | undefined;
|
|
13
|
+
declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void;
|
|
14
|
+
declare function snapshot<T extends object>(proxyObject: T, handlePromise?: HandlePromise): Snapshot<T>;
|
|
15
|
+
declare function ref<T extends object>(obj: T): T & AsRef;
|
|
16
|
+
|
|
17
|
+
export { Snapshot, getVersion, proxy, ref, snapshot, subscribe };
|
package/dist/proxy.js
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
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/proxy.ts
|
|
21
|
+
var proxy_exports = {};
|
|
22
|
+
__export(proxy_exports, {
|
|
23
|
+
getVersion: () => getVersion,
|
|
24
|
+
proxy: () => proxy,
|
|
25
|
+
ref: () => ref,
|
|
26
|
+
snapshot: () => snapshot,
|
|
27
|
+
subscribe: () => subscribe
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(proxy_exports);
|
|
30
|
+
var import_proxy_compare = require("proxy-compare");
|
|
31
|
+
var isDev = process.env.NODE_ENV !== "production";
|
|
32
|
+
var isObject = (x) => typeof x === "object" && x !== null;
|
|
33
|
+
var proxyStateMap = /* @__PURE__ */ new WeakMap();
|
|
34
|
+
var refSet = /* @__PURE__ */ new WeakSet();
|
|
35
|
+
var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), canProxy = (x) => isObject(x) && !refSet.has(x) && (Array.isArray(x) || !(Symbol.iterator in 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), defaultHandlePromise = (promise) => {
|
|
36
|
+
switch (promise.status) {
|
|
37
|
+
case "fulfilled":
|
|
38
|
+
return promise.value;
|
|
39
|
+
case "rejected":
|
|
40
|
+
throw promise.reason;
|
|
41
|
+
default:
|
|
42
|
+
throw promise;
|
|
43
|
+
}
|
|
44
|
+
}, snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version, handlePromise = defaultHandlePromise) => {
|
|
45
|
+
const cache = snapCache.get(target);
|
|
46
|
+
if (cache?.[0] === version) {
|
|
47
|
+
return cache[1];
|
|
48
|
+
}
|
|
49
|
+
const snap = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
50
|
+
(0, import_proxy_compare.markToTrack)(snap, true);
|
|
51
|
+
snapCache.set(target, [version, snap]);
|
|
52
|
+
Reflect.ownKeys(target).forEach((key) => {
|
|
53
|
+
const value = Reflect.get(target, key);
|
|
54
|
+
if (refSet.has(value)) {
|
|
55
|
+
(0, import_proxy_compare.markToTrack)(value, false);
|
|
56
|
+
snap[key] = value;
|
|
57
|
+
} else if (value instanceof Promise) {
|
|
58
|
+
Object.defineProperty(snap, key, {
|
|
59
|
+
get() {
|
|
60
|
+
return handlePromise(value);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
} else if (proxyStateMap.has(value)) {
|
|
64
|
+
snap[key] = snapshot(value, handlePromise);
|
|
65
|
+
} else {
|
|
66
|
+
snap[key] = value;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return Object.freeze(snap);
|
|
70
|
+
}, proxyCache = /* @__PURE__ */ new WeakMap(), versionHolder = [1, 1], proxyFunction2 = (initialObject) => {
|
|
71
|
+
if (!isObject(initialObject)) {
|
|
72
|
+
throw new Error("object required");
|
|
73
|
+
}
|
|
74
|
+
const found = proxyCache.get(initialObject);
|
|
75
|
+
if (found) {
|
|
76
|
+
return found;
|
|
77
|
+
}
|
|
78
|
+
let version = versionHolder[0];
|
|
79
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
80
|
+
const notifyUpdate = (op, nextVersion = ++versionHolder[0]) => {
|
|
81
|
+
if (version !== nextVersion) {
|
|
82
|
+
version = nextVersion;
|
|
83
|
+
listeners.forEach((listener) => listener(op, nextVersion));
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
let checkVersion = versionHolder[1];
|
|
87
|
+
const ensureVersion = (nextCheckVersion = ++versionHolder[1]) => {
|
|
88
|
+
if (checkVersion !== nextCheckVersion && !listeners.size) {
|
|
89
|
+
checkVersion = nextCheckVersion;
|
|
90
|
+
propProxyStates.forEach(([propProxyState]) => {
|
|
91
|
+
const propVersion = propProxyState[1](nextCheckVersion);
|
|
92
|
+
if (propVersion > version) {
|
|
93
|
+
version = propVersion;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return version;
|
|
98
|
+
};
|
|
99
|
+
const createPropListener = (prop) => (op, nextVersion) => {
|
|
100
|
+
const newOp = [...op];
|
|
101
|
+
newOp[1] = [prop, ...newOp[1]];
|
|
102
|
+
notifyUpdate(newOp, nextVersion);
|
|
103
|
+
};
|
|
104
|
+
const propProxyStates = /* @__PURE__ */ new Map();
|
|
105
|
+
const addPropListener = (prop, propProxyState) => {
|
|
106
|
+
if (isDev && propProxyStates.has(prop)) {
|
|
107
|
+
throw new Error("prop listener already exists");
|
|
108
|
+
}
|
|
109
|
+
if (listeners.size) {
|
|
110
|
+
const remove = propProxyState[3](createPropListener(prop));
|
|
111
|
+
propProxyStates.set(prop, [propProxyState, remove]);
|
|
112
|
+
} else {
|
|
113
|
+
propProxyStates.set(prop, [propProxyState]);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const removePropListener = (prop) => {
|
|
117
|
+
const entry = propProxyStates.get(prop);
|
|
118
|
+
if (entry) {
|
|
119
|
+
propProxyStates.delete(prop);
|
|
120
|
+
entry[1]?.();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const addListener = (listener) => {
|
|
124
|
+
listeners.add(listener);
|
|
125
|
+
if (listeners.size === 1) {
|
|
126
|
+
propProxyStates.forEach(([propProxyState, prevRemove], prop) => {
|
|
127
|
+
if (isDev && prevRemove) {
|
|
128
|
+
throw new Error("remove already exists");
|
|
129
|
+
}
|
|
130
|
+
const remove = propProxyState[3](createPropListener(prop));
|
|
131
|
+
propProxyStates.set(prop, [propProxyState, remove]);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const removeListener = () => {
|
|
135
|
+
listeners.delete(listener);
|
|
136
|
+
if (listeners.size === 0) {
|
|
137
|
+
propProxyStates.forEach(([propProxyState, remove], prop) => {
|
|
138
|
+
if (remove) {
|
|
139
|
+
remove();
|
|
140
|
+
propProxyStates.set(prop, [propProxyState]);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
return removeListener;
|
|
146
|
+
};
|
|
147
|
+
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
148
|
+
const handler = {
|
|
149
|
+
deleteProperty(target, prop) {
|
|
150
|
+
const prevValue = Reflect.get(target, prop);
|
|
151
|
+
removePropListener(prop);
|
|
152
|
+
const deleted = Reflect.deleteProperty(target, prop);
|
|
153
|
+
if (deleted) {
|
|
154
|
+
notifyUpdate(["delete", [prop], prevValue]);
|
|
155
|
+
}
|
|
156
|
+
return deleted;
|
|
157
|
+
},
|
|
158
|
+
set(target, prop, value, receiver) {
|
|
159
|
+
const hasPrevValue = Reflect.has(target, prop);
|
|
160
|
+
const prevValue = Reflect.get(target, prop, receiver);
|
|
161
|
+
if (hasPrevValue && (objectIs(prevValue, value) || proxyCache.has(value) && objectIs(prevValue, proxyCache.get(value)))) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
removePropListener(prop);
|
|
165
|
+
if (isObject(value)) {
|
|
166
|
+
value = (0, import_proxy_compare.getUntracked)(value) || value;
|
|
167
|
+
}
|
|
168
|
+
let nextValue = value;
|
|
169
|
+
if (Object.getOwnPropertyDescriptor(target, prop)?.set) {
|
|
170
|
+
} else if (value instanceof Promise) {
|
|
171
|
+
value.then((v) => {
|
|
172
|
+
value.status = "fulfilled";
|
|
173
|
+
value.value = v;
|
|
174
|
+
notifyUpdate(["resolve", [prop], v]);
|
|
175
|
+
}).catch((e) => {
|
|
176
|
+
value.status = "rejected";
|
|
177
|
+
value.reason = e;
|
|
178
|
+
notifyUpdate(["reject", [prop], e]);
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
if (!proxyStateMap.has(value) && canProxy(value)) {
|
|
182
|
+
nextValue = proxy(value);
|
|
183
|
+
}
|
|
184
|
+
const childProxyState = !refSet.has(nextValue) && proxyStateMap.get(nextValue);
|
|
185
|
+
if (childProxyState) {
|
|
186
|
+
addPropListener(prop, childProxyState);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
190
|
+
notifyUpdate(["set", [prop], value, prevValue]);
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const proxyObject = newProxy(baseObject, handler);
|
|
195
|
+
proxyCache.set(initialObject, proxyObject);
|
|
196
|
+
const proxyState = [baseObject, ensureVersion, createSnapshot, addListener];
|
|
197
|
+
proxyStateMap.set(proxyObject, proxyState);
|
|
198
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
199
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
200
|
+
if (desc.get || desc.set) {
|
|
201
|
+
Object.defineProperty(baseObject, key, desc);
|
|
202
|
+
} else {
|
|
203
|
+
proxyObject[key] = initialObject[key];
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
return proxyObject;
|
|
207
|
+
}) => [
|
|
208
|
+
// public functions
|
|
209
|
+
proxyFunction2,
|
|
210
|
+
// shared state
|
|
211
|
+
proxyStateMap,
|
|
212
|
+
refSet,
|
|
213
|
+
// internal things
|
|
214
|
+
objectIs,
|
|
215
|
+
newProxy,
|
|
216
|
+
canProxy,
|
|
217
|
+
defaultHandlePromise,
|
|
218
|
+
snapCache,
|
|
219
|
+
createSnapshot,
|
|
220
|
+
proxyCache,
|
|
221
|
+
versionHolder
|
|
222
|
+
];
|
|
223
|
+
var [proxyFunction] = buildProxyFunction();
|
|
224
|
+
function proxy(initialObject = {}) {
|
|
225
|
+
return proxyFunction(initialObject);
|
|
226
|
+
}
|
|
227
|
+
function getVersion(proxyObject) {
|
|
228
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
229
|
+
return proxyState?.[1]();
|
|
230
|
+
}
|
|
231
|
+
function subscribe(proxyObject, callback, notifyInSync) {
|
|
232
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
233
|
+
if (isDev && !proxyState) {
|
|
234
|
+
console.warn("Please use proxy object");
|
|
235
|
+
}
|
|
236
|
+
let promise;
|
|
237
|
+
const ops = [];
|
|
238
|
+
const addListener = proxyState[3];
|
|
239
|
+
let isListenerActive = false;
|
|
240
|
+
const listener = (op) => {
|
|
241
|
+
ops.push(op);
|
|
242
|
+
if (notifyInSync) {
|
|
243
|
+
callback(ops.splice(0));
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (!promise) {
|
|
247
|
+
promise = Promise.resolve().then(() => {
|
|
248
|
+
promise = void 0;
|
|
249
|
+
if (isListenerActive) {
|
|
250
|
+
callback(ops.splice(0));
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
const removeListener = addListener(listener);
|
|
256
|
+
isListenerActive = true;
|
|
257
|
+
return () => {
|
|
258
|
+
isListenerActive = false;
|
|
259
|
+
removeListener();
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function snapshot(proxyObject, handlePromise) {
|
|
263
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
264
|
+
if (isDev && !proxyState) {
|
|
265
|
+
console.warn("Please use proxy object");
|
|
266
|
+
}
|
|
267
|
+
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
268
|
+
return createSnapshot(target, ensureVersion(), handlePromise);
|
|
269
|
+
}
|
|
270
|
+
function ref(obj) {
|
|
271
|
+
refSet.add(obj);
|
|
272
|
+
return obj;
|
|
273
|
+
}
|
|
274
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
275
|
+
0 && (module.exports = {
|
|
276
|
+
getVersion,
|
|
277
|
+
proxy,
|
|
278
|
+
ref,
|
|
279
|
+
snapshot,
|
|
280
|
+
subscribe
|
|
281
|
+
});
|
package/dist/proxy.mjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
type CompareFn<T = any> = (prev: T, next: T) => boolean;
|
|
2
|
+
declare function subscribeKey<T extends object, K extends keyof T>(obj: T, key: K, fn: (value: T[K]) => void, sync?: boolean, compareFn?: (prev: T[K], next: T[K]) => boolean): () => void;
|
|
3
|
+
|
|
4
|
+
export { CompareFn, subscribeKey };
|