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