@zag-js/store 0.10.2 → 0.10.4
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/index.d.ts +3 -3
- package/dist/index.js +12 -312
- package/dist/index.mjs +3 -20
- package/dist/proxy-computed.d.ts +2 -5
- package/dist/proxy-computed.js +7 -237
- package/dist/proxy-computed.mjs +22 -7
- package/dist/proxy.d.ts +7 -8
- package/dist/proxy.js +20 -52
- package/dist/proxy.mjs +242 -14
- package/dist/subscribe-key.d.ts +2 -4
- package/dist/subscribe-key.js +9 -270
- package/dist/subscribe-key.mjs +17 -7
- package/package.json +2 -4
- package/dist/chunk-4HNO6REI.mjs +0 -253
- package/dist/chunk-GKQGATOG.mjs +0 -23
- package/dist/chunk-LQKXSU6I.mjs +0 -28
package/dist/proxy.js
CHANGED
|
@@ -1,38 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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);
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const proxyCompare = require('proxy-compare');
|
|
19
6
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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) => {
|
|
7
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
8
|
+
const isObject = (x) => typeof x === "object" && x !== null;
|
|
9
|
+
const proxyStateMap = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
const refSet = /* @__PURE__ */ new WeakSet();
|
|
11
|
+
const 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
12
|
switch (promise.status) {
|
|
37
13
|
case "fulfilled":
|
|
38
14
|
return promise.value;
|
|
@@ -47,12 +23,12 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
|
|
|
47
23
|
return cache[1];
|
|
48
24
|
}
|
|
49
25
|
const snap = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
50
|
-
|
|
26
|
+
proxyCompare.markToTrack(snap, true);
|
|
51
27
|
snapCache.set(target, [version, snap]);
|
|
52
28
|
Reflect.ownKeys(target).forEach((key) => {
|
|
53
29
|
const value = Reflect.get(target, key);
|
|
54
30
|
if (refSet.has(value)) {
|
|
55
|
-
|
|
31
|
+
proxyCompare.markToTrack(value, false);
|
|
56
32
|
snap[key] = value;
|
|
57
33
|
} else if (value instanceof Promise) {
|
|
58
34
|
Object.defineProperty(snap, key, {
|
|
@@ -163,11 +139,10 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
|
|
|
163
139
|
}
|
|
164
140
|
removePropListener(prop);
|
|
165
141
|
if (isObject(value)) {
|
|
166
|
-
value =
|
|
142
|
+
value = proxyCompare.getUntracked(value) || value;
|
|
167
143
|
}
|
|
168
144
|
let nextValue = value;
|
|
169
|
-
if (Object.getOwnPropertyDescriptor(target, prop)?.set) {
|
|
170
|
-
} else if (value instanceof Promise) {
|
|
145
|
+
if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else if (value instanceof Promise) {
|
|
171
146
|
value.then((v) => {
|
|
172
147
|
value.status = "fulfilled";
|
|
173
148
|
value.value = v;
|
|
@@ -220,14 +195,10 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
|
|
|
220
195
|
proxyCache,
|
|
221
196
|
versionHolder
|
|
222
197
|
];
|
|
223
|
-
|
|
198
|
+
const [proxyFunction] = buildProxyFunction();
|
|
224
199
|
function proxy(initialObject = {}) {
|
|
225
200
|
return proxyFunction(initialObject);
|
|
226
201
|
}
|
|
227
|
-
function getVersion(proxyObject) {
|
|
228
|
-
const proxyState = proxyStateMap.get(proxyObject);
|
|
229
|
-
return proxyState?.[1]();
|
|
230
|
-
}
|
|
231
202
|
function subscribe(proxyObject, callback, notifyInSync) {
|
|
232
203
|
const proxyState = proxyStateMap.get(proxyObject);
|
|
233
204
|
if (isDev && !proxyState) {
|
|
@@ -271,11 +242,8 @@ function ref(obj) {
|
|
|
271
242
|
refSet.add(obj);
|
|
272
243
|
return obj;
|
|
273
244
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
snapshot,
|
|
280
|
-
subscribe
|
|
281
|
-
});
|
|
245
|
+
|
|
246
|
+
exports.proxy = proxy;
|
|
247
|
+
exports.ref = ref;
|
|
248
|
+
exports.snapshot = snapshot;
|
|
249
|
+
exports.subscribe = subscribe;
|
package/dist/proxy.mjs
CHANGED
|
@@ -1,14 +1,242 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { markToTrack, getUntracked } from 'proxy-compare';
|
|
2
|
+
|
|
3
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
4
|
+
const isObject = (x) => typeof x === "object" && x !== null;
|
|
5
|
+
const proxyStateMap = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
const refSet = /* @__PURE__ */ new WeakSet();
|
|
7
|
+
const 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) ; else if (value instanceof Promise) {
|
|
142
|
+
value.then((v) => {
|
|
143
|
+
value.status = "fulfilled";
|
|
144
|
+
value.value = v;
|
|
145
|
+
notifyUpdate(["resolve", [prop], v]);
|
|
146
|
+
}).catch((e) => {
|
|
147
|
+
value.status = "rejected";
|
|
148
|
+
value.reason = e;
|
|
149
|
+
notifyUpdate(["reject", [prop], e]);
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
if (!proxyStateMap.has(value) && canProxy(value)) {
|
|
153
|
+
nextValue = proxy(value);
|
|
154
|
+
}
|
|
155
|
+
const childProxyState = !refSet.has(nextValue) && proxyStateMap.get(nextValue);
|
|
156
|
+
if (childProxyState) {
|
|
157
|
+
addPropListener(prop, childProxyState);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
161
|
+
notifyUpdate(["set", [prop], value, prevValue]);
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const proxyObject = newProxy(baseObject, handler);
|
|
166
|
+
proxyCache.set(initialObject, proxyObject);
|
|
167
|
+
const proxyState = [baseObject, ensureVersion, createSnapshot, addListener];
|
|
168
|
+
proxyStateMap.set(proxyObject, proxyState);
|
|
169
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
170
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
171
|
+
if (desc.get || desc.set) {
|
|
172
|
+
Object.defineProperty(baseObject, key, desc);
|
|
173
|
+
} else {
|
|
174
|
+
proxyObject[key] = initialObject[key];
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return proxyObject;
|
|
178
|
+
}) => [
|
|
179
|
+
// public functions
|
|
180
|
+
proxyFunction2,
|
|
181
|
+
// shared state
|
|
182
|
+
proxyStateMap,
|
|
183
|
+
refSet,
|
|
184
|
+
// internal things
|
|
185
|
+
objectIs,
|
|
186
|
+
newProxy,
|
|
187
|
+
canProxy,
|
|
188
|
+
defaultHandlePromise,
|
|
189
|
+
snapCache,
|
|
190
|
+
createSnapshot,
|
|
191
|
+
proxyCache,
|
|
192
|
+
versionHolder
|
|
193
|
+
];
|
|
194
|
+
const [proxyFunction] = buildProxyFunction();
|
|
195
|
+
function proxy(initialObject = {}) {
|
|
196
|
+
return proxyFunction(initialObject);
|
|
197
|
+
}
|
|
198
|
+
function subscribe(proxyObject, callback, notifyInSync) {
|
|
199
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
200
|
+
if (isDev && !proxyState) {
|
|
201
|
+
console.warn("Please use proxy object");
|
|
202
|
+
}
|
|
203
|
+
let promise;
|
|
204
|
+
const ops = [];
|
|
205
|
+
const addListener = proxyState[3];
|
|
206
|
+
let isListenerActive = false;
|
|
207
|
+
const listener = (op) => {
|
|
208
|
+
ops.push(op);
|
|
209
|
+
if (notifyInSync) {
|
|
210
|
+
callback(ops.splice(0));
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (!promise) {
|
|
214
|
+
promise = Promise.resolve().then(() => {
|
|
215
|
+
promise = void 0;
|
|
216
|
+
if (isListenerActive) {
|
|
217
|
+
callback(ops.splice(0));
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const removeListener = addListener(listener);
|
|
223
|
+
isListenerActive = true;
|
|
224
|
+
return () => {
|
|
225
|
+
isListenerActive = false;
|
|
226
|
+
removeListener();
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function snapshot(proxyObject, handlePromise) {
|
|
230
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
231
|
+
if (isDev && !proxyState) {
|
|
232
|
+
console.warn("Please use proxy object");
|
|
233
|
+
}
|
|
234
|
+
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
235
|
+
return createSnapshot(target, ensureVersion(), handlePromise);
|
|
236
|
+
}
|
|
237
|
+
function ref(obj) {
|
|
238
|
+
refSet.add(obj);
|
|
239
|
+
return obj;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { proxy, ref, snapshot, subscribe };
|
package/dist/subscribe-key.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
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 };
|
|
1
|
+
export type CompareFn<T = any> = (prev: T, next: T) => boolean;
|
|
2
|
+
export 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;
|
package/dist/subscribe-key.js
CHANGED
|
@@ -1,282 +1,21 @@
|
|
|
1
|
-
|
|
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);
|
|
1
|
+
'use strict';
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
var subscribe_key_exports = {};
|
|
22
|
-
__export(subscribe_key_exports, {
|
|
23
|
-
subscribeKey: () => subscribeKey
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(subscribe_key_exports);
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
26
4
|
|
|
27
|
-
|
|
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 subscribe(proxyObject, callback, notifyInSync) {
|
|
226
|
-
const proxyState = proxyStateMap.get(proxyObject);
|
|
227
|
-
if (isDev && !proxyState) {
|
|
228
|
-
console.warn("Please use proxy object");
|
|
229
|
-
}
|
|
230
|
-
let promise;
|
|
231
|
-
const ops = [];
|
|
232
|
-
const addListener = proxyState[3];
|
|
233
|
-
let isListenerActive = false;
|
|
234
|
-
const listener = (op) => {
|
|
235
|
-
ops.push(op);
|
|
236
|
-
if (notifyInSync) {
|
|
237
|
-
callback(ops.splice(0));
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
if (!promise) {
|
|
241
|
-
promise = Promise.resolve().then(() => {
|
|
242
|
-
promise = void 0;
|
|
243
|
-
if (isListenerActive) {
|
|
244
|
-
callback(ops.splice(0));
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
const removeListener = addListener(listener);
|
|
250
|
-
isListenerActive = true;
|
|
251
|
-
return () => {
|
|
252
|
-
isListenerActive = false;
|
|
253
|
-
removeListener();
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function snapshot(proxyObject, handlePromise) {
|
|
257
|
-
const proxyState = proxyStateMap.get(proxyObject);
|
|
258
|
-
if (isDev && !proxyState) {
|
|
259
|
-
console.warn("Please use proxy object");
|
|
260
|
-
}
|
|
261
|
-
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
262
|
-
return createSnapshot(target, ensureVersion(), handlePromise);
|
|
263
|
-
}
|
|
5
|
+
const proxy = require('./proxy.js');
|
|
264
6
|
|
|
265
|
-
|
|
266
|
-
var defaultCompareFn = (prev, next) => Object.is(prev, next);
|
|
7
|
+
const defaultCompareFn = (prev, next) => Object.is(prev, next);
|
|
267
8
|
function subscribeKey(obj, key, fn, sync, compareFn) {
|
|
268
|
-
let prev = Reflect.get(snapshot(obj), key);
|
|
9
|
+
let prev = Reflect.get(proxy.snapshot(obj), key);
|
|
269
10
|
const isEqual = compareFn || defaultCompareFn;
|
|
270
11
|
function onSnapshotChange() {
|
|
271
|
-
const snap = snapshot(obj);
|
|
12
|
+
const snap = proxy.snapshot(obj);
|
|
272
13
|
if (isEqual(prev, snap[key]))
|
|
273
14
|
return;
|
|
274
15
|
fn(snap[key]);
|
|
275
16
|
prev = Reflect.get(snap, key);
|
|
276
17
|
}
|
|
277
|
-
return subscribe(obj, onSnapshotChange, sync);
|
|
18
|
+
return proxy.subscribe(obj, onSnapshotChange, sync);
|
|
278
19
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
subscribeKey
|
|
282
|
-
});
|
|
20
|
+
|
|
21
|
+
exports.subscribeKey = subscribeKey;
|
package/dist/subscribe-key.mjs
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { snapshot, subscribe } from './proxy.mjs';
|
|
2
|
+
|
|
3
|
+
const defaultCompareFn = (prev, next) => Object.is(prev, next);
|
|
4
|
+
function subscribeKey(obj, key, fn, sync, compareFn) {
|
|
5
|
+
let prev = Reflect.get(snapshot(obj), key);
|
|
6
|
+
const isEqual = compareFn || defaultCompareFn;
|
|
7
|
+
function onSnapshotChange() {
|
|
8
|
+
const snap = snapshot(obj);
|
|
9
|
+
if (isEqual(prev, snap[key]))
|
|
10
|
+
return;
|
|
11
|
+
fn(snap[key]);
|
|
12
|
+
prev = Reflect.get(snap, key);
|
|
13
|
+
}
|
|
14
|
+
return subscribe(obj, onSnapshotChange, sync);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { subscribeKey };
|