@zag-js/store 0.78.1 → 0.78.3

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.mts CHANGED
@@ -1,18 +1,16 @@
1
- declare function makeGlobal<T>(key: string, value: () => T): T;
1
+ declare function clone<T>(x: T): T;
2
+
3
+ declare function globalRef<T>(key: string, value: () => T): T;
2
4
 
3
5
  type AsRef = {
4
6
  $$valtioRef: true;
5
7
  };
6
8
  type Path = (string | symbol)[];
7
- 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];
8
- type AnyFunction = (...args: any[]) => any;
9
- type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<any> ? Awaited<T> : {
10
- readonly [K in keyof T]: Snapshot<T[K]>;
11
- };
12
- type HandlePromise = <P extends Promise<any>>(promise: P) => Awaited<P>;
9
+ type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown];
10
+ type Snapshot<T> = T;
13
11
  declare function proxy<T extends object>(initialObject?: T): T;
14
12
  declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void;
15
- declare function snapshot<T extends object>(proxyObject: T, handlePromise?: HandlePromise): Snapshot<T>;
13
+ declare function snapshot<T extends object>(proxyObject: T): T;
16
14
  declare function ref<T extends object>(obj: T): Ref<T>;
17
15
  type Ref<T> = T & AsRef;
18
16
 
@@ -23,4 +21,4 @@ declare function proxyWithComputed<T extends object, U extends object>(initialOb
23
21
  };
24
22
  }): T & U;
25
23
 
26
- export { type Ref, type Snapshot, makeGlobal, proxy, proxyWithComputed, ref, snapshot, subscribe };
24
+ export { type Ref, type Snapshot, clone, globalRef, proxy, proxyWithComputed, ref, snapshot, subscribe };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,16 @@
1
- declare function makeGlobal<T>(key: string, value: () => T): T;
1
+ declare function clone<T>(x: T): T;
2
+
3
+ declare function globalRef<T>(key: string, value: () => T): T;
2
4
 
3
5
  type AsRef = {
4
6
  $$valtioRef: true;
5
7
  };
6
8
  type Path = (string | symbol)[];
7
- 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];
8
- type AnyFunction = (...args: any[]) => any;
9
- type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<any> ? Awaited<T> : {
10
- readonly [K in keyof T]: Snapshot<T[K]>;
11
- };
12
- type HandlePromise = <P extends Promise<any>>(promise: P) => Awaited<P>;
9
+ type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown];
10
+ type Snapshot<T> = T;
13
11
  declare function proxy<T extends object>(initialObject?: T): T;
14
12
  declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void;
15
- declare function snapshot<T extends object>(proxyObject: T, handlePromise?: HandlePromise): Snapshot<T>;
13
+ declare function snapshot<T extends object>(proxyObject: T): T;
16
14
  declare function ref<T extends object>(obj: T): Ref<T>;
17
15
  type Ref<T> = T & AsRef;
18
16
 
@@ -23,4 +21,4 @@ declare function proxyWithComputed<T extends object, U extends object>(initialOb
23
21
  };
24
22
  }): T & U;
25
23
 
26
- export { type Ref, type Snapshot, makeGlobal, proxy, proxyWithComputed, ref, snapshot, subscribe };
24
+ export { type Ref, type Snapshot, clone, globalRef, proxy, proxyWithComputed, ref, snapshot, subscribe };
package/dist/index.js CHANGED
@@ -3,36 +3,79 @@
3
3
  var proxyCompare = require('proxy-compare');
4
4
 
5
5
  // src/global.ts
