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