xstate 5.32.1 → 6.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +7 -8
  2. package/dist/StateMachine-2dcdb1f5.esm.js +720 -0
  3. package/dist/StateMachine-2ea0a7fa.development.esm.js +737 -0
  4. package/dist/StateMachine-64813f46.cjs.js +723 -0
  5. package/dist/StateMachine-8d22a79c.development.cjs.js +740 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +27 -21
  8. package/dist/declarations/src/StateNode.d.ts +18 -54
  9. package/dist/declarations/src/actions.d.ts +14 -9
  10. package/dist/declarations/src/actors/callback.d.ts +42 -16
  11. package/dist/declarations/src/actors/index.d.ts +9 -6
  12. package/dist/declarations/src/actors/listener.d.ts +18 -0
  13. package/dist/declarations/src/actors/logic.d.ts +105 -0
  14. package/dist/declarations/src/actors/observable.d.ts +72 -27
  15. package/dist/declarations/src/actors/promise.d.ts +133 -48
  16. package/dist/declarations/src/actors/subscription.d.ts +22 -0
  17. package/dist/declarations/src/atom.d.ts +81 -0
  18. package/dist/declarations/src/createActor.d.ts +31 -33
  19. package/dist/declarations/src/createMachine.d.ts +35 -7
  20. package/dist/declarations/src/createMachineFromConfig.d.ts +176 -0
  21. package/dist/declarations/src/graph/graph.d.ts +4 -2
  22. package/dist/declarations/src/index.d.ts +12 -8
  23. package/dist/declarations/src/inspection.d.ts +84 -30
  24. package/dist/declarations/src/mapState.d.ts +4 -2
  25. package/dist/declarations/src/schema.types.d.ts +90 -0
  26. package/dist/declarations/src/serialize.d.ts +44 -0
  27. package/dist/declarations/src/setup.d.ts +251 -135
  28. package/dist/declarations/src/spawn.d.ts +6 -31
  29. package/dist/declarations/src/stateUtils.d.ts +21 -19
  30. package/dist/declarations/src/system.d.ts +8 -7
  31. package/dist/declarations/src/transition.d.ts +5 -5
  32. package/dist/declarations/src/types.d.ts +405 -405
  33. package/dist/declarations/src/types.v6.d.ts +392 -0
  34. package/dist/declarations/src/utils.d.ts +4 -3
  35. package/dist/index-93edd3bd.esm.js +4909 -0
  36. package/dist/index-9cb3b3a0.cjs.js +4968 -0
  37. package/dist/index-e61170ba.development.esm.js +4948 -0
  38. package/dist/index-f6ef20d1.development.cjs.js +5007 -0
  39. package/dist/xstate-actors.cjs.js +12 -838
  40. package/dist/xstate-actors.cjs.mjs +10 -5
  41. package/dist/xstate-actors.development.cjs.js +12 -838
  42. package/dist/xstate-actors.development.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.esm.js +1 -836
  44. package/dist/xstate-actors.esm.js +1 -836
  45. package/dist/xstate-actors.umd.min.js +1 -1
  46. package/dist/xstate-actors.umd.min.js.map +1 -1
  47. package/dist/xstate-graph.cjs.js +18 -29
  48. package/dist/xstate-graph.development.cjs.js +18 -29
  49. package/dist/xstate-graph.development.esm.js +15 -26
  50. package/dist/xstate-graph.esm.js +15 -26
  51. package/dist/xstate-graph.umd.min.js +1 -1
  52. package/dist/xstate-graph.umd.min.js.map +1 -1
  53. package/dist/xstate.cjs.js +1316 -112
  54. package/dist/xstate.cjs.mjs +22 -22
  55. package/dist/xstate.development.cjs.js +1316 -112
  56. package/dist/xstate.development.cjs.mjs +22 -22
  57. package/dist/xstate.development.esm.js +1278 -76
  58. package/dist/xstate.esm.js +1278 -76
  59. package/dist/xstate.umd.min.js +1 -1
  60. package/dist/xstate.umd.min.js.map +1 -1
  61. package/package.json +3 -47
  62. package/dist/StateMachine-45e79c35.esm.js +0 -560
  63. package/dist/StateMachine-be2e51a3.development.cjs.js +0 -569
  64. package/dist/StateMachine-e0178d48.development.esm.js +0 -566
  65. package/dist/StateMachine-e6732977.cjs.js +0 -563
  66. package/dist/assign-2aa58daa.esm.js +0 -127
  67. package/dist/assign-927227d6.development.cjs.js +0 -135
  68. package/dist/assign-e8f2bd75.cjs.js +0 -129
  69. package/dist/assign-f338cee3.development.esm.js +0 -133
  70. package/dist/declarations/src/actions/assign.d.ts +0 -45
  71. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  72. package/dist/declarations/src/actions/emit.d.ts +0 -44
  73. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  74. package/dist/declarations/src/actions/log.d.ts +0 -16
  75. package/dist/declarations/src/actions/raise.d.ts +0 -21
  76. package/dist/declarations/src/actions/send.d.ts +0 -42
  77. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  78. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  79. package/dist/declarations/src/actors/transition.d.ts +0 -143
  80. package/dist/declarations/src/dev/index.d.ts +0 -14
  81. package/dist/declarations/src/guards.d.ts +0 -128
  82. package/dist/log-410e5e55.development.esm.js +0 -429
  83. package/dist/log-7776fcf2.cjs.js +0 -409
  84. package/dist/log-c2e11c01.development.cjs.js +0 -437
  85. package/dist/log-fa75ec2d.esm.js +0 -401
  86. package/dist/raise-74097812.development.esm.js +0 -2823
  87. package/dist/raise-7c948725.cjs.js +0 -2816
  88. package/dist/raise-964cd4e9.development.cjs.js +0 -2878
  89. package/dist/raise-e974d1c9.esm.js +0 -2761
  90. package/dist/xstate-actions.cjs.d.mts +0 -2
  91. package/dist/xstate-actions.cjs.d.ts +0 -2
  92. package/dist/xstate-actions.cjs.js +0 -23
  93. package/dist/xstate-actions.cjs.mjs +0 -14
  94. package/dist/xstate-actions.development.cjs.js +0 -23
  95. package/dist/xstate-actions.development.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.esm.js +0 -4
  97. package/dist/xstate-actions.esm.js +0 -4
  98. package/dist/xstate-actions.umd.min.js +0 -2
  99. package/dist/xstate-actions.umd.min.js.map +0 -1
  100. package/dist/xstate-dev.cjs.d.mts +0 -2
  101. package/dist/xstate-dev.cjs.d.ts +0 -2
  102. package/dist/xstate-dev.cjs.js +0 -48
  103. package/dist/xstate-dev.cjs.mjs +0 -5
  104. package/dist/xstate-dev.development.cjs.js +0 -51
  105. package/dist/xstate-dev.development.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.esm.js +0 -45
  107. package/dist/xstate-dev.esm.js +0 -42
  108. package/dist/xstate-dev.umd.min.js +0 -2
  109. package/dist/xstate-dev.umd.min.js.map +0 -1
  110. package/dist/xstate-guards.cjs.d.mts +0 -2
  111. package/dist/xstate-guards.cjs.d.ts +0 -2
  112. package/dist/xstate-guards.cjs.js +0 -14
  113. package/dist/xstate-guards.cjs.mjs +0 -7
  114. package/dist/xstate-guards.development.cjs.js +0 -14
  115. package/dist/xstate-guards.development.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.esm.js +0 -2
  117. package/dist/xstate-guards.esm.js +0 -2
  118. package/dist/xstate-guards.umd.min.js +0 -2
  119. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -1,2 +1,2 @@
