@zag-js/store 0.2.6 → 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 +280 -8
- package/dist/index.mjs +12 -3
- 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,279 @@
|
|
|
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/subscribe-key.ts
|
|
21
|
+
var subscribe_key_exports = {};
|
|
22
|
+
__export(subscribe_key_exports, {
|
|
23
|
+
subscribeKey: () => subscribeKey
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(subscribe_key_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
|
+
proxyFunction2,
|
|
207
|
+
proxyStateMap,
|
|
208
|
+
refSet,
|
|
209
|
+
objectIs,
|
|
210
|
+
newProxy,
|
|
211
|
+
canProxy,
|
|
212
|
+
defaultHandlePromise,
|
|
213
|
+
snapCache,
|
|
214
|
+
createSnapshot,
|
|
215
|
+
proxyCache,
|
|
216
|
+
versionHolder
|
|
217
|
+
];
|
|
218
|
+
var [proxyFunction] = buildProxyFunction();
|
|
219
|
+
function proxy(initialObject = {}) {
|
|
220
|
+
return proxyFunction(initialObject);
|
|
221
|
+
}
|
|
222
|
+
function subscribe(proxyObject, callback, notifyInSync) {
|
|
223
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
224
|
+
if (isDev && !proxyState) {
|
|
225
|
+
console.warn("Please use proxy object");
|
|
226
|
+
}
|
|
227
|
+
let promise;
|
|
228
|
+
const ops = [];
|
|
229
|
+
const addListener = proxyState[3];
|
|
230
|
+
let isListenerActive = false;
|
|
231
|
+
const listener = (op) => {
|
|
232
|
+
ops.push(op);
|
|
233
|
+
if (notifyInSync) {
|
|
234
|
+
callback(ops.splice(0));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (!promise) {
|
|
238
|
+
promise = Promise.resolve().then(() => {
|
|
239
|
+
promise = void 0;
|
|
240
|
+
if (isListenerActive) {
|
|
241
|
+
callback(ops.splice(0));
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
const removeListener = addListener(listener);
|
|
247
|
+
isListenerActive = true;
|
|
248
|
+
return () => {
|
|
249
|
+
isListenerActive = false;
|
|
250
|
+
removeListener();
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function snapshot(proxyObject, handlePromise) {
|
|
254
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
255
|
+
if (isDev && !proxyState) {
|
|
256
|
+
console.warn("Please use proxy object");
|
|
257
|
+
}
|
|
258
|
+
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
259
|
+
return createSnapshot(target, ensureVersion(), handlePromise);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/subscribe-key.ts
|
|
263
|
+
var defaultCompareFn = (prev, next) => Object.is(prev, next);
|
|
264
|
+
function subscribeKey(obj, key, fn, sync, compareFn) {
|
|
265
|
+
let prev = Reflect.get(snapshot(obj), key);
|
|
266
|
+
const isEqual = compareFn || defaultCompareFn;
|
|
267
|
+
function onSnapshotChange() {
|
|
268
|
+
const snap = snapshot(obj);
|
|
269
|
+
if (isEqual(prev, snap[key]))
|
|
270
|
+
return;
|
|
271
|
+
fn(snap[key]);
|
|
272
|
+
prev = Reflect.get(snap, key);
|
|
273
|
+
}
|
|
274
|
+
return subscribe(obj, onSnapshotChange, sync);
|
|
275
|
+
}
|
|
276
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
277
|
+
0 && (module.exports = {
|
|
278
|
+
subscribeKey
|
|
279
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/store",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "The reactive store package for zag machines",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -23,8 +23,7 @@
|
|
|
23
23
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"proxy-compare": "2.4.0"
|
|
27
|
-
"valtio": "^1.9.0"
|
|
26
|
+
"proxy-compare": "2.4.0"
|
|
28
27
|
},
|
|
29
28
|
"clean-package": "../../clean-package.config.json",
|
|
30
29
|
"main": "dist/index.js",
|