6
- function getGlobal() {
6
+ function glob() {
7
7
  if (typeof globalThis !== "undefined") return globalThis;
8
8
  if (typeof self !== "undefined") return self;
9
9
  if (typeof window !== "undefined") return window;
10
10
  if (typeof global !== "undefined") return global;
11
11
  }
12
- function makeGlobal(key, value) {
13
- const g = getGlobal();
12
+ function globalRef(key, value) {
13
+ const g = glob();
14
14
  if (!g) return value();
15
15
  g[key] || (g[key] = value());
16
16
  return g[key];
17
17
  }
18
- var isDev = () => process.env.NODE_ENV !== "production";
19
- var isObject = (x) => typeof x === "object" && x !== null;
20
- var proxyStateMap = makeGlobal("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap());
21
- var refSet = makeGlobal("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
22
- var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x;
18
+ var refSet = globalRef("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
19
+
20
+ // src/utils.ts
21
+ var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x && "props" in x;
23
22
  var isVueElement = (x) => typeof x === "object" && x !== null && "__v_isVNode" in x;
24
23
  var isDOMElement = (x) => typeof x === "object" && x !== null && "nodeType" in x && typeof x.nodeName === "string";
25
24
  var isElement = (x) => isReactElement(x) || isVueElement(x) || isDOMElement(x);
26
- 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)) && !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), defaultHandlePromise = (promise) => {
27
- switch (promise.status) {
28
- case "fulfilled":
29
- return promise.value;
30
- case "rejected":
31
- throw promise.reason;
32
- default:
33
- throw promise;
25
+ var isObject = (x) => x !== null && typeof x === "object";
26
+ 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);
27
+ var isDev = () => process.env.NODE_ENV !== "production";
28
+
29
+ // src/clone.ts
30
+ function set(obj, key, val) {
31
+ if (typeof val.value === "object" && !canProxy(val.value)) val.value = clone(val.value);
32
+ if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
33
+ Object.defineProperty(obj, key, val);
34
+ } else obj[key] = val.value;
35
+ }
36
+ function clone(x) {
37
+ if (typeof x !== "object") return x;
38
+ var i = 0, k, list, tmp, str = Object.prototype.toString.call(x);
39
+ if (str === "[object Object]") {
40
+ tmp = Object.create(Object.getPrototypeOf(x) || null);
41
+ } else if (str === "[object Array]") {
42
+ tmp = Array(x.length);
43
+ } else if (str === "[object Set]") {
44
+ tmp = /* @__PURE__ */ new Set();
45
+ x.forEach(function(val) {
46
+ tmp.add(clone(val));
47
+ });
48
+ } else if (str === "[object Map]") {
49
+ tmp = /* @__PURE__ */ new Map();
50
+ x.forEach(function(val, key) {
51
+ tmp.set(clone(key), clone(val));
52
+ });
53
+ } else if (str === "[object Date]") {
54
+ tmp = /* @__PURE__ */ new Date(+x);
55
+ } else if (str === "[object RegExp]") {
56
+ tmp = new RegExp(x.source, x.flags);
57
+ } else if (str === "[object DataView]") {
58
+ tmp = new x.constructor(clone(x.buffer));
59
+ } else if (str === "[object ArrayBuffer]") {
60
+ tmp = x.slice(0);
61
+ } else if (str === "[object Blob]") {
62
+ tmp = x.slice();
63
+ } else if (str.slice(-6) === "Array]") {
64
+ tmp = new x.constructor(x);
65
+ }
66
+ if (tmp) {
67
+ for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
68
+ set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
69
+ }
70
+ for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
71
+ if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
72
+ set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
73
+ }
34
74
  }
35
- }, snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version, handlePromise = defaultHandlePromise) => {
75
+ return tmp || x;
76
+ }
77
+ var proxyStateMap = globalRef("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap());
78
+ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version) => {
36
79
  const cache = snapCache.get(target);
37
80
  if (cache?.[0] === version) {
38
81
  return cache[1];
@@ -45,14 +88,8 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
45
88
  if (refSet.has(value)) {
46
89
  proxyCompare.markToTrack(value, false);
47
90
  snap[key] = value;
48
- } else if (value instanceof Promise) {
49
- Object.defineProperty(snap, key, {
50
- get() {
51
- return handlePromise(value);
52
- }
53
- });
54
91
  } else if (proxyStateMap.has(value)) {
55
- snap[key] = snapshot(value, handlePromise);
92
+ snap[key] = snapshot(value);
56
93
  } else {
57
94
  snap[key] = value;
58
95
  }
@@ -157,15 +194,7 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
157
194
  value = proxyCompare.getUntracked(value) || value;
158
195
  }
159
196
  let nextValue = value;
160
- if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else if (value instanceof Promise) {
161
- value.then((v) => {
162
- Object.assign(value, { status: "fulfilled", value: v });
163
- notifyUpdate(["resolve", [prop], v]);
164
- }).catch((e) => {
165
- Object.assign(value, { status: "rejected", reason: e });
166
- notifyUpdate(["reject", [prop], e]);
167
- });
168
- } else {
197
+ if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else {
169
198
  if (!proxyStateMap.has(value) && canProxy(value)) {
170
199
  nextValue = proxy(value);
171
200
  }
@@ -202,7 +231,6 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
202
231
  objectIs,
203
232
  newProxy,
204
233
  canProxy,
205
- defaultHandlePromise,
206
234
  snapCache,
207
235
  createSnapshot,
208
236
  proxyCache,
@@ -243,13 +271,13 @@ function subscribe(proxyObject, callback, notifyInSync) {
243
271
  removeListener();
244
272
  };
245
273
  }
246
- function snapshot(proxyObject, handlePromise) {
274
+ function snapshot(proxyObject) {
247
275
  const proxyState = proxyStateMap.get(proxyObject);
248
276
  if (isDev() && !proxyState) {
249
277
  console.warn("Please use proxy object");
250
278
  }
251
279
  const [target, ensureVersion, createSnapshot] = proxyState;
252
- return createSnapshot(target, ensureVersion(), handlePromise);
280
+ return createSnapshot(target, ensureVersion());
253
281
  }
254
282
  function ref(obj) {
255
283
  refSet.add(obj);
@@ -264,11 +292,11 @@ function proxyWithComputed(initialObject, computedFns) {
264
292
  throw new Error("object property already defined");
265
293
  }
266
294
  const computedFn = computedFns[key];
267
- const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
295
+ const { get, set: set2 } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
268
296
  const desc = {};
269
297
  desc.get = () => get(snapshot(proxyObject));
270
- if (set) {
271
- desc.set = (newValue) => set(proxyObject, newValue);
298
+ if (set2) {
299
+ desc.set = (newValue) => set2(proxyObject, newValue);
272
300
  }
273
301
  Object.defineProperty(initialObject, key, desc);
274
302
  });
@@ -276,7 +304,8 @@ function proxyWithComputed(initialObject, computedFns) {
276
304
  return proxyObject;
277
305
  }
278
306
 
279
- exports.makeGlobal = makeGlobal;
307
+ exports.clone = clone;
308
+ exports.globalRef = globalRef;
280
309
  exports.proxy = proxy;
281
310
  exports.proxyWithComputed = proxyWithComputed;
282
311
  exports.ref = ref;
package/dist/index.mjs CHANGED
@@ -1,36 +1,79 @@
1
1
  import { markToTrack, getUntracked } from 'proxy-compare';
2
2
 
3
3
  // src/global.ts
4
- function getGlobal() {
4
+ function glob() {
5
5
  if (typeof globalThis !== "undefined") return globalThis;
6
6
  if (typeof self !== "undefined") return self;
7
7
  if (typeof window !== "undefined") return window;
8
8
  if (typeof global !== "undefined") return global;
9
9
  }
10
- function makeGlobal(key, value) {
11
- const g = getGlobal();
10
+ function globalRef(key, value) {
11
+ const g = glob();
12
12
  if (!g) return value();
13
13
  g[key] || (g[key] = value());
14
14
  return g[key];
15
15
  }
16
- var isDev = () => process.env.NODE_ENV !== "production";
17
- var isObject = (x) => typeof x === "object" && x !== null;
18
- var proxyStateMap = makeGlobal("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap());
19
- var refSet = makeGlobal("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
20
- var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x;
16
+ var refSet = globalRef("__zag__refSet", () => /* @__PURE__ */ new WeakSet());
17
+
18
+ // src/utils.ts
19
+ var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x && "props" in x;
21
20
  var isVueElement = (x) => typeof x === "object" && x !== null && "__v_isVNode" in x;
22
21
  var isDOMElement = (x) => typeof x === "object" && x !== null && "nodeType" in x && typeof x.nodeName === "string";
23
22
  var isElement = (x) => isReactElement(x) || isVueElement(x) || isDOMElement(x);
24
- 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)) && !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), defaultHandlePromise = (promise) => {
25
- switch (promise.status) {
26
- case "fulfilled":
27
- return promise.value;
28
- case "rejected":
29
- throw promise.reason;
30
- default:
31
- throw promise;
23
+ var isObject = (x) => x !== null && typeof x === "object";
24
+ 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);
25
+ var isDev = () => process.env.NODE_ENV !== "production";
26
+
27
+ // src/clone.ts
28
+ function set(obj, key, val) {
29
+ if (typeof val.value === "object" && !canProxy(val.value)) val.value = clone(val.value);
30
+ if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") {
31
+ Object.defineProperty(obj, key, val);
32
+ } else obj[key] = val.value;
33
+ }
34
+ function clone(x) {
35
+ if (typeof x !== "object") return x;
36
+ var i = 0, k, list, tmp, str = Object.prototype.toString.call(x);
37
+ if (str === "[object Object]") {
38
+ tmp = Object.create(Object.getPrototypeOf(x) || null);
39
+ } else if (str === "[object Array]") {
40
+ tmp = Array(x.length);
41
+ } else if (str === "[object Set]") {
42
+ tmp = /* @__PURE__ */ new Set();
43
+ x.forEach(function(val) {
44
+ tmp.add(clone(val));
45
+ });
46
+ } else if (str === "[object Map]") {
47
+ tmp = /* @__PURE__ */ new Map();
48
+ x.forEach(function(val, key) {
49
+ tmp.set(clone(key), clone(val));
50
+ });
51
+ } else if (str === "[object Date]") {
52
+ tmp = /* @__PURE__ */ new Date(+x);
53
+ } else if (str === "[object RegExp]") {
54
+ tmp = new RegExp(x.source, x.flags);
55
+ } else if (str === "[object DataView]") {
56
+ tmp = new x.constructor(clone(x.buffer));
57
+ } else if (str === "[object ArrayBuffer]") {
58
+ tmp = x.slice(0);
59
+ } else if (str === "[object Blob]") {
60
+ tmp = x.slice();
61
+ } else if (str.slice(-6) === "Array]") {
62
+ tmp = new x.constructor(x);
63
+ }
64
+ if (tmp) {
65
+ for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) {
66
+ set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i]));
67
+ }
68
+ for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) {
69
+ if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue;
70
+ set(tmp, k, Object.getOwnPropertyDescriptor(x, k));
71
+ }
32
72
  }
