@zag-js/store 1.46.0 → 1.48.0
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/clone.d.mts +1 -3
- package/dist/clone.d.ts +1 -3
- package/dist/clone.js +37 -72
- package/dist/clone.mjs +33 -47
- package/dist/global.d.mts +2 -4
- package/dist/global.d.ts +2 -4
- package/dist/global.js +15 -39
- package/dist/global.mjs +10 -13
- package/dist/index.d.mts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +14 -44
- package/dist/index.mjs +1 -10
- package/dist/proxy-computed.d.mts +5 -10
- package/dist/proxy-computed.d.ts +5 -10
- package/dist/proxy-computed.js +17 -45
- package/dist/proxy-computed.mjs +13 -20
- package/dist/proxy.d.mts +9 -10
- package/dist/proxy.d.ts +9 -10
- package/dist/proxy.js +180 -241
- package/dist/proxy.mjs +171 -211
- package/dist/utils.d.mts +3 -5
- package/dist/utils.d.ts +3 -5
- package/dist/utils.js +15 -40
- package/dist/utils.mjs +9 -13
- package/package.json +3 -3
package/dist/proxy.js
CHANGED
|
@@ -1,254 +1,193 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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/proxy.ts
|
|
21
|
-
var proxy_exports = {};
|
|
22
|
-
__export(proxy_exports, {
|
|
23
|
-
getVersion: () => getVersion,
|
|
24
|
-
proxy: () => proxy,
|
|
25
|
-
ref: () => ref,
|
|
26
|
-
snapshot: () => snapshot,
|
|
27
|
-
subscribe: () => subscribe
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
28
4
|
});
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (childProxyState) {
|
|
160
|
-
addPropListener(prop, childProxyState);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
Reflect.set(target, prop, nextValue, receiver);
|
|
164
|
-
notifyUpdate(["set", [prop], value, prevValue]);
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
const proxyObject = newProxy(baseObject, handler);
|
|
169
|
-
proxyCache.set(initialObject, proxyObject);
|
|
170
|
-
const proxyState = [baseObject, ensureVersion, createSnapshot, addListener];
|
|
171
|
-
proxyStateMap.set(proxyObject, proxyState);
|
|
172
|
-
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
173
|
-
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
174
|
-
if (desc.get || desc.set) {
|
|
175
|
-
Object.defineProperty(baseObject, key, desc);
|
|
176
|
-
} else {
|
|
177
|
-
proxyObject[key] = initialObject[key];
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
return proxyObject;
|
|
5
|
+
const require_global = require("./global.js");
|
|
6
|
+
const require_utils = require("./utils.js");
|
|
7
|
+
let proxy_compare = require("proxy-compare");
|
|
8
|
+
// src/proxy.ts
|
|
9
|
+
const proxyStateMap = require_global.globalRef("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap());
|
|
10
|
+
const buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version) => {
|
|
11
|
+
const cache = snapCache.get(target);
|
|
12
|
+
if (cache?.[0] === version) return cache[1];
|
|
13
|
+
const snap = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target));
|
|
14
|
+
(0, proxy_compare.markToTrack)(snap, true);
|
|
15
|
+
snapCache.set(target, [version, snap]);
|
|
16
|
+
Reflect.ownKeys(target).forEach((key) => {
|
|
17
|
+
const value = Reflect.get(target, key);
|
|
18
|
+
if (require_global.refSet.has(value)) {
|
|
19
|
+
(0, proxy_compare.markToTrack)(value, false);
|
|
20
|
+
snap[key] = value;
|
|
21
|
+
} else if (proxyStateMap.has(value)) snap[key] = snapshot(value);
|
|
22
|
+
else snap[key] = value;
|
|
23
|
+
});
|
|
24
|
+
return Object.freeze(snap);
|
|
25
|
+
}, proxyCache = /* @__PURE__ */ new WeakMap(), versionHolder = [1, 1], proxyFunction = (initialObject) => {
|
|
26
|
+
if (!require_utils.isObject(initialObject)) throw new Error("object required");
|
|
27
|
+
const found = proxyCache.get(initialObject);
|
|
28
|
+
if (found) return found;
|
|
29
|
+
let version = versionHolder[0];
|
|
30
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
31
|
+
const notifyUpdate = (op, nextVersion = ++versionHolder[0]) => {
|
|
32
|
+
if (version !== nextVersion) {
|
|
33
|
+
version = nextVersion;
|
|
34
|
+
listeners.forEach((listener) => listener(op, nextVersion));
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
let checkVersion = versionHolder[1];
|
|
38
|
+
const ensureVersion = (nextCheckVersion = ++versionHolder[1]) => {
|
|
39
|
+
if (checkVersion !== nextCheckVersion && !listeners.size) {
|
|
40
|
+
checkVersion = nextCheckVersion;
|
|
41
|
+
propProxyStates.forEach(([propProxyState]) => {
|
|
42
|
+
const propVersion = propProxyState[1](nextCheckVersion);
|
|
43
|
+
if (propVersion > version) version = propVersion;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return version;
|
|
47
|
+
};
|
|
48
|
+
const createPropListener = (prop) => (op, nextVersion) => {
|
|
49
|
+
const newOp = [...op];
|
|
50
|
+
newOp[1] = [prop, ...newOp[1]];
|
|
51
|
+
notifyUpdate(newOp, nextVersion);
|
|
52
|
+
};
|
|
53
|
+
const propProxyStates = /* @__PURE__ */ new Map();
|
|
54
|
+
const addPropListener = (prop, propProxyState) => {
|
|
55
|
+
if (require_utils.isDev() && propProxyStates.has(prop)) throw new Error("prop listener already exists");
|
|
56
|
+
if (listeners.size) {
|
|
57
|
+
const remove = propProxyState[3](createPropListener(prop));
|
|
58
|
+
propProxyStates.set(prop, [propProxyState, remove]);
|
|
59
|
+
} else propProxyStates.set(prop, [propProxyState]);
|
|
60
|
+
};
|
|
61
|
+
const removePropListener = (prop) => {
|
|
62
|
+
const entry = propProxyStates.get(prop);
|
|
63
|
+
if (entry) {
|
|
64
|
+
propProxyStates.delete(prop);
|
|
65
|
+
entry[1]?.();
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const addListener = (listener) => {
|
|
69
|
+
listeners.add(listener);
|
|
70
|
+
if (listeners.size === 1) propProxyStates.forEach(([propProxyState, prevRemove], prop) => {
|
|
71
|
+
if (require_utils.isDev() && prevRemove) throw new Error("remove already exists");
|
|
72
|
+
const remove = propProxyState[3](createPropListener(prop));
|
|
73
|
+
propProxyStates.set(prop, [propProxyState, remove]);
|
|
74
|
+
});
|
|
75
|
+
const removeListener = () => {
|
|
76
|
+
listeners.delete(listener);
|
|
77
|
+
if (listeners.size === 0) propProxyStates.forEach(([propProxyState, remove], prop) => {
|
|
78
|
+
if (remove) {
|
|
79
|
+
remove();
|
|
80
|
+
propProxyStates.set(prop, [propProxyState]);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
return removeListener;
|
|
85
|
+
};
|
|
86
|
+
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
|
|
87
|
+
const proxyObject = newProxy(baseObject, {
|
|
88
|
+
deleteProperty(target, prop) {
|
|
89
|
+
const prevValue = Reflect.get(target, prop);
|
|
90
|
+
removePropListener(prop);
|
|
91
|
+
const deleted = Reflect.deleteProperty(target, prop);
|
|
92
|
+
if (deleted) notifyUpdate([
|
|
93
|
+
"delete",
|
|
94
|
+
[prop],
|
|
95
|
+
prevValue
|
|
96
|
+
]);
|
|
97
|
+
return deleted;
|
|
98
|
+
},
|
|
99
|
+
set(target, prop, value, receiver) {
|
|
100
|
+
const hasPrevValue = Reflect.has(target, prop);
|
|
101
|
+
const prevValue = Reflect.get(target, prop, receiver);
|
|
102
|
+
if (hasPrevValue && (objectIs(prevValue, value) || proxyCache.has(value) && objectIs(prevValue, proxyCache.get(value)))) return true;
|
|
103
|
+
removePropListener(prop);
|
|
104
|
+
if (require_utils.isObject(value)) value = (0, proxy_compare.getUntracked)(value) || value;
|
|
105
|
+
let nextValue = value;
|
|
106
|
+
if (Object.getOwnPropertyDescriptor(target, prop)?.set) {} else {
|
|
107
|
+
if (!proxyStateMap.has(value) && require_utils.canProxy(value)) nextValue = proxy(value);
|
|
108
|
+
const childProxyState = !require_global.refSet.has(nextValue) && proxyStateMap.get(nextValue);
|
|
109
|
+
if (childProxyState) addPropListener(prop, childProxyState);
|
|
110
|
+
}
|
|
111
|
+
Reflect.set(target, prop, nextValue, receiver);
|
|
112
|
+
notifyUpdate([
|
|
113
|
+
"set",
|
|
114
|
+
[prop],
|
|
115
|
+
value,
|
|
116
|
+
prevValue
|
|
117
|
+
]);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
proxyCache.set(initialObject, proxyObject);
|
|
122
|
+
const proxyState = [
|
|
123
|
+
baseObject,
|
|
124
|
+
ensureVersion,
|
|
125
|
+
createSnapshot,
|
|
126
|
+
addListener
|
|
127
|
+
];
|
|
128
|
+
proxyStateMap.set(proxyObject, proxyState);
|
|
129
|
+
Reflect.ownKeys(initialObject).forEach((key) => {
|
|
130
|
+
const desc = Object.getOwnPropertyDescriptor(initialObject, key);
|
|
131
|
+
if (desc.get || desc.set) Object.defineProperty(baseObject, key, desc);
|
|
132
|
+
else proxyObject[key] = initialObject[key];
|
|
133
|
+
});
|
|
134
|
+
return proxyObject;
|
|
181
135
|
}) => [
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
createSnapshot,
|
|
193
|
-
proxyCache,
|
|
194
|
-
versionHolder
|
|
136
|
+
proxyFunction,
|
|
137
|
+
proxyStateMap,
|
|
138
|
+
require_global.refSet,
|
|
139
|
+
objectIs,
|
|
140
|
+
newProxy,
|
|
141
|
+
require_utils.canProxy,
|
|
142
|
+
snapCache,
|
|
143
|
+
createSnapshot,
|
|
144
|
+
proxyCache,
|
|
145
|
+
versionHolder
|
|
195
146
|
];
|
|
196
|
-
|
|
147
|
+
const [proxyFunction] = buildProxyFunction();
|
|
197
148
|
function proxy(initialObject = {}) {
|
|
198
|
-
|
|
149
|
+
return proxyFunction(initialObject);
|
|
199
150
|
}
|
|
200
151
|
function getVersion(proxyObject) {
|
|
201
|
-
|
|
202
|
-
return proxyState?.[1]();
|
|
152
|
+
return proxyStateMap.get(proxyObject)?.[1]();
|
|
203
153
|
}
|
|
204
154
|
function subscribe(proxyObject, callback, notifyInSync) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const removeListener = addListener(listener);
|
|
229
|
-
isListenerActive = true;
|
|
230
|
-
return () => {
|
|
231
|
-
isListenerActive = false;
|
|
232
|
-
removeListener();
|
|
233
|
-
};
|
|
155
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
156
|
+
if (require_utils.isDev() && !proxyState) console.warn("Please use proxy object");
|
|
157
|
+
let promise;
|
|
158
|
+
const ops = [];
|
|
159
|
+
const addListener = proxyState[3];
|
|
160
|
+
let isListenerActive = false;
|
|
161
|
+
const listener = (op) => {
|
|
162
|
+
ops.push(op);
|
|
163
|
+
if (notifyInSync) {
|
|
164
|
+
callback(ops.splice(0));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (!promise) promise = Promise.resolve().then(() => {
|
|
168
|
+
promise = void 0;
|
|
169
|
+
if (isListenerActive) callback(ops.splice(0));
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
const removeListener = addListener(listener);
|
|
173
|
+
isListenerActive = true;
|
|
174
|
+
return () => {
|
|
175
|
+
isListenerActive = false;
|
|
176
|
+
removeListener();
|
|
177
|
+
};
|
|
234
178
|
}
|
|
235
179
|
function snapshot(proxyObject) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
241
|
-
return createSnapshot(target, ensureVersion());
|
|
180
|
+
const proxyState = proxyStateMap.get(proxyObject);
|
|
181
|
+
if (require_utils.isDev() && !proxyState) console.warn("Please use proxy object");
|
|
182
|
+
const [target, ensureVersion, createSnapshot] = proxyState;
|
|
183
|
+
return createSnapshot(target, ensureVersion());
|
|
242
184
|
}
|
|
243
185
|
function ref(obj) {
|
|
244
|
-
|
|
245
|
-
|
|
186
|
+
require_global.refSet.add(obj);
|
|
187
|
+
return obj;
|
|
246
188
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
snapshot,
|
|
253
|
-
subscribe
|
|
254
|
-
});
|
|
189
|
+
exports.getVersion = getVersion;
|
|
190
|
+
exports.proxy = proxy;
|
|
191
|
+
exports.ref = ref;
|
|
192
|
+
exports.snapshot = snapshot;
|
|
193
|
+
exports.subscribe = subscribe;
|