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