@zag-js/store
Advanced tools
Comparing version 0.78.2 to 0.78.3
@@ -1,3 +0,5 @@ | ||
declare function makeGlobal<T>(key: string, value: () => T): T; | ||
declare function clone<T>(x: T): T; | ||
declare function globalRef<T>(key: string, value: () => T): T; | ||
type AsRef = { | ||
@@ -7,11 +9,7 @@ $$valtioRef: true; | ||
type Path = (string | symbol)[]; | ||
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]; | ||
type AnyFunction = (...args: any[]) => any; | ||
type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<any> ? Awaited<T> : { | ||
readonly [K in keyof T]: Snapshot<T[K]>; | ||
}; | ||
type HandlePromise = <P extends Promise<any>>(promise: P) => Awaited<P>; | ||
type Op = [op: "set", path: Path, value: unknown, prevValue: unknown] | [op: "delete", path: Path, prevValue: unknown]; | ||
type Snapshot<T> = T; | ||
declare function proxy<T extends object>(initialObject?: T): T; | ||
declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void; | ||
declare function snapshot<T extends object>(proxyObject: T, handlePromise?: HandlePromise): Snapshot<T>; | ||
declare function snapshot<T extends object>(proxyObject: T): T; | ||
declare function ref<T extends object>(obj: T): Ref<T>; | ||
@@ -27,2 +25,2 @@ type Ref<T> = T & AsRef; | ||
export { type Ref, type Snapshot, makeGlobal, proxy, proxyWithComputed, ref, snapshot, subscribe }; | ||
export { type Ref, type Snapshot, clone, globalRef, proxy, proxyWithComputed, ref, snapshot, subscribe }; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
// src/global.ts | ||
function getGlobal() { | ||
function glob() { | ||
if (typeof globalThis !== "undefined") return globalThis; | ||
@@ -13,4 +13,4 @@ if (typeof self !== "undefined") return self; | ||
} | ||
function makeGlobal(key, value) { | ||
const g = getGlobal(); | ||
function globalRef(key, value) { | ||
const g = glob(); | ||
if (!g) return value(); | ||
@@ -20,20 +20,63 @@ g[key] || (g[key] = value()); | ||
} | ||
var isDev = () => process.env.NODE_ENV !== "production"; | ||
var isObject = (x) => typeof x === "object" && x !== null; | ||
var proxyStateMap = makeGlobal("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap()); | ||
var refSet = makeGlobal("__zag__refSet", () => /* @__PURE__ */ new WeakSet()); | ||
var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x; | ||
var refSet = globalRef("__zag__refSet", () => /* @__PURE__ */ new WeakSet()); | ||
// src/utils.ts | ||
var isReactElement = (x) => typeof x === "object" && x !== null && "$$typeof" in x && "props" in x; | ||
var isVueElement = (x) => typeof x === "object" && x !== null && "__v_isVNode" in x; | ||
var isDOMElement = (x) => typeof x === "object" && x !== null && "nodeType" in x && typeof x.nodeName === "string"; | ||
var isElement = (x) => isReactElement(x) || isVueElement(x) || isDOMElement(x); | ||
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) => { | ||
switch (promise.status) { | ||
case "fulfilled": | ||
return promise.value; | ||
case "rejected": | ||
throw promise.reason; | ||
default: | ||
throw promise; | ||
var isObject = (x) => x !== null && typeof x === "object"; | ||
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); | ||
var isDev = () => process.env.NODE_ENV !== "production"; | ||
// src/clone.ts | ||
function set(obj, key, val) { | ||
if (typeof val.value === "object" && !canProxy(val.value)) val.value = clone(val.value); | ||
if (!val.enumerable || val.get || val.set || !val.configurable || !val.writable || key === "__proto__") { | ||
Object.defineProperty(obj, key, val); | ||
} else obj[key] = val.value; | ||
} | ||
function clone(x) { | ||
if (typeof x !== "object") return x; | ||
var i = 0, k, list, tmp, str = Object.prototype.toString.call(x); | ||
if (str === "[object Object]") { | ||
tmp = Object.create(Object.getPrototypeOf(x) || null); | ||
} else if (str === "[object Array]") { | ||
tmp = Array(x.length); | ||
} else if (str === "[object Set]") { | ||
tmp = /* @__PURE__ */ new Set(); | ||
x.forEach(function(val) { | ||
tmp.add(clone(val)); | ||
}); | ||
} else if (str === "[object Map]") { | ||
tmp = /* @__PURE__ */ new Map(); | ||
x.forEach(function(val, key) { | ||
tmp.set(clone(key), clone(val)); | ||
}); | ||
} else if (str === "[object Date]") { | ||
tmp = /* @__PURE__ */ new Date(+x); | ||
} else if (str === "[object RegExp]") { | ||
tmp = new RegExp(x.source, x.flags); | ||
} else if (str === "[object DataView]") { | ||
tmp = new x.constructor(clone(x.buffer)); | ||
} else if (str === "[object ArrayBuffer]") { | ||
tmp = x.slice(0); | ||
} else if (str === "[object Blob]") { | ||
tmp = x.slice(); | ||
} else if (str.slice(-6) === "Array]") { | ||
tmp = new x.constructor(x); | ||
} | ||
}, snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version, handlePromise = defaultHandlePromise) => { | ||
if (tmp) { | ||
for (list = Object.getOwnPropertySymbols(x); i < list.length; i++) { | ||
set(tmp, list[i], Object.getOwnPropertyDescriptor(x, list[i])); | ||
} | ||
for (i = 0, list = Object.getOwnPropertyNames(x); i < list.length; i++) { | ||
if (Object.hasOwnProperty.call(tmp, k = list[i]) && tmp[k] === x[k]) continue; | ||
set(tmp, k, Object.getOwnPropertyDescriptor(x, k)); | ||
} | ||
} | ||
return tmp || x; | ||
} | ||
var proxyStateMap = globalRef("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap()); | ||
var buildProxyFunction = (objectIs = Object.is, newProxy = (target, handler) => new Proxy(target, handler), snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version) => { | ||
const cache = snapCache.get(target); | ||
@@ -51,10 +94,4 @@ if (cache?.[0] === version) { | ||
snap[key] = value; | ||
} else if (value instanceof Promise) { | ||
Object.defineProperty(snap, key, { | ||
get() { | ||
return handlePromise(value); | ||
} | ||
}); | ||
} else if (proxyStateMap.has(value)) { | ||
snap[key] = snapshot(value, handlePromise); | ||
snap[key] = snapshot(value); | ||
} else { | ||
@@ -164,11 +201,3 @@ snap[key] = value; | ||
let nextValue = value; | ||
if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else if (value instanceof Promise) { | ||
value.then((v) => { | ||
Object.assign(value, { status: "fulfilled", value: v }); | ||
notifyUpdate(["resolve", [prop], v]); | ||
}).catch((e) => { | ||
Object.assign(value, { status: "rejected", reason: e }); | ||
notifyUpdate(["reject", [prop], e]); | ||
}); | ||
} else { | ||
if (Object.getOwnPropertyDescriptor(target, prop)?.set) ; else { | ||
if (!proxyStateMap.has(value) && canProxy(value)) { | ||
@@ -210,3 +239,2 @@ nextValue = proxy(value); | ||
canProxy, | ||
defaultHandlePromise, | ||
snapCache, | ||
@@ -252,3 +280,3 @@ createSnapshot, | ||
} | ||
function snapshot(proxyObject, handlePromise) { | ||
function snapshot(proxyObject) { | ||
const proxyState = proxyStateMap.get(proxyObject); | ||
@@ -259,3 +287,3 @@ if (isDev() && !proxyState) { | ||
const [target, ensureVersion, createSnapshot] = proxyState; | ||
return createSnapshot(target, ensureVersion(), handlePromise); | ||
return createSnapshot(target, ensureVersion()); | ||
} | ||
@@ -275,7 +303,7 @@ function ref(obj) { | ||
const computedFn = computedFns[key]; | ||
const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn; | ||
const { get, set: set2 } = typeof computedFn === "function" ? { get: computedFn } : computedFn; | ||
const desc = {}; | ||
desc.get = () => get(snapshot(proxyObject)); | ||
if (set) { | ||
desc.set = (newValue) => set(proxyObject, newValue); | ||
if (set2) { | ||
desc.set = (newValue) => set2(proxyObject, newValue); | ||
} | ||
@@ -288,3 +316,4 @@ Object.defineProperty(initialObject, key, desc); | ||
exports.makeGlobal = makeGlobal; | ||
exports.clone = clone; | ||
exports.globalRef = globalRef; | ||
exports.proxy = proxy; | ||
@@ -291,0 +320,0 @@ exports.proxyWithComputed = proxyWithComputed; |
{ | ||
"name": "@zag-js/store", | ||
"version": "0.78.2", | ||
"version": "0.78.3", | ||
"description": "The reactive store package for zag machines", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26305
629