@zag-js/store
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -1,2 +0,3 @@ | ||
export { INTERNAL_Snapshot, proxy, ref, snapshot, subscribe } from 'valtio/vanilla'; | ||
export { proxyWithComputed, subscribeKey } from 'valtio/utils'; | ||
export { Snapshot, proxy, ref, snapshot, subscribe } from './proxy.js'; | ||
export { proxyWithComputed } from './proxy-computed.js'; | ||
export { subscribeKey } from './subscribe-key.js'; |
@@ -23,12 +23,284 @@ "use strict"; | ||
__export(src_exports, { | ||
proxy: () => import_vanilla.proxy, | ||
proxyWithComputed: () => import_utils.proxyWithComputed, | ||
ref: () => import_vanilla.ref, | ||
snapshot: () => import_vanilla.snapshot, | ||
subscribe: () => import_vanilla.subscribe, | ||
subscribeKey: () => import_utils.subscribeKey | ||
proxy: () => proxy, | ||
proxyWithComputed: () => proxyWithComputed, | ||
ref: () => ref, | ||
snapshot: () => snapshot, | ||
subscribe: () => subscribe, | ||
subscribeKey: () => subscribeKey | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_vanilla = require("valtio/vanilla"); | ||
var import_utils = require("valtio/utils"); | ||
// src/proxy.ts | ||
var import_proxy_compare = require("proxy-compare"); | ||
var isDev = process.env.NODE_ENV !== "production"; | ||
var isObject = (x) => typeof x === "object" && x !== null; | ||
var proxyStateMap = /* @__PURE__ */ new WeakMap(); | ||
var refSet = /* @__PURE__ */ new WeakSet(); | ||
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?.[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)) { | ||
throw new Error("object required"); | ||
} | ||
const found = proxyCache.get(initialObject); | ||
if (found) { | ||
return found; | ||
} | ||
let version = versionHolder[0]; | ||
const listeners = /* @__PURE__ */ new Set(); | ||
const notifyUpdate = (op, nextVersion = ++versionHolder[0]) => { | ||
if (version !== nextVersion) { | ||
version = nextVersion; | ||
listeners.forEach((listener) => listener(op, nextVersion)); | ||
} | ||
}; | ||
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 version; | ||
}; | ||
const createPropListener = (prop) => (op, nextVersion) => { | ||
const newOp = [...op]; | ||
newOp[1] = [prop, ...newOp[1]]; | ||
notifyUpdate(newOp, nextVersion); | ||
}; | ||
const propProxyStates = /* @__PURE__ */ new Map(); | ||
const addPropListener = (prop, propProxyState) => { | ||
if (isDev && propProxyStates.has(prop)) { | ||
throw new Error("prop listener already exists"); | ||
} | ||
if (listeners.size) { | ||
const remove = propProxyState[3](createPropListener(prop)); | ||
propProxyStates.set(prop, [propProxyState, remove]); | ||
} else { | ||
propProxyStates.set(prop, [propProxyState]); | ||
} | ||
}; | ||
const removePropListener = (prop) => { | ||
const entry = propProxyStates.get(prop); | ||
if (entry) { | ||
propProxyStates.delete(prop); | ||
entry[1]?.(); | ||
} | ||
}; | ||
const addListener = (listener) => { | ||
listeners.add(listener); | ||
if (listeners.size === 1) { | ||
propProxyStates.forEach(([propProxyState, prevRemove], prop) => { | ||
if (isDev && prevRemove) { | ||
throw new Error("remove already exists"); | ||
} | ||
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]); | ||
} | ||
}); | ||
} | ||
}; | ||
return removeListener; | ||
}; | ||
const baseObject = Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject)); | ||
const handler = { | ||
deleteProperty(target, prop) { | ||
const prevValue = Reflect.get(target, prop); | ||
removePropListener(prop); | ||
const deleted = Reflect.deleteProperty(target, prop); | ||
if (deleted) { | ||
notifyUpdate(["delete", [prop], prevValue]); | ||
} | ||
return deleted; | ||
}, | ||
set(target, prop, value, receiver) { | ||
const hasPrevValue = Reflect.has(target, prop); | ||
const prevValue = Reflect.get(target, prop, receiver); | ||
if (hasPrevValue && (objectIs(prevValue, value) || proxyCache.has(value) && objectIs(prevValue, proxyCache.get(value)))) { | ||
return true; | ||
} | ||
removePropListener(prop); | ||
if (isObject(value)) { | ||
value = (0, import_proxy_compare.getUntracked)(value) || value; | ||
} | ||
let nextValue = value; | ||
if (Object.getOwnPropertyDescriptor(target, prop)?.set) { | ||
} else if (value instanceof Promise) { | ||
value.then((v) => { | ||
value.status = "fulfilled"; | ||
value.value = v; | ||
notifyUpdate(["resolve", [prop], v]); | ||
}).catch((e) => { | ||
value.status = "rejected"; | ||
value.reason = e; | ||
notifyUpdate(["reject", [prop], e]); | ||
}); | ||
} else { | ||
if (!proxyStateMap.has(value) && canProxy(value)) { | ||
nextValue = proxy(value); | ||
} | ||
const childProxyState = !refSet.has(nextValue) && proxyStateMap.get(nextValue); | ||
if (childProxyState) { | ||
addPropListener(prop, childProxyState); | ||
} | ||
} | ||
Reflect.set(target, prop, nextValue, receiver); | ||
notifyUpdate(["set", [prop], value, prevValue]); | ||
return true; | ||
} | ||
}; | ||
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); | ||
if (desc.get || desc.set) { | ||
Object.defineProperty(baseObject, key, desc); | ||
} else { | ||
proxyObject[key] = initialObject[key]; | ||
} | ||
}); | ||
return proxyObject; | ||
}) => [ | ||
proxyFunction2, | ||
proxyStateMap, | ||
refSet, | ||
objectIs, | ||
newProxy, | ||
canProxy, | ||
defaultHandlePromise, | ||
snapCache, | ||
createSnapshot, | ||
proxyCache, | ||
versionHolder | ||
]; | ||
var [proxyFunction] = buildProxyFunction(); | ||
function proxy(initialObject = {}) { | ||
return proxyFunction(initialObject); | ||
} | ||
function subscribe(proxyObject, callback, notifyInSync) { | ||
const proxyState = proxyStateMap.get(proxyObject); | ||
if (isDev && !proxyState) { | ||
console.warn("Please use proxy object"); | ||
} | ||
let promise; | ||
const ops = []; | ||
const addListener = proxyState[3]; | ||
let isListenerActive = false; | ||
const listener = (op) => { | ||
ops.push(op); | ||
if (notifyInSync) { | ||
callback(ops.splice(0)); | ||
return; | ||
} | ||
if (!promise) { | ||
promise = Promise.resolve().then(() => { | ||
promise = void 0; | ||
if (isListenerActive) { | ||
callback(ops.splice(0)); | ||
} | ||
}); | ||
} | ||
}; | ||
const removeListener = addListener(listener); | ||
isListenerActive = true; | ||
return () => { | ||
isListenerActive = false; | ||
removeListener(); | ||
}; | ||
} | ||
function snapshot(proxyObject, handlePromise) { | ||
const proxyState = proxyStateMap.get(proxyObject); | ||
if (isDev && !proxyState) { | ||
console.warn("Please use proxy object"); | ||
} | ||
const [target, ensureVersion, createSnapshot] = proxyState; | ||
return createSnapshot(target, ensureVersion(), handlePromise); | ||
} | ||
function ref(obj) { | ||
refSet.add(obj); | ||
return obj; | ||
} | ||
// src/proxy-computed.ts | ||
function proxyWithComputed(initialObject, computedFns) { | ||
const keys = Object.keys(computedFns); | ||
keys.forEach((key) => { | ||
if (Object.getOwnPropertyDescriptor(initialObject, key)) { | ||
throw new Error("object property already defined"); | ||
} | ||
const computedFn = computedFns[key]; | ||
const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn; | ||
const desc = {}; | ||
desc.get = () => get(snapshot(proxyObject)); | ||
if (set) { | ||
desc.set = (newValue) => set(proxyObject, newValue); | ||
} | ||
Object.defineProperty(initialObject, key, desc); | ||
}); | ||
const proxyObject = proxy(initialObject); | ||
return proxyObject; | ||
} | ||
// src/subscribe-key.ts | ||
var defaultCompareFn = (prev, next) => Object.is(prev, next); | ||
function subscribeKey(obj, key, fn, sync, compareFn) { | ||
let prev = Reflect.get(snapshot(obj), key); | ||
const isEqual = compareFn || defaultCompareFn; | ||
function onSnapshotChange() { | ||
const snap = snapshot(obj); | ||
if (isEqual(prev, snap[key])) | ||
return; | ||
fn(snap[key]); | ||
prev = Reflect.get(snap, key); | ||
} | ||
return subscribe(obj, onSnapshotChange, sync); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -35,0 +307,0 @@ 0 && (module.exports = { |
{ | ||
"name": "@zag-js/store", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "The reactive store package for zag machines", | ||
@@ -26,4 +26,3 @@ "keywords": [ | ||
"dependencies": { | ||
"proxy-compare": "2.4.0", | ||
"valtio": "^1.9.0" | ||
"proxy-compare": "2.4.0" | ||
}, | ||
@@ -30,0 +29,0 @@ "clean-package": "../../clean-package.config.json", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
53331
1
18
1491
6
- Removedvaltio@^1.9.0
- Removedderive-valtio@0.1.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removedproxy-compare@2.6.0(transitive)
- Removedreact@18.3.1(transitive)
- Removeduse-sync-external-store@1.2.0(transitive)
- Removedvaltio@1.13.2(transitive)