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