33
- }, snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version, handlePromise = defaultHandlePromise) => {
73
+ return tmp || x;
74
+ }
75
+ var proxyStateMap = globalRef("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap());
76
+ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version) => {
34
77
  const cache = snapCache.get(target);
35
78
  if (cache?.[0] === version) {
36
79
  return cache[1];
@@ -43,14 +86,8 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
43
86
  if (refSet.has(value)) {
44
87
  markToTrack(value, false);
45
88
  snap[key] = value;
46
- } else if (value instanceof Promise) {
47
- Object.defineProperty(snap, key, {
48
- get() {
49
- return handlePromise(value);
50
- }
51
- });
52
89
  } else if (proxyStateMap.has(value)) {
53
- snap[key] = snapshot(value, handlePromise);
90
+ snap[key] = snapshot(value);
54
91
  } else {
55
92
  snap[key] = value;
56
93
  }
@@ -155,15 +192,7 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
155
192
  value = getUntracked(value) || value;
156
193
  }
157
194
  let nextValue = value;
158
- if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else if (value instanceof Promise) {
159
- value.then((v) => {
160
- Object.assign(value, { status: "fulfilled", value: v });
161
- notifyUpdate(["resolve", [prop], v]);
162
- }).catch((e) => {
163
- Object.assign(value, { status: "rejected", reason: e });
164
- notifyUpdate(["reject", [prop], e]);
165
- });
166
- } else {
195
+ if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else {
167
196
  if (!proxyStateMap.has(value) && canProxy(value)) {
168
197
  nextValue = proxy(value);
169
198
  }
@@ -200,7 +229,6 @@ var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) =>
200
229
  objectIs,
201
230
  newProxy,
202
231
  canProxy,
203
- defaultHandlePromise,
204
232
  snapCache,
205
233
  createSnapshot,
206
234
  proxyCache,
@@ -241,13 +269,13 @@ function subscribe(proxyObject, callback, notifyInSync) {
241
269
  removeListener();
242
270
  };
243
271
  }
244
- function snapshot(proxyObject, handlePromise) {
272
+ function snapshot(proxyObject) {
245
273
  const proxyState = proxyStateMap.get(proxyObject);
246
274
  if (isDev() && !proxyState) {
247
275
  console.warn("Please use proxy object");
248
276
  }
249
277
  const [target, ensureVersion, createSnapshot] = proxyState;
250
- return createSnapshot(target, ensureVersion(), handlePromise);
278
+ return createSnapshot(target, ensureVersion());
251
279
  }
252
280
  function ref(obj) {
253
281
  refSet.add(obj);
@@ -262,11 +290,11 @@ function proxyWithComputed(initialObject, computedFns) {
262
290
  throw new Error("object property already defined");
263
291
  }
264
292
  const computedFn = computedFns[key];
265
- const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
293
+ const { get, set: set2 } = typeof computedFn === "function" ? { get: computedFn } : computedFn;
266
294
  const desc = {};
267
295
  desc.get = () => get(snapshot(proxyObject));
268
- if (set) {
269
- desc.set = (newValue) => set(proxyObject, newValue);
296
+ if (set2) {
297
+ desc.set = (newValue) => set2(proxyObject, newValue);
270
298
  }
271
299
  Object.defineProperty(initialObject, key, desc);
272
300
  });
@@ -274,4 +302,4 @@ function proxyWithComputed(initialObject, computedFns) {
274
302
  return proxyObject;
275
303
  }
276
304
 
277
- export { makeGlobal, proxy, proxyWithComputed, ref, snapshot, subscribe };
305
+ export { clone, globalRef, proxy, proxyWithComputed, ref, snapshot, subscribe };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/store",
3
- "version": "0.78.1",
3
+ "version": "0.78.3",
4
4
  "description": "The reactive store package for zag machines",
5
5
  "keywords": [
6
6
  "js",