@zag-js/store 0.1.3 → 0.1.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.js +37 -6
- package/dist/index.mjs +239 -0
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
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
|
+
|
|
1
20
|
// src/index.ts
|
|
2
|
-
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
getHandler: () => getHandler,
|
|
24
|
+
getVersion: () => getVersion,
|
|
25
|
+
proxy: () => proxy,
|
|
26
|
+
proxyWithComputed: () => proxyWithComputed,
|
|
27
|
+
ref: () => ref,
|
|
28
|
+
snapshot: () => snapshot,
|
|
29
|
+
subscribe: () => subscribe
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
32
|
+
var import_proxy_compare = require("proxy-compare");
|
|
3
33
|
var __DEV__ = process.env.NODE_ENV !== "production";
|
|
4
34
|
var VERSION = Symbol();
|
|
5
35
|
var LISTENERS = Symbol();
|
|
@@ -57,12 +87,12 @@ function proxy(initialObject = {}) {
|
|
|
57
87
|
return cache[1];
|
|
58
88
|
}
|
|
59
89
|
const snapshot2 = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
60
|
-
markToTrack(snapshot2, true);
|
|
90
|
+
(0, import_proxy_compare.markToTrack)(snapshot2, true);
|
|
61
91
|
snapshotCache.set(receiver, [version, snapshot2]);
|
|
62
92
|
Reflect.ownKeys(target).forEach((key) => {
|
|
63
93
|
const value = Reflect.get(target, key, receiver);
|
|
64
94
|
if (refSet.has(value)) {
|
|
65
|
-
markToTrack(value, false);
|
|
95
|
+
(0, import_proxy_compare.markToTrack)(value, false);
|
|
66
96
|
snapshot2[key] = value;
|
|
67
97
|
} else if (value instanceof Promise) {
|
|
68
98
|
if (PROMISE_RESULT in value) {
|
|
@@ -130,7 +160,7 @@ function proxy(initialObject = {}) {
|
|
|
130
160
|
childListeners.delete(popPropListener(prop));
|
|
131
161
|
}
|
|
132
162
|
if (isObject(value)) {
|
|
133
|
-
value = getUntracked(value) || value;
|
|
163
|
+
value = (0, import_proxy_compare.getUntracked)(value) || value;
|
|
134
164
|
}
|
|
135
165
|
let nextValue;
|
|
136
166
|
if ((_a = Object.getOwnPropertyDescriptor(target, prop)) == null ? void 0 : _a.set) {
|
|
@@ -228,7 +258,8 @@ function proxyWithComputed(initialObject, computedFns) {
|
|
|
228
258
|
const proxyObject = proxy(initialObject);
|
|
229
259
|
return proxyObject;
|
|
230
260
|
}
|
|
231
|
-
export
|
|
261
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
262
|
+
0 && (module.exports = {
|
|
232
263
|
getHandler,
|
|
233
264
|
getVersion,
|
|
234
265
|
proxy,
|
|
@@ -236,4 +267,4 @@ export {
|
|
|
236
267
|
ref,
|
|
237
268
|
snapshot,
|
|
238
269
|
subscribe
|
|
239
|
-
};
|
|
270
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { getUntracked, markToTrack } from "proxy-compare";
|
|
3
|
+
var __DEV__ = process.env.NODE_ENV !== "production";
|
|
4
|
+
var VERSION = Symbol();
|
|
5
|
+
var LISTENERS = Symbol();
|
|
6
|
+
var SNAPSHOT = Symbol();
|
|
7
|
+
var HANDLER = Symbol();
|
|
8
|
+
var PROMISE_RESULT = Symbol();
|
|
9
|
+
var PROMISE_ERROR = Symbol();
|
|
10
|
+
var refSet = /* @__PURE__ */ new WeakSet();
|
|
11
|
+
function ref(o) {
|
|
12
|
+
refSet.add(o);
|
|
13
|
+
return o;
|
|
14
|
+
}
|
|
15
|
+
var isObject = (x) => typeof x === "object" && x !== null;
|
|
16
|
+
var 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);
|
|
17
|
+
var proxyCache = /* @__PURE__ */ new WeakMap();
|
|
18
|
+
var globalVersion = 1;
|
|
19
|
+
var snapshotCache = /* @__PURE__ */ new WeakMap();
|
|
20
|
+
function proxy(initialObject = {}) {
|
|
21
|
+
if (!isObject(initialObject)) {
|
|
22
|
+
throw new Error("object required");
|
|
23
|
+
}
|
|
24
|
+
const found = proxyCache.get(initialObject);
|
|
25
|
+
if (found) {
|
|
26
|
+
return found;
|
|
27
|
+
}
|
|
28
|
+
let version = globalVersion;
|
|
29
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
30
|
+
const notifyUpdate = (op, nextVersion = ++globalVersion) => {
|
|
31
|
+
if (version !== nextVersion) {
|
|
32
|
+
version = nextVersion;
|
|
33
|
+
listeners.forEach((listener) => listener(op, nextVersion));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const propListeners = /* @__PURE__ */ new Map();
|
|
37
|
+
const getPropListener = (prop) => {
|
|
38
|
+
let propListener = propListeners.get(prop);
|
|
39
|
+
if (!propListener) {
|
|
40
|
+
propListener = (op, nextVersion) => {
|
|
41
|
+
const newOp = [...op];
|
|
42
|
+
newOp[1] = [prop, ...newOp[1]];
|
|
43
|
+
notifyUpdate(newOp, nextVersion);
|
|
44
|
+
};
|
|
45
|
+
propListeners.set(prop, propListener);
|
|
46
|
+
}
|
|
47
|
+
return propListener;
|
|
48
|
+
};
|
|
49
|
+
const popPropListener = (prop) => {
|
|
50
|
+
const propListener = propListeners.get(prop);
|
|
51
|
+
propListeners.delete(prop);
|
|
52
|
+
return propListener;
|
|
53
|
+
};
|
|
54
|
+
const createSnapshot = (target, receiver) => {
|
|
55
|
+
const cache = snapshotCache.get(receiver);
|
|
56
|
+
if ((cache == null ? void 0 : cache[0]) === version) {
|
|
57
|
+
return cache[1];
|
|
58
|
+
}
|
|
59
|
+
const snapshot2 = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
60
|
+
markToTrack(snapshot2, true);
|
|
61
|
+
snapshotCache.set(receiver, [version, snapshot2]);
|
|
62
|
+
Reflect.ownKeys(target).forEach((key) => {
|
|
63
|
+
const value = Reflect.get(target, key, receiver);
|
|
64
|
+
if (refSet.has(value)) {
|
|
65
|
+
markToTrack(value, false);
|
|
66
|
+
snapshot2[key] = value;
|
|
67
|
+
} else if (value instanceof Promise) {
|
|
68
|
+
if (PROMISE_RESULT in value) {
|
|
69
|
+
snapshot2[key] = value[PROMISE_RESULT];
|
|
70
|
+
} else {
|
|
71
|
+
const errorOrPromise = value[PROMISE_ERROR] || value;
|
|
72
|
+
Object.defineProperty(snapshot2, key, {
|
|
73
|
+
get() {
|
|
74
|
+
if (PROMISE_RESULT in value) {
|
|
75
|
+
return value[PROMISE_RESULT];
|
|
76
|
+
}
|
|
77
|
+
throw errorOrPromise;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
} else if (value == null ? void 0 : value[LISTENERS]) {
|
|
82
|
+
snapshot2[key] = value[SNAPSHOT];
|
|
83
|
+
} else {
|
|
84
|
+
snapshot2[key] = value;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
Object.freeze(snapshot2);
|
|
88
|
+
return snapshot2;
|
|
89
|
+
};
|
|
90
|
+
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
91
|
+
const handler = {
|
|
92
|
+
get(target, prop, receiver) {
|
|
93
|
+
if (prop === VERSION) {
|
|
94
|
+
return version;
|
|
95
|
+
}
|
|
96
|
+
if (prop === LISTENERS) {
|
|
97
|
+
return listeners;
|
|
98
|
+
}
|
|
99
|
+
if (prop === SNAPSHOT) {
|
|
100
|
+
return createSnapshot(target, receiver);
|
|
101
|
+
}
|
|
102
|
+
if (prop === HANDLER) {
|
|
103
|
+
return handler;
|
|
104
|
+
}
|
|
105
|
+
return Reflect.get(target, prop, receiver);
|
|
106
|
+
},
|
|
107
|
+
deleteProperty(target, prop) {
|
|
108
|
+
const prevValue = Reflect.get(target, prop);
|
|
109
|
+
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS];
|
|
110
|
+
if (childListeners) {
|
|
111
|
+
childListeners.delete(popPropListener(prop));
|
|
112
|
+
}
|
|
113
|
+
const deleted = Reflect.deleteProperty(target, prop);
|
|
114
|
+
if (deleted) {
|
|
115
|
+
notifyUpdate(["delete", [prop], prevValue]);
|
|
116
|
+
}
|
|
117
|
+
return deleted;
|
|
118
|
+
},
|
|
119
|
+
is: Object.is,
|
|
120
|
+
canProxy,
|
|
121
|
+
set(target, prop, value, receiver) {
|
|
122
|
+
var _a;
|
|
123
|
+
const hasPrevValue = Reflect.has(target, prop);
|
|
124
|
+
const prevValue = Reflect.get(target, prop, receiver);
|
|
125
|
+
if (hasPrevValue && this.is(prevValue, value)) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS];
|
|
129
|
+
if (childListeners) {
|
|
130
|
+
childListeners.delete(popPropListener(prop));
|
|
131
|
+
}
|
|
132
|
+
if (isObject(value)) {
|
|
133
|
+
value = getUntracked(value) || value;
|
|
134
|
+
}
|
|
135
|
+
let nextValue;
|
|
136
|
+
if ((_a = Object.getOwnPropertyDescriptor(target, prop)) == null ? void 0 : _a.set) {
|
|
137
|
+
nextValue = value;
|
|
138
|
+
} else if (value instanceof Promise) {
|
|
139
|
+
nextValue = value.then((v) => {
|
|
140
|
+
nextValue[PROMISE_RESULT] = v;
|
|
141
|
+
notifyUpdate(["resolve", [prop], v]);
|
|
142
|
+
return v;
|
|
143
|
+
}).catch((e) => {
|
|
144
|
+
nextValue[PROMISE_ERROR] = e;
|
|
145
|
+
notifyUpdate(["reject", [prop], e]);
|
|
146
|
+
});
|
|
147
|
+
} else if (value == null ? void 0 : value[LISTENERS]) {
|
|
148
|
+
nextValue = value;
|
|
149
|
+
nextValue[LISTENERS].add(getPropListener(prop));
|
|
150
|
+
} else if (this.canProxy(value)) {
|
|
151
|
+
nextValue = proxy(value);
|
|
152
|
+
nextValue[LISTENERS].add(getPropListener(prop));
|
|
153
|
+
} else {
|
|
154
|
+
nextValue = value;
|
|
155
|
+
}
|
|
156
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
157
|
+
notifyUpdate(["set", [prop], value, prevValue]);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const proxyObject = new Proxy(baseObject, handler);
|
|
162
|
+
proxyCache.set(initialObject, proxyObject);
|
|
163
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
164
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
165
|
+
if (desc.get || desc.set) {
|
|
166
|
+
Object.defineProperty(baseObject, key, desc);
|
|
167
|
+
} else {
|
|
168
|
+
proxyObject[key] = initialObject[key];
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return proxyObject;
|
|
172
|
+
}
|
|
173
|
+
function getVersion(proxyObject) {
|
|
174
|
+
return isObject(proxyObject) ? proxyObject[VERSION] : void 0;
|
|
175
|
+
}
|
|
176
|
+
function subscribe(proxyObject, callback, notifyInSync) {
|
|
177
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[LISTENERS])) {
|
|
178
|
+
console.warn("Please use proxy object");
|
|
179
|
+
}
|
|
180
|
+
let promise;
|
|
181
|
+
const ops = [];
|
|
182
|
+
const listener = (op) => {
|
|
183
|
+
ops.push(op);
|
|
184
|
+
if (notifyInSync) {
|
|
185
|
+
callback(ops.splice(0));
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (!promise) {
|
|
189
|
+
promise = Promise.resolve().then(() => {
|
|
190
|
+
promise = void 0;
|
|
191
|
+
callback(ops.splice(0));
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
proxyObject[LISTENERS].add(listener);
|
|
196
|
+
return () => {
|
|
197
|
+
;
|
|
198
|
+
proxyObject[LISTENERS].delete(listener);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function snapshot(proxyObject) {
|
|
202
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[SNAPSHOT])) {
|
|
203
|
+
console.warn("Please use proxy object");
|
|
204
|
+
}
|
|
205
|
+
return proxyObject[SNAPSHOT];
|
|
206
|
+
}
|
|
207
|
+
function getHandler(proxyObject) {
|
|
208
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[HANDLER])) {
|
|
209
|
+
console.warn("Please use proxy object");
|
|
210
|
+
}
|
|
211
|
+
return proxyObject[HANDLER];
|
|
212
|
+
}
|
|
213
|
+
function proxyWithComputed(initialObject, computedFns) {
|
|
214
|
+
;
|
|
215
|
+
Object.keys(computedFns).forEach((key) => {
|
|
216
|
+
if (Object.getOwnPropertyDescriptor(initialObject, key)) {
|
|
217
|
+
throw new Error("object property already defined");
|
|
218
|
+
}
|
|
219
|
+
const computedFn = computedFns[key];
|
|
220
|
+
const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
|
|
221
|
+
const desc = {};
|
|
222
|
+
desc.get = () => get(snapshot(proxyObject));
|
|
223
|
+
if (set) {
|
|
224
|
+
desc.set = (newValue) => set(proxyObject, newValue);
|
|
225
|
+
}
|
|
226
|
+
Object.defineProperty(initialObject, key, desc);
|
|
227
|
+
});
|
|
228
|
+
const proxyObject = proxy(initialObject);
|
|
229
|
+
return proxyObject;
|
|
230
|
+
}
|
|
231
|
+
export {
|
|
232
|
+
getHandler,
|
|
233
|
+
getVersion,
|
|
234
|
+
proxy,
|
|
235
|
+
proxyWithComputed,
|
|
236
|
+
ref,
|
|
237
|
+
snapshot,
|
|
238
|
+
subscribe
|
|
239
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@zag-js/store",
|
|
4
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
5
4
|
"description": "The reactive store package for zag machines",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
6
8
|
"keywords": [
|
|
7
9
|
"js",
|
|
8
10
|
"utils",
|
|
@@ -12,8 +14,6 @@
|
|
|
12
14
|
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
13
15
|
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
14
16
|
"license": "MIT",
|
|
15
|
-
"main": "dist/index.js",
|
|
16
|
-
"types": "dist/index.d.ts",
|
|
17
17
|
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/store",
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"files": [
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"proxy-compare": "2.3.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"build-fast": "tsup src/index.ts --format=esm",
|
|
32
|
+
"build-fast": "tsup src/index.ts --format=esm,cjs",
|
|
33
33
|
"start": "pnpm build --watch",
|
|
34
|
-
"build": "tsup src/index.ts --format=esm --dts",
|
|
34
|
+
"build": "tsup src/index.ts --format=esm,cjs --dts",
|
|
35
35
|
"test": "jest --config ../../../jest.config.js --rootDir tests",
|
|
36
36
|
"lint": "eslint src --ext .ts,.tsx",
|
|
37
37
|
"test-ci": "pnpm test --ci --runInBand -u",
|