Socket
Socket
Sign inDemoInstall

@zag-js/core

Package Overview
Dependencies
Maintainers
1
Versions
890
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zag-js/core - npm Package Compare versions

Comparing version 0.0.0-dev-20220412162814 to 0.0.0-dev-20220412212252

2

dist/index.js

@@ -1,2 +0,2 @@

var A=Object.defineProperty,Y=Object.defineProperties,tt=Object.getOwnPropertyDescriptor,et=Object.getOwnPropertyDescriptors,nt=Object.getOwnPropertyNames,N=Object.getOwnPropertySymbols;var V=Object.prototype.hasOwnProperty,it=Object.prototype.propertyIsEnumerable;var W=(n,t,e)=>t in n?A(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,v=(n,t)=>{for(var e in t||(t={}))V.call(t,e)&&W(n,e,t[e]);if(N)for(var e of N(t))it.call(t,e)&&W(n,e,t[e]);return n},M=(n,t)=>Y(n,et(t)),st=n=>A(n,"__esModule",{value:!0});var ot=(n,t)=>{for(var e in t)A(n,e,{get:t[e],enumerable:!0})},rt=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of nt(t))!V.call(n,s)&&(e||s!=="default")&&A(n,s,{get:()=>t[s],enumerable:!(i=tt(t,s))||i.enumerable});return n};var at=(n=>(t,e)=>n&&n.get(t)||(e=rt(st({}),t,1),n&&n.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var lt={};ot(lt,{Machine:()=>g,choose:()=>$,createMachine:()=>vt,guards:()=>Z,mergeProps:()=>X,proxy:()=>m.proxy,ref:()=>m.ref,snapshot:()=>m.snapshot,subscribe:()=>m.subscribe});var m=require("valtio/vanilla");function K(n){for(;n.length>0;)n.pop();return n}var F=(n,...t)=>{let e=typeof n=="function"?n(...t):n;return e!=null?e:void 0},p=n=>n,L=()=>{};var H=(...n)=>(...t)=>{n.forEach(function(e){e==null||e(...t)})},R=(()=>{let n=0;return()=>(n++,n.toString(36))})();var f=n=>Array.isArray(n);var d=n=>!(n==null||typeof n!="object"||f(n)),U=n=>typeof n=="number"&&!Number.isNaN(n),h=n=>typeof n=="string",E=n=>typeof n=="function";function O(...n){let t=n.length===1?n[0]:n[1],e=n.length===2?n[0]:!0}function j(...n){let t=n.length===1?n[0]:n[1],e=n.length===2?n[0]:!0}function $(n){return{predicate:t=>(e,i)=>{var s;return(s=n.find(o=>{var r;return o.guard=o.guard||(()=>!0),h(o.guard)?!!((r=t[o.guard])==null?void 0:r.call(t,e,i)):B(o.guard)?o.guard.predicate(t!=null?t:{})(e,i):o.guard(e,i)}))==null?void 0:s.actions}}}function B(n){return d(n)&&n.predicate!=null}function D(n,t){return(e,i)=>B(n)?n.predicate(t!=null?t:{})(e,i):n}function ct(...n){return{predicate:t=>(e,i)=>n.map(s=>{var o;return h(s)?!!((o=t[s])==null?void 0:o.call(t,e,i)):E(s)?s(e,i):s.predicate(t)(e,i)}).some(Boolean)}}function ht(...n){return{predicate:t=>(e,i)=>n.map(s=>{var o;return h(s)?!!((o=t[s])==null?void 0:o.call(t,e,i)):E(s)?s(e,i):s.predicate(t)(e,i)}).every(Boolean)}}function Tt(n){return{predicate:t=>(e,i)=>{var s;return h(n)?!((s=t[n])==null?void 0:s.call(t,e,i)):E(n)?!n(e,i):!n.predicate(t)(e,i)}}}var Z={or:ct,and:ht,not:Tt};function pt(n){return d(n)&&n.predicate!=null}var ut=()=>!0;function w(n,t){return n=n!=null?n:ut,(e,i)=>{if(h(n)){let s=t==null?void 0:t[n];return E(s)?s(e,i):s}return pt(n)?n.predicate(t!=null?t:{})(e,i):n==null?void 0:n(e,i)}}var I=require("klona/json"),y=require("valtio/utils"),C=require("valtio/vanilla");var _=require("valtio");function q(n){var e;let t=(0,_.proxy)({value:"",previousValue:"",event:p({}),context:(e=n.context)!=null?e:p({}),done:!1,tags:[],hasTag(i){return this.tags.includes(i)},matches(...i){return i.includes(this.value)},can(i){return p(this).nextEvents.includes(i)},get nextEvents(){var o,r,a,c;let i=(a=(r=(o=n.states)==null?void 0:o[this.value])==null?void 0:r.on)!=null?a:{},s=(c=n==null?void 0:n.on)!=null?c:{};return Object.keys(v(v({},i),s))},get changed(){return this.event.value==="machine.init"||!this.previousValue?!1:this.value!==this.previousValue}});return p(t)}function b(n,t){return(e,i)=>{if(U(n))return n;if(E(n))return n(e,i);if(h(n)){let s=Number.parseFloat(n);if(!Number.isNaN(s))return s;if(t){let o=t==null?void 0:t[n];if(o==null){let r=`[machine] Cannot determine delay for ${n}. It doesn't exist in options.delays`;throw new Error(r)}return E(o)?o(e,i):o}}}}function T(n){return h(n)?{type:n}:n}function S(n){return n?f(n)?n:[n]:[]}function z(n){return h(n)?{target:n}:n}function J(n,t){return(e,i)=>S(n).map(z).find(s=>{var a;let r=w(s.guard,t)(e,i);return(a=r!=null?r:s.target)!=null?a:s.actions})}function Q(n,t){let e=h(n)?z(n):n,i=s=>(s.actions&&!s.target&&t&&(s.target=t),s);if(f(e))return e.map(i);if(d(e))return i(p(e))}var g=class{constructor(t,e){this.config=t;this.status="Not Started";this.type="machine";this.activityEvents=new Map;this.delayedEvents=new Map;this.stateListeners=new Set;this.eventListeners=new Set;this.doneListeners=new Set;this.contextWatchers=new Set;this.removeStateListener=L;this.removeEventListener=L;this.children=new Map;this.start=t=>{if(this.status==="Running")return this;this.status="Running";let e=T("machine.init");if(t){let o=d(t)?t:{context:this.config.context,value:t};this.setState(o.value),this.setContext(o.context)}let i={target:t?void 0:this.config.initial},s=this.getNextStateInfo(i,e);return s.target=p(s.target||i.target),this.initialState=s,this.performStateChangeEffects(s.target,s,e),this.removeStateListener=(0,C.subscribe)(this.state,()=>{this.stateListeners.forEach(o=>{o(this.stateSnapshot)})},this.sync),this.removeEventListener=(0,y.subscribeKey)(this.state,"event",o=>{this.config.onEvent&&this.executeActions(this.config.onEvent,o);for(let r of this.eventListeners)r(o)}),this.setupContextWatchers(),this.executeActivities(T("machine.start"),S(this.config.activities),"machine.start"),this.executeActions(this.config.entry,T("machine.start")),this};this.setupContextWatchers=()=>{var t;for(let[e,i]of Object.entries((t=this.config.watch)!=null?t:{}))this.contextWatchers.add((0,y.subscribeKey)(this.state.context,e,()=>{this.executeActions(i,this.state.event)}))};this.setupComputed=()=>{var i;let t=p((i=this.config.computed)!=null?i:{}),e=Object.fromEntries(Object.entries(t).map(([s,o])=>[s,r=>o(r(this.state.context))]));(0,y.derive)(e,{proxy:this.state.context})};this.detachComputed=()=>{(0,y.underive)(this.state.context,{delete:!0})};this.stop=()=>{if(this.status!=="Stopped")return this.setState(null),this.setEvent("machine.stop"),this.config.context&&this.setContext(this.config.context),this.stopStateListeners(),this.stopChildren(),this.stopActivities(),this.stopDelayedEvents(),this.stopContextWatchers(),this.stopEventListeners(),this.detachComputed(),this.status="Stopped",this.executeActions(this.config.exit,T("machine.stop")),this};this.stopEventListeners=()=>{this.eventListeners.clear(),this.removeEventListener()};this.stopStateListeners=()=>{this.removeStateListener(),this.stateListeners.clear()};this.stopContextWatchers=()=>{this.contextWatchers.forEach(t=>t()),this.contextWatchers.clear()};this.stopDelayedEvents=()=>{this.delayedEvents.forEach(t=>{t.forEach(e=>e())}),this.delayedEvents.clear()};this.stopActivities=t=>{var e,i;t?((e=this.activityEvents.get(t))==null||e.forEach(s=>s()),(i=this.activityEvents.get(t))==null||i.clear(),this.activityEvents.delete(t)):(this.activityEvents.forEach(s=>{s.forEach(o=>o()),s.clear()}),this.activityEvents.clear())};this.sendChild=(t,e)=>{let i=T(t),s=F(e,this.contextSnapshot),o=this.children.get(s);o||j(`[machine/send-child] Cannot send '${i.type}' event to unknown child`),o.send(i)};this.stopChild=t=>{this.children.has(t)||j("[machine/stop-child] Cannot stop unknown child"),this.children.get(t).stop(),this.children.delete(t)};this.removeChild=t=>{this.children.delete(t)};this.stopChildren=()=>{this.children.forEach(t=>t.stop()),this.children.clear()};this.setParent=t=>{this.parent=t};this.spawn=(t,e)=>{let i=F(t);return e&&(i.id=e),i.type="machine.actor",i.setParent(this),this.children.set(i.id,p(i)),i.onDone(()=>{this.removeChild(i.id)}).start(),p((0,C.ref)(i))};this.addActivityCleanup=(t,e)=>{var i;!t||(this.activityEvents.has(t)?(i=this.activityEvents.get(t))==null||i.add(e):this.activityEvents.set(t,new Set([e])))};this.setState=t=>{this.state.previousValue=this.state.value,this.state.value=t;let e=this.getStateNode(t);t==null?K(this.state.tags):this.state.tags=S(e==null?void 0:e.tags)};this.setContext=t=>{for(let e in t)this.state.context[e]=t[e]};this.withContext=t=>{this.detachComputed();let e=v(v({},this.config.context),t);return new g(M(v({},this.config),{context:e}),this.options)};this.withOptions=t=>(this.detachComputed(),new g(this.config,v(v({},this.options),t)));this.setActions=t=>{this.actionMap=v(v({},this.actionMap),t)};this.clone=()=>(this.detachComputed(),new g(this.config,this.options));this.getStateNode=t=>{var e;if(!!t)return(e=this.config.states)==null?void 0:e[t]};this.getNextStateInfo=(t,e)=>{var r;let i=this.determineTransition(t,e),s=(r=i==null?void 0:i.target)!=null?r:this.state.value,o=this.getStateNode(s);return{transition:i,stateNode:o,target:s}};this.getActionFromDelayedTransition=t=>{let e=T("machine.after"),s=b(t.delay,this.delayMap)(this.contextSnapshot,e),o;return{entry:()=>{o=globalThis.setTimeout(()=>{let r=this.state.value,a=this.getNextStateInfo(t,e);this.performStateChangeEffects(r,a,e)},s)},exit:()=>{globalThis.clearTimeout(o)}}};this.getDelayedEventActions=t=>{let e=this.getStateNode(t),i=T("machine.after");if(!e||!e.after)return;let s=[],o=[];if(f(e.after)){let r=this.determineTransition(e.after,i);if(!r)return;let a=this.getActionFromDelayedTransition(r);s.push(a.entry),o.push(a.exit)}else if(d(e.after))for(let r in e.after){let a=e.after[r],c={};if(f(a)){let l=this.determineTransition(a,i);l&&(c=l)}else h(a)?c={target:a,delay:r}:c=M(v({},a),{delay:r});let u=this.getActionFromDelayedTransition(c);s.push(u.entry),o.push(u.exit)}return{entries:s,exits:o}};this.executeActions=(t,e)=>{var s;let i=D(t,this.guardMap)(this.contextSnapshot,e);for(let o of S(i)){let r=h(o)?(s=this.actionMap)==null?void 0:s[o]:o;O(h(o)&&!r,`[machine/exec-action] No implementation found for action: \`${o}\``),r==null||r(this.state.context,e,this.meta)}};this.executeActivities=(t,e,i)=>{var s;for(let o of e){let r=h(o)?(s=this.activityMap)==null?void 0:s[o]:o;if(!r){O(`[machine/exec-activity] No implementation found for activity: \`${o}\``);continue}let a=r(this.state.context,t,this.meta);a&&this.addActivityCleanup(i!=null?i:this.state.value,a)}};this.createEveryActivities=(t,e)=>{if(!t)return;let i=T("machine.every");if(f(t)){let s=S(t).find(c=>{let u=b(c.delay,this.delayMap);c.delay=u(this.contextSnapshot,i);let x=w(c.guard,this.guardMap)(this.contextSnapshot,i);return x!=null?x:c.delay});if(!s)return;let r=b(s.delay,this.delayMap)(this.contextSnapshot,i);e(()=>{let c=globalThis.setInterval(()=>{this.executeActions(s.actions,i)},r);return()=>{globalThis.clearInterval(c)}})}else for(let s in t){let o=t==null?void 0:t[s],a=b(s,this.delayMap)(this.contextSnapshot,i);e(()=>{let u=globalThis.setInterval(()=>{this.executeActions(o,i)},a);return()=>{globalThis.clearInterval(u)}})}};this.setEvent=t=>{this.state.event=(0,C.ref)(T(t))};this.performExitEffects=(t,e)=>{let i=this.state.value,s=t?this.getStateNode(t):void 0;this.stopActivities(i);let o=D(s==null?void 0:s.exit,this.guardMap)(this.contextSnapshot,e),r=S(o),a=this.delayedEvents.get(i);a&&r.push(...a),this.executeActions(r,e),this.eventListeners.clear()};this.performEntryEffects=(t,e)=>{let i=this.getStateNode(t),s=S(i==null?void 0:i.activities);this.createEveryActivities(i==null?void 0:i.every,c=>{s.unshift(c)}),s.length>0&&this.executeActivities(e,s);let o=D(i==null?void 0:i.entry,this.guardMap)(this.contextSnapshot,e),r=S(o),a=this.getDelayedEventActions(t);(i==null?void 0:i.after)&&a&&(this.delayedEvents.set(t,a==null?void 0:a.exits),r.push(...a.entries)),this.executeActions(r,e),(i==null?void 0:i.type)==="final"&&(this.state.done=!0,this.doneListeners.forEach(c=>{c(this.stateSnapshot)}),this.stop())};this.performTransitionEffects=(t,e)=>{let i=this.determineTransition(t,e);this.executeActions(i==null?void 0:i.actions,e)};this.performStateChangeEffects=(t,e,i)=>{var o,r;this.setEvent(i),e.target=(r=(o=e.target)!=null?o:this.state.value)!=null?r:void 0;let s=e.target&&e.target!==this.state.value;s&&this.performExitEffects(t,i),this.performTransitionEffects(e==null?void 0:e.transition,i),this.setState(e.target),s&&this.performEntryEffects(e.target,i)};this.determineTransition=(t,e)=>{let i=J(t,this.guardMap);return i==null?void 0:i(this.contextSnapshot,e)};this.sendParent=t=>{var i;this.parent||j("[machine/send-parent] Cannot send event to an unknown parent");let e=T(t);(i=this.parent)==null||i.send(e)};this.send=t=>{let e=T(t);this.transition(this.state.value,e)};this.transition=(t,e)=>{var c,u,l;let i=h(t)?this.getStateNode(t):t==null?void 0:t.stateNode,s=T(e);if(!i&&!this.config.on){let x=this.status==="Stopped"?"[machine/transition] Cannot transition a stopped machine":"[machine/transition] State does not have a definition";O(x);return}let o=(l=(c=i==null?void 0:i.on)==null?void 0:c[s.type])!=null?l:(u=this.config.on)==null?void 0:u[s.type],r=Q(o,this.state.value);if(!r)return;let a=this.getNextStateInfo(r,s);return this.performStateChangeEffects(this.state.value,a,s),a.stateNode};this.subscribe=t=>(this.stateListeners.add(t),this.status==="Running"&&t(this.stateSnapshot),()=>{this.stateListeners.delete(t)});this.onDone=t=>(this.doneListeners.add(t),this);this.onTransition=t=>(this.stateListeners.add(t),this.status==="Running"&&t(this.stateSnapshot),this);this.onEvent=t=>(t(this.state.event),this.eventListeners.add(t),this);var s,o,r,a,c,u,l,x,G,P,k;this.options=(0,I.klona)(e),this.id=(s=t.id)!=null?s:`machine-${R()}`,this.guardMap=(r=(o=this.options)==null?void 0:o.guards)!=null?r:{},this.actionMap=(c=(a=this.options)==null?void 0:a.actions)!=null?c:{},this.delayMap=(l=(u=this.options)==null?void 0:u.delays)!=null?l:{},this.activityMap=(G=(x=this.options)==null?void 0:x.activities)!=null?G:{},this.sync=(k=(P=this.options)==null?void 0:P.sync)!=null?k:!1,this.state=q((0,I.klona)(t)),this.setupComputed();let i=T("machine.created");this.executeActions(t==null?void 0:t.created,i)}get stateSnapshot(){return p((0,C.snapshot)(this.state))}get contextSnapshot(){return this.stateSnapshot.context}get self(){let t=this;return{id:this.id,send:this.send.bind(this),sendParent:this.sendParent.bind(this),sendChild:this.sendChild.bind(this),stop:this.stop.bind(this),stopChild:this.stopChild.bind(this),spawn:this.spawn.bind(this),get state(){return t.stateSnapshot}}}get meta(){return{state:this.stateSnapshot,guards:this.guardMap,send:this.send.bind(this),listen:this.onEvent.bind(this),self:this.self,getState:()=>this.stateSnapshot}}},vt=(n,t)=>new g(n,t);var St=(...n)=>n.map(t=>t==null?void 0:t.trim()).filter(Boolean).join(" ");function X(...n){var e,i;let t={};for(let s of n){for(let o in t){if(/^on[A-Z]/.test(o)&&typeof t[o]=="function"&&typeof s[o]=="function"){t[o]=H(t[o],s[o]);continue}if(o==="className"||o==="class"){t[o]=St(t[o],s[o]);continue}if(o==="style"){t[o]=Object.assign({},(e=t[o])!=null?e:{},(i=s[o])!=null?i:{});continue}t[o]=s[o]!==void 0?s[o]:t[o]}for(let o in s)t[o]===void 0&&(t[o]=s[o])}return t}module.exports=at(lt);
var J=Object.defineProperty;var Ct=Object.getOwnPropertyDescriptor;var Ot=Object.getOwnPropertyNames;var At=Object.prototype.hasOwnProperty;var wt=(e,t)=>{for(var n in t)J(e,n,{get:t[n],enumerable:!0})},jt=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of Ot(t))!At.call(e,s)&&s!==n&&J(e,s,{get:()=>t[s],enumerable:!(i=Ct(t,s))||i.enumerable});return e};var Pt=e=>jt(J({},"__esModule",{value:!0}),e);var Ut={};wt(Ut,{Machine:()=>w,choose:()=>dt,createMachine:()=>Ht,guards:()=>pt,mergeProps:()=>gt,proxy:()=>j,ref:()=>L,snapshot:()=>N,subscribe:()=>P});module.exports=Pt(Ut);var Jt=Symbol(),$t=Symbol(),Dt=Symbol(),et=Object.getPrototypeOf,$=new WeakMap,Mt=e=>e&&($.has(e)?$.get(e):et(e)===Object.prototype||et(e)===Array.prototype);var nt=e=>Mt(e)&&e[Dt]||null,B=(e,t=!0)=>{$.set(e,t)};var V={},at=Symbol(),b=Symbol(),_=Symbol(),Lt=Symbol(),k=Symbol(),it=Symbol(),X=new WeakSet;function L(e){return X.add(e),e}var G=e=>typeof e=="object"&&e!==null,It=e=>G(e)&&!X.has(e)&&(Array.isArray(e)||!(Symbol.iterator in e))&&!(e instanceof WeakMap)&&!(e instanceof WeakSet)&&!(e instanceof Error)&&!(e instanceof Number)&&!(e instanceof Date)&&!(e instanceof String)&&!(e instanceof RegExp)&&!(e instanceof ArrayBuffer),st=new WeakMap,rt=1,ot=new WeakMap;function j(e={}){if(!G(e))throw new Error("object required");let t=st.get(e);if(t)return t;let n=rt,i=new Set,s=(l,a=++rt)=>{n!==a&&(n=a,i.forEach(u=>u(l,a)))},r=new Map,o=l=>{let a=r.get(l);return a||(a=(u,p)=>{let S=[...u];S[1]=[l,...S[1]],s(S,p)},r.set(l,a)),a},c=l=>{let a=r.get(l);return r.delete(l),a},h=(l,a)=>{let u=ot.get(a);if(u?.[0]===n)return u[1];let p=Array.isArray(l)?[]:Object.create(Object.getPrototypeOf(l));return B(p,!0),ot.set(a,[n,p]),Reflect.ownKeys(l).forEach(S=>{let f=Reflect.get(l,S,a);if(X.has(f))B(f,!1),p[S]=f;else if(f instanceof Promise)if(k in f)p[S]=f[k];else{let W=f[it]||f;Object.defineProperty(p,S,{get(){if(k in f)return f[k];throw W}})}else f?.[b]?p[S]=f[_]:p[S]=f}),Object.freeze(p),p},d=Array.isArray(e)?[]:Object.create(Object.getPrototypeOf(e)),y={get(l,a,u){return a===at?n:a===b?i:a===_?h(l,u):a===Lt?y:Reflect.get(l,a,u)},deleteProperty(l,a){let u=Reflect.get(l,a),p=u?.[b];p&&p.delete(c(a));let S=Reflect.deleteProperty(l,a);return S&&s(["delete",[a],u]),S},is:Object.is,canProxy:It,set(l,a,u,p){var S;let f=Reflect.get(l,a,p);if(this.is(f,u))return!0;let W=f?.[b];W&&W.delete(c(a)),G(u)&&(u=nt(u)||u);let x;return(S=Object.getOwnPropertyDescriptor(l,a))!=null&&S.set?x=u:u instanceof Promise?x=u.then(M=>(x[k]=M,s(["resolve",[a],M]),M)).catch(M=>{x[it]=M,s(["reject",[a],M])}):u?.[b]?(x=u,x[b].add(o(a))):this.canProxy(u)?(x=j(u),x[b].add(o(a))):x=u,Reflect.set(l,a,x,p),s(["set",[a],u,f]),!0}},T=new Proxy(d,y);return st.set(e,T),Reflect.ownKeys(e).forEach(l=>{let a=Object.getOwnPropertyDescriptor(e,l);a.get||a.set?Object.defineProperty(d,l,a):T[l]=e[l]}),T}function q(e){return G(e)?e[at]:void 0}function P(e,t,n){(V.env&&V.env.MODE)!=="production"&&!e?.[b]&&console.warn("Please use proxy object");let i,s=[],r=o=>{if(s.push(o),n){t(s.splice(0));return}i||(i=Promise.resolve().then(()=>{i=void 0,t(s.splice(0))}))};return e[b].add(r),()=>{e[b].delete(r)}}function N(e){return(V.env&&V.env.MODE)!=="production"&&!e?.[_]&&console.warn("Please use proxy object"),e[_]}function ct(e){for(;e.length>0;)e.pop();return e}var Z=(e,...t)=>{let n=typeof e=="function"?e(...t):e;return n??void 0},m=e=>e,Q=()=>{};var ut=(...e)=>(...t)=>{e.forEach(function(n){n?.(...t)})},ht=(()=>{let e=0;return()=>(e++,e.toString(36))})();var C=e=>Array.isArray(e);var O=e=>!(e==null||typeof e!="object"||C(e)),lt=e=>typeof e=="number"&&!Number.isNaN(e),v=e=>typeof e=="string",A=e=>typeof e=="function";function H(...e){let t=e.length===1?e[0]:e[1],n=e.length===2?e[0]:!0}function K(...e){let t=e.length===1?e[0]:e[1],n=e.length===2?e[0]:!0}function dt(e){return{predicate:t=>(n,i)=>{var s;return(s=e.find(r=>{var o;return r.guard=r.guard||(()=>!0),v(r.guard)?!!((o=t[r.guard])!=null&&o.call(t,n,i)):ft(r.guard)?r.guard.predicate(t!=null?t:{})(n,i):r.guard(n,i)}))==null?void 0:s.actions}}}function ft(e){return O(e)&&e.predicate!=null}function U(e,t){return(n,i)=>ft(e)?e.predicate(t!=null?t:{})(n,i):e}function kt(...e){return{predicate:t=>(n,i)=>e.map(s=>{var r;return v(s)?!!((r=t[s])!=null&&r.call(t,n,i)):A(s)?s(n,i):s.predicate(t)(n,i)}).some(Boolean)}}function Nt(...e){return{predicate:t=>(n,i)=>e.map(s=>{var r;return v(s)?!!((r=t[s])!=null&&r.call(t,n,i)):A(s)?s(n,i):s.predicate(t)(n,i)}).every(Boolean)}}function Rt(e){return{predicate:t=>(n,i)=>{var s;return v(e)?!((s=t[e])!=null&&s.call(t,n,i)):A(e)?!e(n,i):!e.predicate(t)(n,i)}}}var pt={or:kt,and:Nt,not:Rt};function Ft(e){return O(e)&&e.predicate!=null}var Wt=()=>!0;function z(e,t){return e=e!=null?e:Wt,(n,i)=>{if(v(e)){let s=t==null?void 0:t[e];return A(s)?s(n,i):s}return Ft(e)?e.predicate(t!=null?t:{})(n,i):e==null?void 0:e(n,i)}}function I(e){var t,n,i;if(Array.isArray(e)){for(n=Array(t=e.length);t--;)n[t]=(i=e[t])&&typeof i=="object"?I(i):i;return n}if(Object.prototype.toString.call(e)==="[object Object]"){n={};for(t in e)t==="__proto__"?Object.defineProperty(n,t,{value:I(e[t]),configurable:!0,enumerable:!0,writable:!0}):n[t]=(i=e[t])&&typeof i=="object"?I(i):i;return n}return e}function tt(e,t,n,i){return P(e,s=>{s.some(r=>r[1][0]===t)&&n(e[t])},i)}var se=Symbol();var D=new WeakMap,R=new WeakMap,vt=(e,t)=>{let n=D.get(e);n&&(n[0].forEach(i=>{let{d:s}=i;e!==s&&vt(s)}),++n[2],t&&n[3].add(t))},_t=(e,t)=>{let n=D.get(e);return n?.[2]?(n[3].add(t),!0):!1},Y=e=>{let t=D.get(e);t&&(--t[2],t[2]||(t[3].forEach(n=>n()),t[3].clear()),t[0].forEach(n=>{let{d:i}=n;e!==i&&Y(i)}))},Gt=e=>{let{s:t,d:n}=e,i=R.get(n);i||(i=[new Set],R.set(e.d,i)),i[0].add(e);let s=D.get(t);if(!s){let r=new Set,o=P(t,c=>{r.forEach(h=>{let{d,c:y,n:T,i:l}=h;t===d&&c.every(a=>a[1].length===1&&l.includes(a[1][0]))||h.p||(vt(t,y),T?Y(t):h.p=Promise.resolve().then(()=>{delete h.p,Y(t)}))})},!0);s=[r,o,0,new Set],D.set(t,s)}s[0].add(e)},St=e=>{let{s:t,d:n}=e,i=R.get(n);i?.[0].delete(e),i?.[0].size===0&&R.delete(n);let s=D.get(t);if(s){let[r,o]=s;r.delete(e),r.size||(o(),D.delete(t))}},Vt=e=>{let t=R.get(e);return t?Array.from(t[0]):[]};function Tt(e,t){let n=t?.proxy||j({}),i=!!t?.sync,s=Object.keys(e);return s.forEach(r=>{if(Object.getOwnPropertyDescriptor(n,r))throw new Error("object property already defined");let o=e[r],c=null,h=()=>{if(c&&(Array.from(c).map(([a])=>_t(a,h)).some(a=>a)||Array.from(c).every(([a,u])=>q(a)===u.v)))return;let d=new Map,T=o(a=>(d.set(a,{v:q(a)}),a)),l=()=>{d.forEach((a,u)=>{var p;let S=(p=c?.get(u))==null?void 0:p.s;if(S)a.s=S;else{let f={s:u,d:n,k:r,c:h,n:i,i:s};Gt(f),a.s=f}}),c?.forEach((a,u)=>{!d.has(u)&&a.s&&St(a.s)}),c=d};T instanceof Promise?T.finally(l):l(),n[r]=T};h()}),n}function yt(e,t){let n=t?.delete?new Set:null;Vt(e).forEach(i=>{let{k:s}=i;(!t?.keys||t.keys.includes(s))&&(St(i),n&&n.add(s))}),n&&n.forEach(i=>{delete e[i]})}function Et(e){var n;let t=j({value:"",previousValue:"",event:m({}),context:(n=e.context)!=null?n:m({}),done:!1,tags:[],hasTag(i){return this.tags.includes(i)},matches(...i){return i.includes(this.value)},can(i){return m(this).nextEvents.includes(i)},get nextEvents(){var r,o,c,h;let i=(c=(o=(r=e.states)==null?void 0:r[this.value])==null?void 0:o.on)!=null?c:{},s=(h=e==null?void 0:e.on)!=null?h:{};return Object.keys({...i,...s})},get changed(){return this.event.value==="machine.init"||!this.previousValue?!1:this.value!==this.previousValue}});return m(t)}function F(e,t){return(n,i)=>{if(lt(e))return e;if(A(e))return e(n,i);if(v(e)){let s=Number.parseFloat(e);if(!Number.isNaN(s))return s;if(t){let r=t==null?void 0:t[e];if(r==null){let o=`[machine] Cannot determine delay for ${e}. It doesn't exist in options.delays`;throw new Error(o)}return A(r)?r(n,i):r}}}}function E(e){return v(e)?{type:e}:e}function g(e){return e?C(e)?e:[e]:[]}function mt(e){return v(e)?{target:e}:e}function xt(e,t){return(n,i)=>g(e).map(mt).find(s=>{var c;let o=z(s.guard,t)(n,i);return(c=o!=null?o:s.target)!=null?c:s.actions})}function bt(e,t){let n=v(e)?mt(e):e,i=s=>(s.actions&&!s.target&&t&&(s.target=t),s);if(C(n))return n.map(i);if(O(n))return i(m(n))}var w=class{constructor(t,n){this.config=t;this.status="Not Started";this.type="machine";this.activityEvents=new Map;this.delayedEvents=new Map;this.stateListeners=new Set;this.eventListeners=new Set;this.doneListeners=new Set;this.contextWatchers=new Set;this.removeStateListener=Q;this.removeEventListener=Q;this.children=new Map;this.start=t=>{if(this.status==="Running")return this;this.status="Running";let n=E("machine.init");if(t){let r=O(t)?t:{context:this.config.context,value:t};this.setState(r.value),this.setContext(r.context)}let i={target:t?void 0:this.config.initial},s=this.getNextStateInfo(i,n);return s.target=m(s.target||i.target),this.initialState=s,this.performStateChangeEffects(s.target,s,n),this.removeStateListener=P(this.state,()=>{this.stateListeners.forEach(r=>{r(this.stateSnapshot)})},this.sync),this.removeEventListener=tt(this.state,"event",r=>{this.config.onEvent&&this.executeActions(this.config.onEvent,r);for(let o of this.eventListeners)o(r)}),this.setupContextWatchers(),this.executeActivities(E("machine.start"),g(this.config.activities),"machine.start"),this.executeActions(this.config.entry,E("machine.start")),this};this.setupContextWatchers=()=>{var t;for(let[n,i]of Object.entries((t=this.config.watch)!=null?t:{}))this.contextWatchers.add(tt(this.state.context,n,()=>{this.executeActions(i,this.state.event)}))};this.setupComputed=()=>{var i;let t=m((i=this.config.computed)!=null?i:{}),n=Object.fromEntries(Object.entries(t).map(([s,r])=>[s,o=>r(o(this.state.context))]));Tt(n,{proxy:this.state.context})};this.detachComputed=()=>{yt(this.state.context,{delete:!0})};this.stop=()=>{if(this.status!=="Stopped")return this.setState(null),this.setEvent("machine.stop"),this.config.context&&this.setContext(this.config.context),this.stopStateListeners(),this.stopChildren(),this.stopActivities(),this.stopDelayedEvents(),this.stopContextWatchers(),this.stopEventListeners(),this.detachComputed(),this.status="Stopped",this.executeActions(this.config.exit,E("machine.stop")),this};this.stopEventListeners=()=>{this.eventListeners.clear(),this.removeEventListener()};this.stopStateListeners=()=>{this.removeStateListener(),this.stateListeners.clear()};this.stopContextWatchers=()=>{this.contextWatchers.forEach(t=>t()),this.contextWatchers.clear()};this.stopDelayedEvents=()=>{this.delayedEvents.forEach(t=>{t.forEach(n=>n())}),this.delayedEvents.clear()};this.stopActivities=t=>{var n,i;t?((n=this.activityEvents.get(t))==null||n.forEach(s=>s()),(i=this.activityEvents.get(t))==null||i.clear(),this.activityEvents.delete(t)):(this.activityEvents.forEach(s=>{s.forEach(r=>r()),s.clear()}),this.activityEvents.clear())};this.sendChild=(t,n)=>{let i=E(t),s=Z(n,this.contextSnapshot),r=this.children.get(s);r||K(`[machine/send-child] Cannot send '${i.type}' event to unknown child`),r.send(i)};this.stopChild=t=>{this.children.has(t)||K("[machine/stop-child] Cannot stop unknown child"),this.children.get(t).stop(),this.children.delete(t)};this.removeChild=t=>{this.children.delete(t)};this.stopChildren=()=>{this.children.forEach(t=>t.stop()),this.children.clear()};this.setParent=t=>{this.parent=t};this.spawn=(t,n)=>{let i=Z(t);return n&&(i.id=n),i.type="machine.actor",i.setParent(this),this.children.set(i.id,m(i)),i.onDone(()=>{this.removeChild(i.id)}).start(),m(L(i))};this.addActivityCleanup=(t,n)=>{var i;!t||(this.activityEvents.has(t)?(i=this.activityEvents.get(t))==null||i.add(n):this.activityEvents.set(t,new Set([n])))};this.setState=t=>{this.state.previousValue=this.state.value,this.state.value=t;let n=this.getStateNode(t);t==null?ct(this.state.tags):this.state.tags=g(n==null?void 0:n.tags)};this.setContext=t=>{for(let n in t)this.state.context[n]=t[n]};this.withContext=t=>{this.detachComputed();let n={...this.config.context,...t};return new w({...this.config,context:n},this.options)};this.withOptions=t=>(this.detachComputed(),new w(this.config,{...this.options,...t}));this.setActions=t=>{this.actionMap={...this.actionMap,...t}};this.clone=()=>(this.detachComputed(),new w(this.config,this.options));this.getStateNode=t=>{var n;if(!!t)return(n=this.config.states)==null?void 0:n[t]};this.getNextStateInfo=(t,n)=>{var o;let i=this.determineTransition(t,n),s=(o=i==null?void 0:i.target)!=null?o:this.state.value,r=this.getStateNode(s);return{transition:i,stateNode:r,target:s}};this.getActionFromDelayedTransition=t=>{let n=E("machine.after"),s=F(t.delay,this.delayMap)(this.contextSnapshot,n),r;return{entry:()=>{r=globalThis.setTimeout(()=>{let o=this.state.value,c=this.getNextStateInfo(t,n);this.performStateChangeEffects(o,c,n)},s)},exit:()=>{globalThis.clearTimeout(r)}}};this.getDelayedEventActions=t=>{let n=this.getStateNode(t),i=E("machine.after");if(!n||!n.after)return;let s=[],r=[];if(C(n.after)){let o=this.determineTransition(n.after,i);if(!o)return;let c=this.getActionFromDelayedTransition(o);s.push(c.entry),r.push(c.exit)}else if(O(n.after))for(let o in n.after){let c=n.after[o],h={};if(C(c)){let y=this.determineTransition(c,i);y&&(h=y)}else v(c)?h={target:c,delay:o}:h={...c,delay:o};let d=this.getActionFromDelayedTransition(h);s.push(d.entry),r.push(d.exit)}return{entries:s,exits:r}};this.executeActions=(t,n)=>{var s;let i=U(t,this.guardMap)(this.contextSnapshot,n);for(let r of g(i)){let o=v(r)?(s=this.actionMap)==null?void 0:s[r]:r;H(v(r)&&!o,`[machine/exec-action] No implementation found for action: \`${r}\``),o==null||o(this.state.context,n,this.meta)}};this.executeActivities=(t,n,i)=>{var s;for(let r of n){let o=v(r)?(s=this.activityMap)==null?void 0:s[r]:r;if(!o){H(`[machine/exec-activity] No implementation found for activity: \`${r}\``);continue}let c=o(this.state.context,t,this.meta);c&&this.addActivityCleanup(i!=null?i:this.state.value,c)}};this.createEveryActivities=(t,n)=>{if(!t)return;let i=E("machine.every");if(C(t)){let s=g(t).find(h=>{let d=F(h.delay,this.delayMap);h.delay=d(this.contextSnapshot,i);let T=z(h.guard,this.guardMap)(this.contextSnapshot,i);return T!=null?T:h.delay});if(!s)return;let o=F(s.delay,this.delayMap)(this.contextSnapshot,i);n(()=>{let h=globalThis.setInterval(()=>{this.executeActions(s.actions,i)},o);return()=>{globalThis.clearInterval(h)}})}else for(let s in t){let r=t==null?void 0:t[s],c=F(s,this.delayMap)(this.contextSnapshot,i);n(()=>{let d=globalThis.setInterval(()=>{this.executeActions(r,i)},c);return()=>{globalThis.clearInterval(d)}})}};this.setEvent=t=>{this.state.event=L(E(t))};this.performExitEffects=(t,n)=>{let i=this.state.value,s=t?this.getStateNode(t):void 0;this.stopActivities(i);let r=U(s==null?void 0:s.exit,this.guardMap)(this.contextSnapshot,n),o=g(r),c=this.delayedEvents.get(i);c&&o.push(...c),this.executeActions(o,n),this.eventListeners.clear()};this.performEntryEffects=(t,n)=>{let i=this.getStateNode(t),s=g(i==null?void 0:i.activities);this.createEveryActivities(i==null?void 0:i.every,h=>{s.unshift(h)}),s.length>0&&this.executeActivities(n,s);let r=U(i==null?void 0:i.entry,this.guardMap)(this.contextSnapshot,n),o=g(r),c=this.getDelayedEventActions(t);(i==null?void 0:i.after)&&c&&(this.delayedEvents.set(t,c==null?void 0:c.exits),o.push(...c.entries)),this.executeActions(o,n),(i==null?void 0:i.type)==="final"&&(this.state.done=!0,this.doneListeners.forEach(h=>{h(this.stateSnapshot)}),this.stop())};this.performTransitionEffects=(t,n)=>{let i=this.determineTransition(t,n);this.executeActions(i==null?void 0:i.actions,n)};this.performStateChangeEffects=(t,n,i)=>{var r,o;this.setEvent(i),n.target=(o=(r=n.target)!=null?r:this.state.value)!=null?o:void 0;let s=n.target&&n.target!==this.state.value;s&&this.performExitEffects(t,i),this.performTransitionEffects(n==null?void 0:n.transition,i),this.setState(n.target),s&&this.performEntryEffects(n.target,i)};this.determineTransition=(t,n)=>{let i=xt(t,this.guardMap);return i==null?void 0:i(this.contextSnapshot,n)};this.sendParent=t=>{var i;this.parent||K("[machine/send-parent] Cannot send event to an unknown parent");let n=E(t);(i=this.parent)==null||i.send(n)};this.send=t=>{let n=E(t);this.transition(this.state.value,n)};this.transition=(t,n)=>{var h,d,y;let i=v(t)?this.getStateNode(t):t==null?void 0:t.stateNode,s=E(n);if(!i&&!this.config.on){let T=this.status==="Stopped"?"[machine/transition] Cannot transition a stopped machine":"[machine/transition] State does not have a definition";H(T);return}let r=(y=(h=i==null?void 0:i.on)==null?void 0:h[s.type])!=null?y:(d=this.config.on)==null?void 0:d[s.type],o=bt(r,this.state.value);if(!o)return;let c=this.getNextStateInfo(o,s);return this.performStateChangeEffects(this.state.value,c,s),c.stateNode};this.subscribe=t=>(this.stateListeners.add(t),this.status==="Running"&&t(this.stateSnapshot),()=>{this.stateListeners.delete(t)});this.onDone=t=>(this.doneListeners.add(t),this);this.onTransition=t=>(this.stateListeners.add(t),this.status==="Running"&&t(this.stateSnapshot),this);this.onEvent=t=>(t(this.state.event),this.eventListeners.add(t),this);var s,r,o,c,h,d,y,T,l,a,u;this.options=I(n),this.id=(s=t.id)!=null?s:`machine-${ht()}`,this.guardMap=(o=(r=this.options)==null?void 0:r.guards)!=null?o:{},this.actionMap=(h=(c=this.options)==null?void 0:c.actions)!=null?h:{},this.delayMap=(y=(d=this.options)==null?void 0:d.delays)!=null?y:{},this.activityMap=(l=(T=this.options)==null?void 0:T.activities)!=null?l:{},this.sync=(u=(a=this.options)==null?void 0:a.sync)!=null?u:!1,this.state=Et(I(t)),this.setupComputed();let i=E("machine.created");this.executeActions(t==null?void 0:t.created,i)}get stateSnapshot(){return m(N(this.state))}get contextSnapshot(){return this.stateSnapshot.context}get self(){let t=this;return{id:this.id,send:this.send.bind(this),sendParent:this.sendParent.bind(this),sendChild:this.sendChild.bind(this),stop:this.stop.bind(this),stopChild:this.stopChild.bind(this),spawn:this.spawn.bind(this),get state(){return t.stateSnapshot}}}get meta(){return{state:this.stateSnapshot,guards:this.guardMap,send:this.send.bind(this),listen:this.onEvent.bind(this),self:this.self,getState:()=>this.stateSnapshot}}},Ht=(e,t)=>new w(e,t);var Kt=(...e)=>e.map(t=>t==null?void 0:t.trim()).filter(Boolean).join(" ");function gt(...e){var n,i;let t={};for(let s of e){for(let r in t){if(/^on[A-Z]/.test(r)&&typeof t[r]=="function"&&typeof s[r]=="function"){t[r]=ut(t[r],s[r]);continue}if(r==="className"||r==="class"){t[r]=Kt(t[r],s[r]);continue}if(r==="style"){t[r]=Object.assign({},(n=t[r])!=null?n:{},(i=s[r])!=null?i:{});continue}t[r]=s[r]!==void 0?s[r]:t[r]}for(let r in s)t[r]===void 0&&(t[r]=s[r])}return t}
//# sourceMappingURL=index.js.map
{
"name": "@zag-js/core",
"version": "0.0.0-dev-20220412162814",
"version": "0.0.0-dev-20220412212252",
"description": "A minimal implementation of xstate fsm for UI machines",
"keywords": [
"ui-machines",
"state-machines",
"zag",
"fsm",

@@ -32,3 +34,3 @@ "xstate",

"dependencies": {
"@zag-js/utils": "^0.0.0-dev-20220412162814",
"@zag-js/utils": "^0.0.0-dev-20220412212252",
"klona": "^2.0.5",

@@ -35,0 +37,0 @@ "valtio": "^1.5.2"

@@ -1,2 +0,2 @@

import { proxy } from "valtio"
import { proxy } from "valtio/vanilla"
import { cast } from "@zag-js/utils"

@@ -3,0 +3,0 @@ import { ActionTypes, Dict, StateMachine as S } from "./types"

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc