@zag-js/store
Advanced tools
Comparing version 0.0.0-dev-20240605193509 to 0.0.0-dev-20240605203045
@@ -1,2 +0,319 @@ | ||
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var src_exports={};__export(src_exports,{makeGlobal:()=>makeGlobal,proxy:()=>proxy,proxyWithComputed:()=>proxyWithComputed,ref:()=>ref,snapshot:()=>snapshot,subscribe:()=>subscribe});module.exports=__toCommonJS(src_exports);function getGlobal(){if(typeof globalThis!=="undefined")return globalThis;if(typeof self!=="undefined")return self;if(typeof window!=="undefined")return window;if(typeof global!=="undefined")return global}function makeGlobal(key,value){const g=getGlobal();if(!g)return value();g[key]||(g[key]=value());return g[key]}var import_proxy_compare=require("proxy-compare");var isDev=process.env.NODE_ENV!=="production";var isObject=x=>typeof x==="object"&&x!==null;var proxyStateMap=makeGlobal("__zag__proxyStateMap",()=>new WeakMap);var refSet=makeGlobal("__zag__refSet",()=>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=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=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=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=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=>{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(!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}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}0&&(module.exports={makeGlobal,proxy,proxyWithComputed,ref,snapshot,subscribe}); | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
makeGlobal: () => makeGlobal, | ||
proxy: () => proxy, | ||
proxyWithComputed: () => proxyWithComputed, | ||
ref: () => ref, | ||
snapshot: () => snapshot, | ||
subscribe: () => subscribe | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/global.ts | ||
function getGlobal() { | ||
if (typeof globalThis !== "undefined") | ||
return globalThis; | ||
if (typeof self !== "undefined") | ||
return self; | ||
if (typeof window !== "undefined") | ||
return window; | ||
if (typeof global !== "undefined") | ||
return global; | ||
} | ||
function makeGlobal(key, value) { | ||
const g = getGlobal(); | ||
if (!g) | ||
return value(); | ||
g[key] || (g[key] = value()); | ||
return g[key]; | ||
} | ||
// 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 = makeGlobal("__zag__proxyStateMap", () => /* @__PURE__ */ new WeakMap()); | ||
var refSet = makeGlobal("__zag__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) => { | ||
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 (!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; | ||
}) => [ | ||
// public functions | ||
proxyFunction2, | ||
// shared state | ||
proxyStateMap, | ||
refSet, | ||
// internal things | ||
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; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
makeGlobal, | ||
proxy, | ||
proxyWithComputed, | ||
ref, | ||
snapshot, | ||
subscribe | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@zag-js/store", | ||
"version": "0.0.0-dev-20240605193509", | ||
"version": "0.0.0-dev-20240605203045", | ||
"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
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
78108
989
1