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