@zag-js/store 0.0.0-dev-20220625180418
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/README.md +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +260 -0
- package/dist/index.mjs +241 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# @zag-js/store
|
|
2
|
+
|
|
3
|
+
The reactive store package for zag machines
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn add @zag-js/store
|
|
9
|
+
# or
|
|
10
|
+
npm i @zag-js/store
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Contribution
|
|
14
|
+
|
|
15
|
+
Yes please! See the
|
|
16
|
+
[contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md)
|
|
17
|
+
for details.
|
|
18
|
+
|
|
19
|
+
## Licence
|
|
20
|
+
|
|
21
|
+
This project is licensed under the terms of the
|
|
22
|
+
[MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface AsRef {
|
|
2
|
+
$$valtioRef: true;
|
|
3
|
+
}
|
|
4
|
+
export declare function ref<T extends object>(o: T): T & AsRef;
|
|
5
|
+
declare type Path = (string | symbol)[];
|
|
6
|
+
declare type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown] | [op: "resolve", path: Path, value: unknown] | [op: "reject", path: Path, error: unknown];
|
|
7
|
+
export declare function proxy<T extends object>(initialObject?: T): T;
|
|
8
|
+
export declare function getVersion(proxyObject: unknown): number | undefined;
|
|
9
|
+
export declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void;
|
|
10
|
+
declare type AnyFunction = (...args: any[]) => any;
|
|
11
|
+
declare type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<infer V> ? Snapshot<V> : {
|
|
12
|
+
readonly [K in keyof T]: Snapshot<T[K]>;
|
|
13
|
+
};
|
|
14
|
+
export declare function snapshot<T extends object>(proxyObject: T): Snapshot<T>;
|
|
15
|
+
export declare function getHandler<T extends object>(proxyObject: T): any;
|
|
16
|
+
export declare function proxyWithComputed<T extends object, U extends object>(initialObject: T, computedFns: {
|
|
17
|
+
[K in keyof U]: ((snap: Snapshot<T>) => U[K]) | {
|
|
18
|
+
get: (snap: Snapshot<T>) => U[K];
|
|
19
|
+
set?: (state: T, newValue: U[K]) => void;
|
|
20
|
+
};
|
|
21
|
+
}): T & U;
|
|
22
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
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/index.ts
|
|
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");
|
|
33
|
+
var __DEV__ = void 0 !== "production";
|
|
34
|
+
var VERSION = Symbol();
|
|
35
|
+
var LISTENERS = Symbol();
|
|
36
|
+
var SNAPSHOT = Symbol();
|
|
37
|
+
var HANDLER = Symbol();
|
|
38
|
+
var PROMISE_RESULT = Symbol();
|
|
39
|
+
var PROMISE_ERROR = Symbol();
|
|
40
|
+
var refSet = /* @__PURE__ */ new WeakSet();
|
|
41
|
+
function ref(o) {
|
|
42
|
+
refSet.add(o);
|
|
43
|
+
return o;
|
|
44
|
+
}
|
|
45
|
+
var isObject = (x) => typeof x === "object" && x !== null;
|
|
46
|
+
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);
|
|
47
|
+
var proxyCache = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
var globalVersion = 1;
|
|
49
|
+
var snapshotCache = /* @__PURE__ */ new WeakMap();
|
|
50
|
+
function proxy(initialObject = {}) {
|
|
51
|
+
if (!isObject(initialObject)) {
|
|
52
|
+
throw new Error("object required");
|
|
53
|
+
}
|
|
54
|
+
const found = proxyCache.get(initialObject);
|
|
55
|
+
if (found) {
|
|
56
|
+
return found;
|
|
57
|
+
}
|
|
58
|
+
let version = globalVersion;
|
|
59
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
60
|
+
const notifyUpdate = (op, nextVersion = ++globalVersion) => {
|
|
61
|
+
if (version !== nextVersion) {
|
|
62
|
+
version = nextVersion;
|
|
63
|
+
listeners.forEach((listener) => listener(op, nextVersion));
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const propListeners = /* @__PURE__ */ new Map();
|
|
67
|
+
const getPropListener = (prop) => {
|
|
68
|
+
let propListener = propListeners.get(prop);
|
|
69
|
+
if (!propListener) {
|
|
70
|
+
propListener = (op, nextVersion) => {
|
|
71
|
+
const newOp = [...op];
|
|
72
|
+
newOp[1] = [prop, ...newOp[1]];
|
|
73
|
+
notifyUpdate(newOp, nextVersion);
|
|
74
|
+
};
|
|
75
|
+
propListeners.set(prop, propListener);
|
|
76
|
+
}
|
|
77
|
+
return propListener;
|
|
78
|
+
};
|
|
79
|
+
const popPropListener = (prop) => {
|
|
80
|
+
const propListener = propListeners.get(prop);
|
|
81
|
+
propListeners.delete(prop);
|
|
82
|
+
return propListener;
|
|
83
|
+
};
|
|
84
|
+
const createSnapshot = (target, receiver) => {
|
|
85
|
+
const cache = snapshotCache.get(receiver);
|
|
86
|
+
if ((cache == null ? void 0 : cache[0]) === version) {
|
|
87
|
+
return cache[1];
|
|
88
|
+
}
|
|
89
|
+
const snapshot2 = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
90
|
+
(0, import_proxy_compare.markToTrack)(snapshot2, true);
|
|
91
|
+
snapshotCache.set(receiver, [version, snapshot2]);
|
|
92
|
+
Reflect.ownKeys(target).forEach((key) => {
|
|
93
|
+
const value = Reflect.get(target, key, receiver);
|
|
94
|
+
if (refSet.has(value)) {
|
|
95
|
+
(0, import_proxy_compare.markToTrack)(value, false);
|
|
96
|
+
snapshot2[key] = value;
|
|
97
|
+
} else if (value instanceof Promise) {
|
|
98
|
+
if (PROMISE_RESULT in value) {
|
|
99
|
+
snapshot2[key] = value[PROMISE_RESULT];
|
|
100
|
+
} else {
|
|
101
|
+
const errorOrPromise = value[PROMISE_ERROR] || value;
|
|
102
|
+
Object.defineProperty(snapshot2, key, {
|
|
103
|
+
get() {
|
|
104
|
+
if (PROMISE_RESULT in value) {
|
|
105
|
+
return value[PROMISE_RESULT];
|
|
106
|
+
}
|
|
107
|
+
throw errorOrPromise;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
} else if (value == null ? void 0 : value[LISTENERS]) {
|
|
112
|
+
snapshot2[key] = value[SNAPSHOT];
|
|
113
|
+
} else {
|
|
114
|
+
snapshot2[key] = value;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
Object.freeze(snapshot2);
|
|
118
|
+
return snapshot2;
|
|
119
|
+
};
|
|
120
|
+
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
121
|
+
const handler = {
|
|
122
|
+
get(target, prop, receiver) {
|
|
123
|
+
if (prop === VERSION) {
|
|
124
|
+
return version;
|
|
125
|
+
}
|
|
126
|
+
if (prop === LISTENERS) {
|
|
127
|
+
return listeners;
|
|
128
|
+
}
|
|
129
|
+
if (prop === SNAPSHOT) {
|
|
130
|
+
return createSnapshot(target, receiver);
|
|
131
|
+
}
|
|
132
|
+
if (prop === HANDLER) {
|
|
133
|
+
return handler;
|
|
134
|
+
}
|
|
135
|
+
return Reflect.get(target, prop, receiver);
|
|
136
|
+
},
|
|
137
|
+
deleteProperty(target, prop) {
|
|
138
|
+
const prevValue = Reflect.get(target, prop);
|
|
139
|
+
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS];
|
|
140
|
+
if (childListeners) {
|
|
141
|
+
childListeners.delete(popPropListener(prop));
|
|
142
|
+
}
|
|
143
|
+
const deleted = Reflect.deleteProperty(target, prop);
|
|
144
|
+
if (deleted) {
|
|
145
|
+
notifyUpdate(["delete", [prop], prevValue]);
|
|
146
|
+
}
|
|
147
|
+
return deleted;
|
|
148
|
+
},
|
|
149
|
+
is: Object.is,
|
|
150
|
+
canProxy,
|
|
151
|
+
set(target, prop, value, receiver) {
|
|
152
|
+
var _a;
|
|
153
|
+
const hasPrevValue = Reflect.has(target, prop);
|
|
154
|
+
const prevValue = Reflect.get(target, prop, receiver);
|
|
155
|
+
if (hasPrevValue && this.is(prevValue, value)) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS];
|
|
159
|
+
if (childListeners) {
|
|
160
|
+
childListeners.delete(popPropListener(prop));
|
|
161
|
+
}
|
|
162
|
+
if (isObject(value)) {
|
|
163
|
+
value = (0, import_proxy_compare.getUntracked)(value) || value;
|
|
164
|
+
}
|
|
165
|
+
let nextValue;
|
|
166
|
+
if ((_a = Object.getOwnPropertyDescriptor(target, prop)) == null ? void 0 : _a.set) {
|
|
167
|
+
nextValue = value;
|
|
168
|
+
} else if (value instanceof Promise) {
|
|
169
|
+
nextValue = value.then((v) => {
|
|
170
|
+
nextValue[PROMISE_RESULT] = v;
|
|
171
|
+
notifyUpdate(["resolve", [prop], v]);
|
|
172
|
+
return v;
|
|
173
|
+
}).catch((e) => {
|
|
174
|
+
nextValue[PROMISE_ERROR] = e;
|
|
175
|
+
notifyUpdate(["reject", [prop], e]);
|
|
176
|
+
});
|
|
177
|
+
} else if (value == null ? void 0 : value[LISTENERS]) {
|
|
178
|
+
nextValue = value;
|
|
179
|
+
nextValue[LISTENERS].add(getPropListener(prop));
|
|
180
|
+
} else if (this.canProxy(value)) {
|
|
181
|
+
nextValue = proxy(value);
|
|
182
|
+
nextValue[LISTENERS].add(getPropListener(prop));
|
|
183
|
+
} else {
|
|
184
|
+
nextValue = value;
|
|
185
|
+
}
|
|
186
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
187
|
+
notifyUpdate(["set", [prop], value, prevValue]);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
const proxyObject = new Proxy(baseObject, handler);
|
|
192
|
+
proxyCache.set(initialObject, proxyObject);
|
|
193
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
194
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
195
|
+
if (desc.get || desc.set) {
|
|
196
|
+
Object.defineProperty(baseObject, key, desc);
|
|
197
|
+
} else {
|
|
198
|
+
proxyObject[key] = initialObject[key];
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
return proxyObject;
|
|
202
|
+
}
|
|
203
|
+
function getVersion(proxyObject) {
|
|
204
|
+
return isObject(proxyObject) ? proxyObject[VERSION] : void 0;
|
|
205
|
+
}
|
|
206
|
+
function subscribe(proxyObject, callback, notifyInSync) {
|
|
207
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[LISTENERS])) {
|
|
208
|
+
console.warn("Please use proxy object");
|
|
209
|
+
}
|
|
210
|
+
let promise;
|
|
211
|
+
const ops = [];
|
|
212
|
+
const listener = (op) => {
|
|
213
|
+
ops.push(op);
|
|
214
|
+
if (notifyInSync) {
|
|
215
|
+
callback(ops.splice(0));
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (!promise) {
|
|
219
|
+
promise = Promise.resolve().then(() => {
|
|
220
|
+
promise = void 0;
|
|
221
|
+
callback(ops.splice(0));
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
proxyObject[LISTENERS].add(listener);
|
|
226
|
+
return () => {
|
|
227
|
+
;
|
|
228
|
+
proxyObject[LISTENERS].delete(listener);
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function snapshot(proxyObject) {
|
|
232
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[SNAPSHOT])) {
|
|
233
|
+
console.warn("Please use proxy object");
|
|
234
|
+
}
|
|
235
|
+
return proxyObject[SNAPSHOT];
|
|
236
|
+
}
|
|
237
|
+
function getHandler(proxyObject) {
|
|
238
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[HANDLER])) {
|
|
239
|
+
console.warn("Please use proxy object");
|
|
240
|
+
}
|
|
241
|
+
return proxyObject[HANDLER];
|
|
242
|
+
}
|
|
243
|
+
function proxyWithComputed(initialObject, computedFns) {
|
|
244
|
+
;
|
|
245
|
+
Object.keys(computedFns).forEach((key) => {
|
|
246
|
+
if (Object.getOwnPropertyDescriptor(initialObject, key)) {
|
|
247
|
+
throw new Error("object property already defined");
|
|
248
|
+
}
|
|
249
|
+
const computedFn = computedFns[key];
|
|
250
|
+
const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
|
|
251
|
+
const desc = {};
|
|
252
|
+
desc.get = () => get(snapshot(proxyObject));
|
|
253
|
+
if (set) {
|
|
254
|
+
desc.set = (newValue) => set(proxyObject, newValue);
|
|
255
|
+
}
|
|
256
|
+
Object.defineProperty(initialObject, key, desc);
|
|
257
|
+
});
|
|
258
|
+
const proxyObject = proxy(initialObject);
|
|
259
|
+
return proxyObject;
|
|
260
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { getUntracked, markToTrack } from "proxy-compare";
|
|
5
|
+
var __DEV__ = void 0 !== "production";
|
|
6
|
+
var VERSION = Symbol();
|
|
7
|
+
var LISTENERS = Symbol();
|
|
8
|
+
var SNAPSHOT = Symbol();
|
|
9
|
+
var HANDLER = Symbol();
|
|
10
|
+
var PROMISE_RESULT = Symbol();
|
|
11
|
+
var PROMISE_ERROR = Symbol();
|
|
12
|
+
var refSet = /* @__PURE__ */ new WeakSet();
|
|
13
|
+
function ref(o) {
|
|
14
|
+
refSet.add(o);
|
|
15
|
+
return o;
|
|
16
|
+
}
|
|
17
|
+
var isObject = (x) => typeof x === "object" && x !== null;
|
|
18
|
+
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);
|
|
19
|
+
var proxyCache = /* @__PURE__ */ new WeakMap();
|
|
20
|
+
var globalVersion = 1;
|
|
21
|
+
var snapshotCache = /* @__PURE__ */ new WeakMap();
|
|
22
|
+
function proxy(initialObject = {}) {
|
|
23
|
+
if (!isObject(initialObject)) {
|
|
24
|
+
throw new Error("object required");
|
|
25
|
+
}
|
|
26
|
+
const found = proxyCache.get(initialObject);
|
|
27
|
+
if (found) {
|
|
28
|
+
return found;
|
|
29
|
+
}
|
|
30
|
+
let version = globalVersion;
|
|
31
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
32
|
+
const notifyUpdate = (op, nextVersion = ++globalVersion) => {
|
|
33
|
+
if (version !== nextVersion) {
|
|
34
|
+
version = nextVersion;
|
|
35
|
+
listeners.forEach((listener) => listener(op, nextVersion));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const propListeners = /* @__PURE__ */ new Map();
|
|
39
|
+
const getPropListener = (prop) => {
|
|
40
|
+
let propListener = propListeners.get(prop);
|
|
41
|
+
if (!propListener) {
|
|
42
|
+
propListener = (op, nextVersion) => {
|
|
43
|
+
const newOp = [...op];
|
|
44
|
+
newOp[1] = [prop, ...newOp[1]];
|
|
45
|
+
notifyUpdate(newOp, nextVersion);
|
|
46
|
+
};
|
|
47
|
+
propListeners.set(prop, propListener);
|
|
48
|
+
}
|
|
49
|
+
return propListener;
|
|
50
|
+
};
|
|
51
|
+
const popPropListener = (prop) => {
|
|
52
|
+
const propListener = propListeners.get(prop);
|
|
53
|
+
propListeners.delete(prop);
|
|
54
|
+
return propListener;
|
|
55
|
+
};
|
|
56
|
+
const createSnapshot = (target, receiver) => {
|
|
57
|
+
const cache = snapshotCache.get(receiver);
|
|
58
|
+
if ((cache == null ? void 0 : cache[0]) === version) {
|
|
59
|
+
return cache[1];
|
|
60
|
+
}
|
|
61
|
+
const snapshot2 = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
62
|
+
markToTrack(snapshot2, true);
|
|
63
|
+
snapshotCache.set(receiver, [version, snapshot2]);
|
|
64
|
+
Reflect.ownKeys(target).forEach((key) => {
|
|
65
|
+
const value = Reflect.get(target, key, receiver);
|
|
66
|
+
if (refSet.has(value)) {
|
|
67
|
+
markToTrack(value, false);
|
|
68
|
+
snapshot2[key] = value;
|
|
69
|
+
} else if (value instanceof Promise) {
|
|
70
|
+
if (PROMISE_RESULT in value) {
|
|
71
|
+
snapshot2[key] = value[PROMISE_RESULT];
|
|
72
|
+
} else {
|
|
73
|
+
const errorOrPromise = value[PROMISE_ERROR] || value;
|
|
74
|
+
Object.defineProperty(snapshot2, key, {
|
|
75
|
+
get() {
|
|
76
|
+
if (PROMISE_RESULT in value) {
|
|
77
|
+
return value[PROMISE_RESULT];
|
|
78
|
+
}
|
|
79
|
+
throw errorOrPromise;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
} else if (value == null ? void 0 : value[LISTENERS]) {
|
|
84
|
+
snapshot2[key] = value[SNAPSHOT];
|
|
85
|
+
} else {
|
|
86
|
+
snapshot2[key] = value;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
Object.freeze(snapshot2);
|
|
90
|
+
return snapshot2;
|
|
91
|
+
};
|
|
92
|
+
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
93
|
+
const handler = {
|
|
94
|
+
get(target, prop, receiver) {
|
|
95
|
+
if (prop === VERSION) {
|
|
96
|
+
return version;
|
|
97
|
+
}
|
|
98
|
+
if (prop === LISTENERS) {
|
|
99
|
+
return listeners;
|
|
100
|
+
}
|
|
101
|
+
if (prop === SNAPSHOT) {
|
|
102
|
+
return createSnapshot(target, receiver);
|
|
103
|
+
}
|
|
104
|
+
if (prop === HANDLER) {
|
|
105
|
+
return handler;
|
|
106
|
+
}
|
|
107
|
+
return Reflect.get(target, prop, receiver);
|
|
108
|
+
},
|
|
109
|
+
deleteProperty(target, prop) {
|
|
110
|
+
const prevValue = Reflect.get(target, prop);
|
|
111
|
+
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS];
|
|
112
|
+
if (childListeners) {
|
|
113
|
+
childListeners.delete(popPropListener(prop));
|
|
114
|
+
}
|
|
115
|
+
const deleted = Reflect.deleteProperty(target, prop);
|
|
116
|
+
if (deleted) {
|
|
117
|
+
notifyUpdate(["delete", [prop], prevValue]);
|
|
118
|
+
}
|
|
119
|
+
return deleted;
|
|
120
|
+
},
|
|
121
|
+
is: Object.is,
|
|
122
|
+
canProxy,
|
|
123
|
+
set(target, prop, value, receiver) {
|
|
124
|
+
var _a;
|
|
125
|
+
const hasPrevValue = Reflect.has(target, prop);
|
|
126
|
+
const prevValue = Reflect.get(target, prop, receiver);
|
|
127
|
+
if (hasPrevValue && this.is(prevValue, value)) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS];
|
|
131
|
+
if (childListeners) {
|
|
132
|
+
childListeners.delete(popPropListener(prop));
|
|
133
|
+
}
|
|
134
|
+
if (isObject(value)) {
|
|
135
|
+
value = getUntracked(value) || value;
|
|
136
|
+
}
|
|
137
|
+
let nextValue;
|
|
138
|
+
if ((_a = Object.getOwnPropertyDescriptor(target, prop)) == null ? void 0 : _a.set) {
|
|
139
|
+
nextValue = value;
|
|
140
|
+
} else if (value instanceof Promise) {
|
|
141
|
+
nextValue = value.then((v) => {
|
|
142
|
+
nextValue[PROMISE_RESULT] = v;
|
|
143
|
+
notifyUpdate(["resolve", [prop], v]);
|
|
144
|
+
return v;
|
|
145
|
+
}).catch((e) => {
|
|
146
|
+
nextValue[PROMISE_ERROR] = e;
|
|
147
|
+
notifyUpdate(["reject", [prop], e]);
|
|
148
|
+
});
|
|
149
|
+
} else if (value == null ? void 0 : value[LISTENERS]) {
|
|
150
|
+
nextValue = value;
|
|
151
|
+
nextValue[LISTENERS].add(getPropListener(prop));
|
|
152
|
+
} else if (this.canProxy(value)) {
|
|
153
|
+
nextValue = proxy(value);
|
|
154
|
+
nextValue[LISTENERS].add(getPropListener(prop));
|
|
155
|
+
} else {
|
|
156
|
+
nextValue = value;
|
|
157
|
+
}
|
|
158
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
159
|
+
notifyUpdate(["set", [prop], value, prevValue]);
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const proxyObject = new Proxy(baseObject, handler);
|
|
164
|
+
proxyCache.set(initialObject, proxyObject);
|
|
165
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
166
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
167
|
+
if (desc.get || desc.set) {
|
|
168
|
+
Object.defineProperty(baseObject, key, desc);
|
|
169
|
+
} else {
|
|
170
|
+
proxyObject[key] = initialObject[key];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
return proxyObject;
|
|
174
|
+
}
|
|
175
|
+
function getVersion(proxyObject) {
|
|
176
|
+
return isObject(proxyObject) ? proxyObject[VERSION] : void 0;
|
|
177
|
+
}
|
|
178
|
+
function subscribe(proxyObject, callback, notifyInSync) {
|
|
179
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[LISTENERS])) {
|
|
180
|
+
console.warn("Please use proxy object");
|
|
181
|
+
}
|
|
182
|
+
let promise;
|
|
183
|
+
const ops = [];
|
|
184
|
+
const listener = (op) => {
|
|
185
|
+
ops.push(op);
|
|
186
|
+
if (notifyInSync) {
|
|
187
|
+
callback(ops.splice(0));
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (!promise) {
|
|
191
|
+
promise = Promise.resolve().then(() => {
|
|
192
|
+
promise = void 0;
|
|
193
|
+
callback(ops.splice(0));
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
proxyObject[LISTENERS].add(listener);
|
|
198
|
+
return () => {
|
|
199
|
+
;
|
|
200
|
+
proxyObject[LISTENERS].delete(listener);
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function snapshot(proxyObject) {
|
|
204
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[SNAPSHOT])) {
|
|
205
|
+
console.warn("Please use proxy object");
|
|
206
|
+
}
|
|
207
|
+
return proxyObject[SNAPSHOT];
|
|
208
|
+
}
|
|
209
|
+
function getHandler(proxyObject) {
|
|
210
|
+
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[HANDLER])) {
|
|
211
|
+
console.warn("Please use proxy object");
|
|
212
|
+
}
|
|
213
|
+
return proxyObject[HANDLER];
|
|
214
|
+
}
|
|
215
|
+
function proxyWithComputed(initialObject, computedFns) {
|
|
216
|
+
;
|
|
217
|
+
Object.keys(computedFns).forEach((key) => {
|
|
218
|
+
if (Object.getOwnPropertyDescriptor(initialObject, key)) {
|
|
219
|
+
throw new Error("object property already defined");
|
|
220
|
+
}
|
|
221
|
+
const computedFn = computedFns[key];
|
|
222
|
+
const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
|
|
223
|
+
const desc = {};
|
|
224
|
+
desc.get = () => get(snapshot(proxyObject));
|
|
225
|
+
if (set) {
|
|
226
|
+
desc.set = (newValue) => set(proxyObject, newValue);
|
|
227
|
+
}
|
|
228
|
+
Object.defineProperty(initialObject, key, desc);
|
|
229
|
+
});
|
|
230
|
+
const proxyObject = proxy(initialObject);
|
|
231
|
+
return proxyObject;
|
|
232
|
+
}
|
|
233
|
+
export {
|
|
234
|
+
getHandler,
|
|
235
|
+
getVersion,
|
|
236
|
+
proxy,
|
|
237
|
+
proxyWithComputed,
|
|
238
|
+
ref,
|
|
239
|
+
snapshot,
|
|
240
|
+
subscribe
|
|
241
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zag-js/store",
|
|
3
|
+
"version": "0.0.0-dev-20220625180418",
|
|
4
|
+
"description": "The reactive store package for zag machines",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"js",
|
|
7
|
+
"utils",
|
|
8
|
+
"store",
|
|
9
|
+
"reactivity"
|
|
10
|
+
],
|
|
11
|
+
"author": "Segun Adebayo <sage@adebayosegun.com>",
|
|
12
|
+
"homepage": "https://github.com/chakra-ui/zag#readme",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"module": "dist/index.mjs",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/store",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/**/*"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build:fast": "yarn zag build",
|
|
24
|
+
"start": "yarn zag build --watch",
|
|
25
|
+
"build": "yarn zag build --prod",
|
|
26
|
+
"test": "jest --config ../../../jest.config.js --rootDir tests",
|
|
27
|
+
"lint": "eslint src --ext .ts,.tsx",
|
|
28
|
+
"test:ci": "yarn test --ci --runInBand --updateSnapshot",
|
|
29
|
+
"test:watch": "yarn test --watchAll"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/chakra-ui/zag/issues"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"proxy-compare": "2.2.0"
|
|
39
|
+
}
|
|
40
|
+
}
|