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