1
- !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,function(t){"use strict";class s{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const s={value:t,next:null};if(this._current)return this._last.next=s,void(this._last=s);this._current=s,this._last=s,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const e="xstate.stop";function i(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const r=t=>{const s=i();s&&s.register(t)};function o(t){setTimeout(()=>{throw t})}const n="function"==typeof Symbol&&Symbol.observable||"@@observable";function a(t,s,e){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:s)?.bind(r),complete:(i?t.complete:e)?.bind(r)}}function c(t,s){return`${t.sessionId}.${s}`}let h=0;let p=!1;let u=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const d={clock:{setTimeout:(t,s)=>setTimeout(t,s),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class l{constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new s(this._process.bind(this)),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=u.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this.systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const i={...d,...e},{clock:r,logger:n,parent:l,syncSnapshot:_,id:v,systemId:f,inspect:y}=i;this.system=l?l.system:function(t,s){const e=new Map,i=new Map,r=new WeakMap,o=new Set,n={},{clock:p,logger:u}=s,d={schedule:(t,s,e,i,r=Math.random().toString(36).slice(2))=>{const o={source:t,target:s,event:e,delay:i,id:r,startedAt:Date.now()},a=c(t,r);l._snapshot._scheduledEvents[a]=o;const h=p.setTimeout(()=>{delete n[a],delete l._snapshot._scheduledEvents[a],l._relay(t,s,e)},i);n[a]=h},cancel:(t,s)=>{const e=c(t,s),i=n[e];delete n[e],delete l._snapshot._scheduledEvents[e],void 0!==i&&p.clearTimeout(i)},cancelAll:t=>{for(const s in l._snapshot._scheduledEvents){const e=l._snapshot._scheduledEvents[s];e.source===t&&d.cancel(t,e.id)}}},l={_snapshot:{_scheduledEvents:(s?.snapshot&&s.snapshot.scheduler)??{}},_bookId:()=>"x:"+h++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const s=r.get(t);void 0!==s&&(i.delete(s),r.delete(t))},get:t=>i.get(t),getAll:()=>Object.fromEntries(i.entries()),_set:(t,s)=>{const e=i.get(t);if(e&&e!==s)throw new Error(`Actor with system ID '${t}' already exists.`);i.set(t,s),r.set(s,t)},inspect:t=>{const s=a(t);return o.add(s),{unsubscribe(){o.delete(s)}}},_sendInspectionEvent:s=>{if(!o.size)return;const e={...s,rootId:t.sessionId};o.forEach(t=>t.next?.(e))},_relay:(t,s,e)=>{l._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:s,event:e}),s._send(e)},scheduler:d,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const s in t){const{source:e,target:i,event:r,delay:o,id:n}=t[s];d.schedule(e,i,r,o,n)}},_clock:p,_logger:u};return l}(this,{clock:r,logger:n}),y&&!l&&this.system.inspect(a(y)),this.sessionId=this.system._bookId(),this.id=v??this.sessionId,this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=l,this._syncSnapshot=_,this.options=i,this.src=i.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const s=this.eventListeners.get(t.type),e=this.eventListeners.get("*");if(!s&&!e)return;const i=[...s?s.values():[],...e?e.values():[]];for(const s of i)try{s(t)}catch(t){o(t)}},actionExecutor:t=>{const s=()=>{if(this._actorScope.system._sendInspectionEvent({type:"@xstate.action",actorRef:this,action:{type:t.type,params:t.params}}),!t.exec)return;const s=p;try{p=!0,t.exec(t.info,t.params)}finally{p=s}};this._processingStatus===u.Running?s():this._deferred.push(s)}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),f&&(this.systemId=f,this.system._set(f,this)),this._initState(e?.snapshot??e?.state),f&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,s){let e;for(this._snapshot=t;e=this._deferred.shift();)try{e()}catch(s){this._deferred.length=0,this._snapshot={...t,status:"error",error:s}}switch(this._snapshot.status){case"active":for(const s of this.observers)try{s.next?.(t)}catch(t){o(t)}break;case"done":for(const s of this.observers)try{s.next?.(t)}catch(t){o(t)}this._stopProcedure(),this._complete(),this._doneEvent=(i=this.id,r=this._snapshot.output,{type:`xstate.done.actor.${i}`,output:r,actorId:i}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var i,r;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:s,snapshot:t})}subscribe(t,s,e){const i=a(t,s,e);if(this._processingStatus!==u.Stopped)this.observers.add(i);else switch(this._snapshot.status){case"done":try{i.complete?.()}catch(t){o(t)}break;case"error":{const t=this._snapshot.error;if(i.error)try{i.error(t)}catch(t){o(t)}else o(t);break}}return{unsubscribe:()=>{this.observers.delete(i)}}}on(t,s){let e=this.eventListeners.get(t);e||(e=new Set,this.eventListeners.set(t,e));const i=s.bind(void 0);return e.add(i),{unsubscribe:()=>{e.delete(i)}}}select(t,s=Object.is){return{subscribe:e=>{const i=a(e),r=this.getSnapshot();let o=t(r);return this.subscribe(e=>{const r=t(e);s(o,r)||(o=r,i.next?.(r))})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===u.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.systemId&&this.system._set(this.systemId,this),this._processingStatus=u.Running;const t={type:"xstate.init",input:this.options.input};this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let s,i;try{s=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){i={err:t}}if(i){const{err:t}=i;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(s,t),t.type===e&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===u.Stopped?this:(this.mailbox.clear(),this._processingStatus===u.NotStarted?(this._processingStatus=u.Stopped,this):(this.mailbox.enqueue({type:e}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){o(t)}this.observers.clear(),this.eventListeners.clear()}_reportError(t){if(!this.observers.size)return this._parent||o(t),void this.eventListeners.clear();let s=!1;for(const e of this.observers){const i=e.error;s||=!i;try{i?.(t)}catch(t){o(t)}}this.observers.clear(),this.eventListeners.clear(),s&&o(t)}_error(t){var s;this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${s=this.id}`,error:t,actorId:s})}_stopProcedure(){return this._processingStatus!==u.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new s(this._process.bind(this)),this._processingStatus=u.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==u.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:r)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[n](){return this}getSnapshot(){return this._snapshot}}function _(t,s){return{config:t,transition:(s,e,i)=>({...s,context:t(s.context,e,i)}),getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof s?s({input:e}):s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}const v=new WeakMap;const f="xstate.observable.next",y="xstate.observable.error",g="xstate.observable.complete";const b="xstate.promise.resolve",m="xstate.promise.reject",S=new WeakMap;const x=_(t=>{},void 0);t.createEmptyActor=function(){return function(t,...[s]){return new l(t,s)}(x)},t.fromCallback=function(t){return{config:t,start:(s,e)=>{const{self:i,system:r,emit:o}=e,n={receivers:void 0,dispose:void 0};v.set(i,n),n.dispose=t({input:s.input,system:r,self:i,sendBack:t=>{"stopped"!==i.getSnapshot().status&&i._parent&&r._relay(i,i._parent,t)},receive:t=>{n.receivers??=new Set,n.receivers.add(t)},emit:o})},transition:(t,s,i)=>{const r=v.get(i.self);return s.type===e?(t={...t,status:"stopped",error:void 0},v.delete(i.self),r.receivers?.clear(),r.dispose?.(),t):(r.receivers?.forEach(t=>t(s)),t)},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromEventObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case y:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case g:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i,emit:r})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e,emit:r}).subscribe({next:t=>{e._parent&&i._relay(e,e._parent,t)},error:t=>{i._relay(e,e,{type:y,data:t})},complete:()=>{i._relay(e,e,{type:g})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromObservable=function(t){return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case f:return{...t,context:s.data};case y:return{...t,status:"error",error:s.data,input:void 0,_subscription:void 0};case g:return{...t,status:"done",input:void 0,_subscription:void 0};case e:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:void 0,input:s,_subscription:void 0}),start:(s,{self:e,system:i,emit:r})=>{"done"!==s.status&&(s._subscription=t({input:s.input,system:i,self:e,emit:r}).subscribe({next:t=>{i._relay(e,e,{type:f,data:t})},error:t=>{i._relay(e,e,{type:y,data:t})},complete:()=>{i._relay(e,e,{type:g})}}))},getPersistedSnapshot:({_subscription:t,...s})=>s,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,s,i)=>{if("active"!==t.status)return t;switch(s.type){case b:{const e=s.data;return{...t,status:"done",output:e,input:void 0}}case m:return{...t,status:"error",error:s.data,input:void 0};case e:return S.get(i.self)?.abort(),S.delete(i.self),{...t,status:"stopped",input:void 0};default:return t}},start:(s,{self:e,system:i,emit:r})=>{if("active"!==s.status)return;const o=new AbortController;S.set(e,o);Promise.resolve(t({input:s.input,system:i,self:e,signal:o.signal,emit:r})).then(t=>{"active"===e.getSnapshot().status&&(S.delete(e),i._relay(e,e,{type:b,data:t}))},t=>{"active"===e.getSnapshot().status&&(S.delete(e),i._relay(e,e,{type:m,data:t}))})},getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,input:s}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}},t.fromTransition=_,Object.defineProperty(t,"__esModule",{value:!0})});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateActors={})}(this,function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s="@xstate.init",r="@xstate.stop";function i(t){return{type:s,input:t}}function n(t){setTimeout(()=>{throw t})}const o="function"==typeof Symbol&&Symbol.observable||"@@observable",c=new WeakMap;function a(t){return{start:(e,{self:s,system:r})=>{const i=e.input.actor;if(!i||"function"==typeof i.getSnapshot&&"stopped"===i.getSnapshot().status)return;const n=t(e.input,{self:s,system:r});if(!n)return;let o,a=!1;const h=()=>{a||(a=!0,n.unsubscribe(),o?.unsubscribe())},u=s._parent;u&&(o=u.subscribe({complete:h,error:h})),c.set(s,{unsubscribe:h})},transition:(t,e,{self:s})=>e.type===r?(c.get(s)?.unsubscribe(),c.delete(s),{...t,status:"stopped",error:void 0}):t,getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}function h(t,e,s){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,s())}function u(){return a(({actor:t,eventType:e,mapper:s},{self:r,system:i})=>{const n="*"!==e&&e.endsWith(".*");return t.on(n?"*":e,t=>{n&&!function(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const s=e.split("."),r=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],i=r[t];if("*"===e){return t===s.length-1}if(e!==i)return!1}return!0}(t.type,e)||h(r,i,()=>s(t))})})}const p=u(),d=Symbol.for("xstate.atom");function l(){return a(({actor:t,mappers:e},{self:s,system:r})=>{const{done:i,error:n,snapshot:o}=e;if((c=t)&&"object"==typeof c&&d in c){if(!o)return;return t.subscribe(t=>h(s,r,()=>o(t)))}var c;return t.subscribe({next:t=>{"done"===t.status&&i?h(s,r,()=>i(t.output)):"error"===t.status&&n?h(s,r,()=>n(t.error)):"active"===t.status&&o&&h(s,r,()=>o(t))},error:t=>{n&&h(s,r,()=>n(t))},complete:()=>{}})})}const f=l();function _(t){if(void 0!==t)return"number"==typeof t?t:function(t){const e=t.trim(),s=e.match(/^(\d+)ms$/i);if(s)return parseInt(s[1],10);const r=e.match(/^(\d*)(\.?)(\d*)s$/i);if(r){const t=r[1]?parseInt(r[1],10):0,e=!!r[2],s=r[3]?parseInt(r[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?s:0)}const i=e.match(/^P(?:(?<weeks>\d+(?:[.,]\d+)?)W)?(?:(?<days>\d+(?:[.,]\d+)?)D)?(?:T(?:(?<hours>\d+(?:[.,]\d+)?)H)?(?:(?<minutes>\d+(?:[.,]\d+)?)M)?(?:(?<seconds>\d+(?:[.,]\d+)?)S)?)?$/i);if(!i?.groups)return;const{weeks:n,days:o,hours:c,minutes:a,seconds:h}=i.groups;if(!(n||o||c||a||h))return;const u=t=>t?Number(t.replace(",",".")):0;return 7*u(n)*24*60*60*1e3+24*u(o)*60*60*1e3+60*u(c)*60*1e3+60*u(a)*1e3+1e3*u(h)}(t)}function y(t,e,s){const r="object"==typeof t,i=r?t:void 0;return{next:(r?t.next:t)?.bind(i),error:(r?t.error:e)?.bind(i),complete:(r?t.complete:s)?.bind(i)}}function v(t,e){return`${t.sessionId}.${e}`}const g=new WeakMap,m="xstate.logic.effect.resolve",b="xstate.logic.effect.reject",S="xstate.logic.effect.start";function x(t){let e=g.get(t);return e||(e=new Map,g.set(t,e)),e}function k(t,e){if(t?.length)for(const s of t)switch(s.type){case"emit":e.emit(s.event);break;case"sendBack":{const t=e.self._parent;t&&e.system._relay(e.self,t,s.event);break}case"raise":e.system._relay(e.self,e.self,s.event);break;case"effect":{if(!s.key){const t=s.exec();"function"==typeof t&&x(e.self).set(Symbol(),{cleanup:t});break}const t=x(e.self);if(t.has(s.key))break;const r=s.exec();t.set(s.key,{cleanup:"function"==typeof r?r:void 0});break}case"cleanupEffects":{const t=g.get(e.self);if(!t)break;for(const{cleanup:e}of t.values())e?.();g.delete(e.self);break}}}function w(t){const e=(e,s,i)=>{if("active"!==e.status)return[e,[]];if(s.type===r)return[{...e,status:"stopped",input:void 0},[{type:"cleanupEffects"}]];if(s.type===S)return[{...e,effects:{...e.effects,[s.key]:{status:"active"}}},[]];if(s.type===m)return[{...e,effects:{...e.effects,[s.key]:{status:"done",output:s.output}}},[]];if(s.type===b)return[{...e,effects:{...e.effects,[s.key]:{status:"error",error:s.error}}},[]];const n=[],o={},c={emit:t=>{n.push({type:"emit",event:t})},sendBack:t=>{n.push({type:"sendBack",event:t})},raise:t=>{n.push({type:"raise",event:t})},effect:(t,s)=>{if("string"==typeof t)return r=t,i=s,void(e.effects?.[r]||(n.push({type:"effect",key:r,exec:i}),o[r]={status:"active"}));var r,i;n.push({type:"effect",exec:t})}},a=t.run({context:e.context,event:s,input:e.input,system:i.system,self:i.self,emit:i.emit},c),h={...e,...a||{}};return("context"in e||void 0!==a?.context)&&(h.context=a?.context??e.context),(a?.effects||Object.keys(o).length)&&(h.effects={...e.effects,...a?.effects,...o}),[h,n]};return{id:t.id,config:t,transition:e,start:(t,s)=>{const[r,n]=e(t,i(t.input),s);Object.assign(t,r),k(n,s)},getInitialSnapshot:(e,s)=>{const r=function(t,e){return"function"==typeof t?t({input:e}):t}(t.context,s),i={status:"active",output:void 0,error:void 0,input:s};return void 0!==r&&(i.context=r),{...i}},getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}let E=!1;let I=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const A={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)};class L{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=this._process.bind(this),this.mailbox=new e(this._boundProcess),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=I.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._lastSourceRef=void 0,this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[],this.systemId=void 0,this.sessionId=void 0,this.system=void 0,this.trigger=void 0,this.src=void 0,this._deferred=[],this._pendingEffects=void 0;const r={...A,...s},{clock:i,logger:o,parent:c,syncSnapshot:a,id:h,systemId:u,inspect:p}=r;this.system=c?c.system:function(t,e){let s=0;const r=new Map,i=new Map,n=new WeakMap,o=new Set,c={},{clock:a,logger:h}=e,u=(t,e,s,r,i)=>{o.size&&t&&(t._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:r,id:i})},p={schedule:(t,e,s,r,i=Math.random().toString(36).slice(2))=>{u(t,e,s,r,i);const n={source:t,target:e,event:s,delay:r,id:i,startedAt:Date.now()},o=v(t,i);l._snapshot._scheduledEvents[o]=n;const h=a.setTimeout(()=>{delete c[o],delete l._snapshot._scheduledEvents[o],d(t,e,s)},r);c[o]=h},cancel:(t,e)=>{const s=v(t,e),r=c[s];delete c[s],delete l._snapshot._scheduledEvents[s],void 0!==r&&a.clearTimeout(r)},cancelAll:t=>{for(const e in l._snapshot._scheduledEvents){const s=l._snapshot._scheduledEvents[e];s.source===t&&p.cancel(t,s.id)}}},d=(t,e,s)=>{const r=e.logic;if("function"==typeof r?.isInternalEventType&&r.isInternalEventType(s.type)&&t!==e)throw new Error(`Internal event "${s.type}" cannot be sent to actor "${e.id}" from outside.`);e._lastSourceRef=t,e._send(s)},l={children:r,reverseKeyedActors:n,keyedActors:i,_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+s++,_register:(t,e)=>(r.set(t,e),t),_unregister:t=>{r.delete(t.sessionId);const e=n.get(t);void 0!==e&&(i.delete(e),n.delete(t))},get:t=>i.get(t),getAll:()=>Object.fromEntries(i.entries()),_set:(t,e)=>{const s=i.get(t);if(s&&s!==e)throw new Error(`Actor with system ID '${t}' already exists.`);i.set(t,e),n.set(e,t)},inspect:t=>{const e=y(t);return o.add(e),{unsubscribe(){o.delete(e)}}},_sendInspectionEvent:e=>{if(!o.size)return;const s={...e,rootId:t.sessionId};o.forEach(t=>t.next?.(s))},_relay:(t,e,s)=>{u(t,e,s),d(t,e,s)},scheduler:p,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:r,event:i,delay:n,id:o}=t[e];p.schedule(s,r,i,n,o)}},_clock:a,_logger:h,_timerStrategy:e.timers};return l}(this,{clock:i,logger:o,timers:r.timers}),p&&!c&&this.system.inspect(y(p)),this.sessionId=this.system._bookId(),this.id=h??this.sessionId,this.logger=s?.logger??this.system._logger,this.clock=s?.clock??this.system._clock,this._parent=c,this._syncSnapshot=a,this.options=r,this.src=r.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const e=this.eventListeners.get(t.type),s=this.eventListeners.get("*");if(e||s){if(e)for(const s of e)try{s(t)}catch(t){n(t)}if(s)for(const e of s)try{e(t)}catch(t){n(t)}}},actionExecutor:t=>{const e=()=>{if(this._collectedActions.push({type:t.type,params:t.params}),!t.exec)return;const e=E;try{E=!0,t.exec()}finally{E=e}};this._processingStatus===I.Running?e():this._deferred.push(e)}},this.send=this.send.bind(this),this.trigger=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}}),u&&(this.systemId=u,this.system._set(u,this)),this._collectedMicrosteps=[];let d=s?.snapshot??s?.state;if(d&&"object"==typeof d&&"_pendingEffects"in d){const{_pendingEffects:t,...e}=d;this._pendingEffects=t,d=e}try{this._snapshot=d?this.logic.restoreSnapshot?this.logic.restoreSnapshot(d,this._actorScope):d:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t},this._deferred.length=0}u&&"active"!==this._snapshot.status&&this.system._unregister(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_setErrorSnapshot(t,e=this._snapshot){this._snapshot={...e,status:"error",error:t}}_next(t){for(const e of this.observers)try{e.next?.(t)}catch(t){n(t)}}update(t,e){this._snapshot=t;for(let e=0;e<this._deferred.length;e++){const s=this._deferred[e];try{s()}catch(e){this._deferred.length=0,this._setErrorSnapshot(e,t);break}}switch(this._deferred.length=0,this._snapshot.status){case"active":this._next(t);break;case"done":{this._next(t),this._stopProcedure(),this._complete();const e=(s=this.id,r=this._snapshot.output,{type:`xstate.done.actor.${s}`,output:r,actorId:s});this._parent&&this.system._relay(this,this._parent,e);break}case"error":this._error(this._snapshot.error)}var s,r;this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps,actions:this._collectedActions,sent:this._collectedSent,eventType:e.type}),this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[]}subscribe(t,e,s){const r=y(t,e,s);if(this._processingStatus!==I.Stopped)this.observers.add(r);else switch(this._snapshot.status){case"done":try{r.complete?.()}catch(t){n(t)}break;case"error":{const t=this._snapshot.error;if(r.error)try{r.error(t)}catch(t){n(t)}else n(t);break}}return{unsubscribe:()=>{this.observers.delete(r)}}}on(t,e){let s=this.eventListeners.get(t);return s||(s=new Set,this.eventListeners.set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,r,i)=>{const n=y(s,r,i);let o=t(this.getSnapshot());return this.subscribe({next:s=>{const r=t(s);e(o,r)||(o=r,n.next?.(r))},error:n.error,complete:n.complete})},get:()=>t(this.get())}}start(){if(this._processingStatus===I.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.systemId&&this.system._set(this.systemId,this),this._processingStatus=I.Running;const t=i(this.options.input);this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(this.update(this._snapshot,t),this._pendingEffects){const t=this.options.timers??this.system._timerStrategy??"resume";for(const e of this._pendingEffects)"@xstate.raise"===e.type&&this.system.scheduler.schedule(this,this,e.event,M(t,e),e.id);this._pendingEffects=void 0}return this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._setErrorSnapshot(t),void this._error(t)}const[i,n]=Array.isArray(e)?e:[e,void 0];this.update(i,t),k(n,this._actorScope),t.type===r&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===I.Stopped?this:(this.mailbox.clear(),this._processingStatus===I.NotStarted?(this._processingStatus=I.Stopped,this):(this.mailbox.enqueue({type:r}),this.system._unregister(this),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){n(t)}this.observers.clear(),this.eventListeners.clear()}_error(t){if(this._stopProcedure(),this.observers.size){let e=!1;for(const s of this.observers){const r=s.error;e||=!r;try{r?.(t)}catch(t){n(t)}}this.observers.clear(),this.eventListeners.clear(),e&&n(t)}else this._parent||n(t),this.eventListeners.clear();var e;this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${e=this.id}`,error:t,actorId:e})}_stopProcedure(){this._processingStatus===I.Running&&(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._boundProcess),this._processingStatus=I.Stopped,this.system._unregister(this))}_send(t){this._processingStatus!==I.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){const e=this.logic.getPersistedSnapshot(this._snapshot,t),s=this.system._snapshot._scheduledEvents;let r;const i=Date.now();for(const t in s){const e=s[t];e.source===this&&e.target===this&&(r??=[]).push({type:"@xstate.raise",event:e.event,id:e.id,delay:e.delay,startedAt:e.startedAt,elapsed:Math.max(0,i-e.startedAt)})}return r??=this._pendingEffects,r?{...e,_pendingEffects:r}:e}[o](){return this}getSnapshot(){return this.get()}get(){return this._snapshot}}function M(t,e){if("function"==typeof t)return t(e);switch(t){case"restart":return e.delay;case"absolute":return Math.max(0,e.startedAt+e.delay-Date.now());default:return Math.max(0,e.delay-e.elapsed)}}const T=new WeakMap;const P="xstate.observable.next",R="xstate.observable.error",$="xstate.observable.complete";const j="xstate.async.resolve",O="xstate.async.reject";class W extends Error{constructor(t){super(`Async logic timed out after ${t}.`),this.name="TimeoutError"}}const N=w({context:void 0,run:()=>{}});t.TimeoutError=W,t.createAsyncLogic=function(t){const e=t;return w({id:e.id,schemas:e.schemas,context:void 0,run:({event:t,input:r,self:i,system:n},o)=>{switch(t.type){case j:{const e=t.data??t.output;return{status:"done",output:e,input:void 0,effects:{async:{status:"done",output:e}}}}case O:{const e=t.data??t.error;return{status:"error",error:e,input:void 0,effects:{async:{status:"error",error:e}}}}}if(t.type===s)return o.effect(()=>{const t=i,s=new AbortController,c=e.timeout,a=_(c),h=void 0===a?void 0:n._clock.setTimeout(()=>{"active"===i.getSnapshot().status&&(s.abort(),n._relay(t,t,{type:O,data:new W(c)}))},a),u=()=>{void 0!==h&&n._clock.clearTimeout(h)};return Promise.resolve(e.run({input:r,system:n,self:i,signal:s.signal},{emit:o.emit,step:async(e,s)=>{const r=i.getSnapshot().effects?.[e];if("done"===r?.status)return r.output;if("error"===r?.status)throw r.error;if("active"===r?.status)return function(t,e){return new Promise((s,r)=>{const i=t.subscribe(t=>{const n=t.effects?.[e];"done"===n?.status?(i.unsubscribe(),s(n.output)):"error"===n?.status&&(i.unsubscribe(),r(n.error))})})}(i,e);n._relay(t,t,{type:S,key:e});try{const r=await s();return n._relay(t,t,{type:m,key:e,output:r}),r}catch(s){throw n._relay(t,t,{type:b,key:e,error:s}),s}}})).then(e=>{u(),"active"===i.getSnapshot().status&&n._relay(t,t,{type:j,data:e})},e=>{u(),"active"===i.getSnapshot().status&&n._relay(t,t,{type:O,data:e})}),()=>{s.abort(),u()}}),{effects:{async:{status:"active"}}}}})},t.createCallbackLogic=function(t){const e="function"==typeof t?t:t.run;return w({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,s)=>{const{event:r,input:i,self:n,system:o}=t,c=t.emit,a=T.get(n);a?.receivers?.forEach(t=>t(r)),s.effect("callback",()=>{const t={receivers:void 0,dispose:void 0};return T.set(n,t),t.dispose=e({input:i,system:o,self:n,sendBack:t=>{if("stopped"===n.getSnapshot().status)return;const e=n._parent;e&&o._relay(n,e,t)},receive:e=>{t.receivers??=new Set,t.receivers.add(e)},emit:c}),()=>{T.delete(n),t.receivers?.clear(),t.dispose?.()}})}})},t.createEmptyActor=function(){return new L(N,t);var t},t.createEventObservableLogic=function(t){const e="function"==typeof t?t:t.run;return{config:e,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case R:return{...t,status:"error",error:e.data,input:void 0,_subscription:void 0};case $:return{...t,status:"done",input:void 0,_subscription:void 0};case r:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(t,{self:s,system:r,emit:i})=>{"done"!==t.status&&(t._subscription=e({input:t.input,system:r,self:s,emit:i}).subscribe({next:t=>{s._parent&&r._relay(s,s._parent,t)},error:t=>{r._relay(s,s,{type:R,data:t})},complete:()=>{r._relay(s,s,{type:$})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})}},t.createListenerLogic=u,t.createLogic=w,t.createObservableLogic=function(t){const e="function"==typeof t?t:t.run;return w({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,s)=>{const{event:r,input:i,self:n,system:o}=t,c=t.emit;switch(r.type){case P:return{context:r.data};case R:return{status:"error",error:r.data,input:void 0,effects:{observable:{status:"error",error:r.data}}};case $:return{status:"done",input:void 0,effects:{observable:{status:"done"}}}}s.effect("observable",()=>{const t=n,s=e({input:i,system:o,self:n,emit:c}).subscribe({next:e=>{o._relay(t,t,{type:P,data:e})},error:e=>{o._relay(t,t,{type:R,data:e})},complete:()=>{o._relay(t,t,{type:$})}});return()=>s.unsubscribe()})}})},t.createSubscriptionLogic=l,t.listenerLogic=p,t.subscriptionLogic=f,Object.defineProperty(t,"__esModule",{value:!0})});
2
2
  //# sourceMappingURL=xstate-actors.umd.min.js.map