@zag-js/store
Advanced tools
Comparing version 0.0.0-dev-20230116072304 to 0.0.0-dev-20230127074732
@@ -1,23 +0,2 @@ | ||
interface AsRef { | ||
$$valtioRef: true; | ||
} | ||
declare function ref<T extends object>(o: T): T & AsRef; | ||
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]; | ||
declare function proxy<T extends object>(initialObject?: T): T; | ||
declare function getVersion(proxyObject: unknown): number | undefined; | ||
declare function subscribe<T extends object>(proxyObject: T, callback: (ops: Op[]) => void, notifyInSync?: boolean): () => void; | ||
type AnyFunction = (...args: any[]) => any; | ||
type Snapshot<T> = T extends AnyFunction ? T : T extends AsRef ? T : T extends Promise<infer V> ? Snapshot<V> : { | ||
readonly [K in keyof T]: Snapshot<T[K]>; | ||
}; | ||
declare function snapshot<T extends object>(proxyObject: T): Snapshot<T>; | ||
declare function getHandler<T extends object>(proxyObject: T): any; | ||
declare function proxyWithComputed<T extends object, U extends object>(initialObject: T, computedFns: { | ||
[K in keyof U]: ((snap: Snapshot<T>) => U[K]) | { | ||
get: (snap: Snapshot<T>) => U[K]; | ||
set?: (state: T, newValue: U[K]) => void; | ||
}; | ||
}): T & U; | ||
export { getHandler, getVersion, proxy, proxyWithComputed, ref, snapshot, subscribe }; | ||
export { INTERNAL_Snapshot, proxy, ref, snapshot, subscribe } from 'valtio/vanilla'; | ||
export { proxyWithComputed, subscribeKey } from 'valtio/utils'; |
@@ -6,2 +6,5 @@ "use strict"; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __esm = (fn, res) => function __init() { | ||
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; | ||
}; | ||
var __export = (target, all) => { | ||
@@ -21,7 +24,13 @@ for (var name in all) | ||
// <define:import.meta.env> | ||
var define_import_meta_env_default; | ||
var init_define_import_meta_env = __esm({ | ||
"<define:import.meta.env>"() { | ||
define_import_meta_env_default = { MODE: "production" }; | ||
} | ||
}); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
getHandler: () => getHandler, | ||
getVersion: () => getVersion, | ||
proxy: () => proxy, | ||
@@ -31,24 +40,50 @@ proxyWithComputed: () => proxyWithComputed, | ||
snapshot: () => snapshot, | ||
subscribe: () => subscribe | ||
subscribe: () => subscribe, | ||
subscribeKey: () => subscribeKey | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
init_define_import_meta_env(); | ||
// ../../node_modules/.pnpm/valtio@1.9.0/node_modules/valtio/esm/vanilla.mjs | ||
init_define_import_meta_env(); | ||
var import_proxy_compare = require("proxy-compare"); | ||
var __DEV__ = process.env.NODE_ENV !== "production"; | ||
var VERSION = Symbol(); | ||
var LISTENERS = Symbol(); | ||
var SNAPSHOT = Symbol(); | ||
var HANDLER = Symbol(); | ||
var PROMISE_RESULT = Symbol(); | ||
var PROMISE_ERROR = Symbol(); | ||
var isObject = (x) => typeof x === "object" && x !== null; | ||
var proxyStateMap = /* @__PURE__ */ new WeakMap(); | ||
var refSet = /* @__PURE__ */ new WeakSet(); | ||
function ref(o) { | ||
refSet.add(o); | ||
return o; | ||
} | ||
var isObject = (x) => typeof x === "object" && x !== null; | ||
var 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); | ||
var proxyCache = /* @__PURE__ */ new WeakMap(); | ||
var globalVersion = 1; | ||
var snapshotCache = /* @__PURE__ */ new WeakMap(); | ||
function proxy(initialObject = {}) { | ||
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) => { | ||
switch (promise.status) { | ||
case "fulfilled": | ||
return promise.value; | ||
case "rejected": | ||
throw promise.reason; | ||
default: | ||
throw promise; | ||
} | ||
}, snapCache = /* @__PURE__ */ new WeakMap(), createSnapshot = (target, version, handlePromise = defaultHandlePromise) => { | ||
const cache = snapCache.get(target); | ||
if ((cache == null ? void 0 : cache[0]) === version) { | ||
return cache[1]; | ||
} | ||
const snap = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target)); | ||
(0, import_proxy_compare.markToTrack)(snap, true); | ||
snapCache.set(target, [version, snap]); | ||
Reflect.ownKeys(target).forEach((key) => { | ||
const value = Reflect.get(target, key); | ||
if (refSet.has(value)) { | ||
(0, import_proxy_compare.markToTrack)(value, false); | ||
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); | ||
} else { | ||
snap[key] = value; | ||
} | ||
}); | ||
return Object.freeze(snap); | ||
}, proxyCache = /* @__PURE__ */ new WeakMap(), versionHolder = [1, 1], proxyFunction2 = (initialObject) => { | ||
if (!isObject(initialObject)) { | ||
@@ -61,5 +96,5 @@ throw new Error("object required"); | ||
} | ||
let version = globalVersion; | ||
let version = versionHolder[0]; | ||
const listeners = /* @__PURE__ */ new Set(); | ||
const notifyUpdate = (op, nextVersion = ++globalVersion) => { | ||
const notifyUpdate = (op, nextVersion = ++versionHolder[0]) => { | ||
if (version !== nextVersion) { | ||
@@ -70,79 +105,69 @@ version = nextVersion; | ||
}; | ||
const propListeners = /* @__PURE__ */ new Map(); | ||
const getPropListener = (prop) => { | ||
let propListener = propListeners.get(prop); | ||
if (!propListener) { | ||
propListener = (op, nextVersion) => { | ||
const newOp = [...op]; | ||
newOp[1] = [prop, ...newOp[1]]; | ||
notifyUpdate(newOp, nextVersion); | ||
}; | ||
propListeners.set(prop, propListener); | ||
let checkVersion = versionHolder[1]; | ||
const ensureVersion = (nextCheckVersion = ++versionHolder[1]) => { | ||
if (checkVersion !== nextCheckVersion && !listeners.size) { | ||
checkVersion = nextCheckVersion; | ||
propProxyStates.forEach(([propProxyState]) => { | ||
const propVersion = propProxyState[1](nextCheckVersion); | ||
if (propVersion > version) { | ||
version = propVersion; | ||
} | ||
}); | ||
} | ||
return propListener; | ||
return version; | ||
}; | ||
const popPropListener = (prop) => { | ||
const propListener = propListeners.get(prop); | ||
propListeners.delete(prop); | ||
return propListener; | ||
const createPropListener = (prop) => (op, nextVersion) => { | ||
const newOp = [...op]; | ||
newOp[1] = [prop, ...newOp[1]]; | ||
notifyUpdate(newOp, nextVersion); | ||
}; | ||
const createSnapshot = (target, receiver) => { | ||
const cache = snapshotCache.get(receiver); | ||
if ((cache == null ? void 0 : cache[0]) === version) { | ||
return cache[1]; | ||
const propProxyStates = /* @__PURE__ */ new Map(); | ||
const addPropListener = (prop, propProxyState) => { | ||
if ((define_import_meta_env_default && define_import_meta_env_default.MODE) !== "production" && propProxyStates.has(prop)) { | ||
throw new Error("prop listener already exists"); | ||
} | ||
const snapshot2 = Array.isArray(target) ? [] : Object.create(Object.getPrototypeOf(target)); | ||
(0, import_proxy_compare.markToTrack)(snapshot2, true); | ||
snapshotCache.set(receiver, [version, snapshot2]); | ||
Reflect.ownKeys(target).forEach((key) => { | ||
const value = Reflect.get(target, key, receiver); | ||
if (refSet.has(value)) { | ||
(0, import_proxy_compare.markToTrack)(value, false); | ||
snapshot2[key] = value; | ||
} else if (value instanceof Promise) { | ||
if (PROMISE_RESULT in value) { | ||
snapshot2[key] = value[PROMISE_RESULT]; | ||
} else { | ||
const errorOrPromise = value[PROMISE_ERROR] || value; | ||
Object.defineProperty(snapshot2, key, { | ||
get() { | ||
if (PROMISE_RESULT in value) { | ||
return value[PROMISE_RESULT]; | ||
} | ||
throw errorOrPromise; | ||
} | ||
}); | ||
if (listeners.size) { | ||
const remove = propProxyState[3](createPropListener(prop)); | ||
propProxyStates.set(prop, [propProxyState, remove]); | ||
} else { | ||
propProxyStates.set(prop, [propProxyState]); | ||
} | ||
}; | ||
const removePropListener = (prop) => { | ||
var _a; | ||
const entry = propProxyStates.get(prop); | ||
if (entry) { | ||
propProxyStates.delete(prop); | ||
(_a = entry[1]) == null ? void 0 : _a.call(entry); | ||
} | ||
}; | ||
const addListener = (listener) => { | ||
listeners.add(listener); | ||
if (listeners.size === 1) { | ||
propProxyStates.forEach(([propProxyState, prevRemove], prop) => { | ||
if ((define_import_meta_env_default && define_import_meta_env_default.MODE) !== "production" && prevRemove) { | ||
throw new Error("remove already exists"); | ||
} | ||
} else if (value == null ? void 0 : value[LISTENERS]) { | ||
snapshot2[key] = value[SNAPSHOT]; | ||
} else { | ||
snapshot2[key] = value; | ||
const remove = propProxyState[3](createPropListener(prop)); | ||
propProxyStates.set(prop, [propProxyState, remove]); | ||
}); | ||
} | ||
const removeListener = () => { | ||
listeners.delete(listener); | ||
if (listeners.size === 0) { | ||
propProxyStates.forEach(([propProxyState, remove], prop) => { | ||
if (remove) { | ||
remove(); | ||
propProxyStates.set(prop, [propProxyState]); | ||
} | ||
}); | ||
} | ||
}); | ||
Object.freeze(snapshot2); | ||
return snapshot2; | ||
}; | ||
return removeListener; | ||
}; | ||
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject)); | ||
const handler = { | ||
get(target, prop, receiver) { | ||
if (prop === VERSION) { | ||
return version; | ||
} | ||
if (prop === LISTENERS) { | ||
return listeners; | ||
} | ||
if (prop === SNAPSHOT) { | ||
return createSnapshot(target, receiver); | ||
} | ||
if (prop === HANDLER) { | ||
return handler; | ||
} | ||
return Reflect.get(target, prop, receiver); | ||
}, | ||
deleteProperty(target, prop) { | ||
const prevValue = Reflect.get(target, prop); | ||
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS]; | ||
if (childListeners) { | ||
childListeners.delete(popPropListener(prop)); | ||
} | ||
removePropListener(prop); | ||
const deleted = Reflect.deleteProperty(target, prop); | ||
@@ -154,4 +179,2 @@ if (deleted) { | ||
}, | ||
is: Object.is, | ||
canProxy, | ||
set(target, prop, value, receiver) { | ||
@@ -161,32 +184,30 @@ var _a; | ||
const prevValue = Reflect.get(target, prop, receiver); | ||
if (hasPrevValue && this.is(prevValue, value)) { | ||
if (hasPrevValue && objectIs(prevValue, value)) { | ||
return true; | ||
} | ||
const childListeners = prevValue == null ? void 0 : prevValue[LISTENERS]; | ||
if (childListeners) { | ||
childListeners.delete(popPropListener(prop)); | ||
} | ||
removePropListener(prop); | ||
if (isObject(value)) { | ||
value = (0, import_proxy_compare.getUntracked)(value) || value; | ||
} | ||
let nextValue; | ||
if ((_a = Object.getOwnPropertyDescriptor(target, prop)) == null ? void 0 : _a.set) { | ||
nextValue = value; | ||
} else if (value instanceof Promise) { | ||
nextValue = value.then((v) => { | ||
nextValue[PROMISE_RESULT] = v; | ||
let nextValue = value; | ||
if ((_a = Object.getOwnPropertyDescriptor(target, prop)) == null ? void 0 : _a.set) | ||
; | ||
else if (value instanceof Promise) { | ||
value.then((v) => { | ||
value.status = "fulfilled"; | ||
value.value = v; | ||
notifyUpdate(["resolve", [prop], v]); | ||
return v; | ||
}).catch((e) => { | ||
nextValue[PROMISE_ERROR] = e; | ||
value.status = "rejected"; | ||
value.reason = e; | ||
notifyUpdate(["reject", [prop], e]); | ||
}); | ||
} else if (value == null ? void 0 : value[LISTENERS]) { | ||
nextValue = value; | ||
nextValue[LISTENERS].add(getPropListener(prop)); | ||
} else if (this.canProxy(value)) { | ||
nextValue = proxy(value); | ||
nextValue[LISTENERS].add(getPropListener(prop)); | ||
} else { | ||
nextValue = value; | ||
if (!proxyStateMap.has(value) && canProxy(value)) { | ||
nextValue = proxy(value); | ||
} | ||
const childProxyState = !refSet.has(nextValue) && proxyStateMap.get(nextValue); | ||
if (childProxyState) { | ||
addPropListener(prop, childProxyState); | ||
} | ||
} | ||
@@ -198,6 +219,16 @@ Reflect.set(target, prop, nextValue, receiver); | ||
}; | ||
const proxyObject = new Proxy(baseObject, handler); | ||
const proxyObject = newProxy(baseObject, handler); | ||
proxyCache.set(initialObject, proxyObject); | ||
const proxyState = [ | ||
baseObject, | ||
ensureVersion, | ||
createSnapshot, | ||
addListener | ||
]; | ||
proxyStateMap.set(proxyObject, proxyState); | ||
Reflect.ownKeys(initialObject).forEach((key) => { | ||
const desc = Object.getOwnPropertyDescriptor(initialObject, key); | ||
const desc = Object.getOwnPropertyDescriptor( | ||
initialObject, | ||
key | ||
); | ||
if (desc.get || desc.set) { | ||
@@ -210,8 +241,22 @@ Object.defineProperty(baseObject, key, desc); | ||
return proxyObject; | ||
}) => [ | ||
proxyFunction2, | ||
proxyStateMap, | ||
refSet, | ||
objectIs, | ||
newProxy, | ||
canProxy, | ||
defaultHandlePromise, | ||
snapCache, | ||
createSnapshot, | ||
proxyCache, | ||
versionHolder | ||
]; | ||
var [proxyFunction] = buildProxyFunction(); | ||
function proxy(initialObject = {}) { | ||
return proxyFunction(initialObject); | ||
} | ||
function getVersion(proxyObject) { | ||
return isObject(proxyObject) ? proxyObject[VERSION] : void 0; | ||
} | ||
function subscribe(proxyObject, callback, notifyInSync) { | ||
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[LISTENERS])) { | ||
const proxyState = proxyStateMap.get(proxyObject); | ||
if ((define_import_meta_env_default && define_import_meta_env_default.MODE) !== "production" && !proxyState) { | ||
console.warn("Please use proxy object"); | ||
@@ -221,2 +266,4 @@ } | ||
const ops = []; | ||
const addListener = proxyState[3]; | ||
let isListenerActive = false; | ||
const listener = (op) => { | ||
@@ -231,26 +278,45 @@ ops.push(op); | ||
promise = void 0; | ||
callback(ops.splice(0)); | ||
if (isListenerActive) { | ||
callback(ops.splice(0)); | ||
} | ||
}); | ||
} | ||
}; | ||
proxyObject[LISTENERS].add(listener); | ||
const removeListener = addListener(listener); | ||
isListenerActive = true; | ||
return () => { | ||
; | ||
proxyObject[LISTENERS].delete(listener); | ||
isListenerActive = false; | ||
removeListener(); | ||
}; | ||
} | ||
function snapshot(proxyObject) { | ||
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[SNAPSHOT])) { | ||
function snapshot(proxyObject, handlePromise) { | ||
const proxyState = proxyStateMap.get(proxyObject); | ||
if ((define_import_meta_env_default && define_import_meta_env_default.MODE) !== "production" && !proxyState) { | ||
console.warn("Please use proxy object"); | ||
} | ||
return proxyObject[SNAPSHOT]; | ||
const [target, ensureVersion, createSnapshot] = proxyState; | ||
return createSnapshot(target, ensureVersion(), handlePromise); | ||
} | ||
function getHandler(proxyObject) { | ||
if (__DEV__ && !(proxyObject == null ? void 0 : proxyObject[HANDLER])) { | ||
console.warn("Please use proxy object"); | ||
} | ||
return proxyObject[HANDLER]; | ||
function ref(obj) { | ||
refSet.add(obj); | ||
return obj; | ||
} | ||
// ../../node_modules/.pnpm/valtio@1.9.0/node_modules/valtio/esm/vanilla/utils.mjs | ||
init_define_import_meta_env(); | ||
function subscribeKey(proxyObject, key, callback, notifyInSync) { | ||
let prevValue = proxyObject[key]; | ||
return subscribe( | ||
proxyObject, | ||
() => { | ||
const nextValue = proxyObject[key]; | ||
if (!Object.is(prevValue, nextValue)) { | ||
callback(prevValue = nextValue); | ||
} | ||
}, | ||
notifyInSync | ||
); | ||
} | ||
var DEVTOOLS = Symbol(); | ||
function proxyWithComputed(initialObject, computedFns) { | ||
; | ||
Object.keys(computedFns).forEach((key) => { | ||
@@ -274,4 +340,2 @@ if (Object.getOwnPropertyDescriptor(initialObject, key)) { | ||
0 && (module.exports = { | ||
getHandler, | ||
getVersion, | ||
proxy, | ||
@@ -281,3 +345,4 @@ proxyWithComputed, | ||
snapshot, | ||
subscribe | ||
subscribe, | ||
subscribeKey | ||
}); |
{ | ||
"name": "@zag-js/store", | ||
"version": "0.0.0-dev-20230116072304", | ||
"version": "0.0.0-dev-20230127074732", | ||
"description": "The reactive store package for zag machines", | ||
@@ -31,3 +31,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"clean-package": "2.2.0" | ||
"clean-package": "2.2.0", | ||
"valtio": "^1.9.0" | ||
}, | ||
@@ -34,0 +35,0 @@ "module": "dist/index.mjs", |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
24329
638
1
2