xstate 5.0.0-beta.25 → 5.0.0-beta.27
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.
- package/actions/dist/xstate-actions.cjs.js +20 -18
- package/actions/dist/xstate-actions.cjs.mjs +2 -0
- package/actions/dist/xstate-actions.development.cjs.js +20 -18
- package/actions/dist/xstate-actions.development.cjs.mjs +2 -0
- package/actions/dist/xstate-actions.development.esm.js +1 -1
- package/actions/dist/xstate-actions.esm.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +37 -14
- package/actors/dist/xstate-actors.cjs.mjs +0 -5
- package/actors/dist/xstate-actors.development.cjs.js +37 -14
- package/actors/dist/xstate-actors.development.cjs.mjs +0 -5
- package/actors/dist/xstate-actors.development.esm.js +34 -1
- package/actors/dist/xstate-actors.esm.js +34 -1
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/{actions-5943a9db.esm.js → actions-020463e9.esm.js} +76 -532
- package/dist/{actions-cf69419d.development.esm.js → actions-9754d2ca.development.esm.js} +76 -532
- package/dist/{actions-0971b43d.development.cjs.js → actions-ca622922.development.cjs.js} +81 -546
- package/dist/{actions-319cefe7.cjs.js → actions-d1dba4ac.cjs.js} +81 -546
- package/dist/declarations/src/Machine.d.ts +4 -3
- package/dist/declarations/src/State.d.ts +3 -3
- package/dist/declarations/src/StateNode.d.ts +0 -9
- package/dist/declarations/src/actions/assign.d.ts +6 -11
- package/dist/declarations/src/actions/cancel.d.ts +5 -12
- package/dist/declarations/src/actions/choose.d.ts +7 -6
- package/dist/declarations/src/actions/log.d.ts +5 -20
- package/dist/declarations/src/actions/pure.d.ts +9 -8
- package/dist/declarations/src/actions/raise.d.ts +6 -5
- package/dist/declarations/src/actions/send.d.ts +7 -12
- package/dist/declarations/src/actions/stop.d.ts +5 -12
- package/dist/declarations/src/actions.d.ts +2 -0
- package/dist/declarations/src/actors/index.d.ts +0 -20
- package/dist/declarations/src/actors/promise.d.ts +2 -1
- package/dist/declarations/src/constants.d.ts +7 -0
- package/dist/declarations/src/guards.d.ts +2 -2
- package/dist/declarations/src/index.d.ts +14 -14
- package/dist/declarations/src/typegenTypes.d.ts +1 -1
- package/dist/declarations/src/types.d.ts +18 -22
- package/dist/declarations/src/utils.d.ts +1 -2
- package/dist/promise-2ad94e3b.development.esm.js +406 -0
- package/dist/promise-3b7e3357.development.cjs.js +412 -0
- package/dist/promise-5b07c38e.esm.js +406 -0
- package/dist/promise-7a8c1768.cjs.js +412 -0
- package/dist/xstate.cjs.js +78 -102
- package/dist/xstate.development.cjs.js +78 -102
- package/dist/xstate.development.esm.js +6 -30
- package/dist/xstate.esm.js +6 -30
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +6 -6
- package/guards/dist/xstate-guards.development.cjs.js +6 -6
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -2,21 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var guards_dist_xstateGuards = require('../../dist/actions-d1dba4ac.cjs.js');
|
|
6
|
+
var promise = require('../../dist/promise-7a8c1768.cjs.js');
|
|
6
7
|
require('../../dev/dist/xstate-dev.cjs.js');
|
|
7
8
|
|
|
9
|
+
function isActorRef(item) {
|
|
10
|
+
return !!item && typeof item === 'object' && typeof item.send === 'function';
|
|
11
|
+
}
|
|
8
12
|
|
|
13
|
+
// TODO: refactor the return type, this could be written in a better way
|
|
14
|
+
// but it's best to avoid unneccessary breaking changes now
|
|
15
|
+
// @deprecated use `interpret(actorLogic)` instead
|
|
16
|
+
function toActorRef(actorRefLike) {
|
|
17
|
+
return {
|
|
18
|
+
subscribe: () => ({
|
|
19
|
+
unsubscribe: () => void 0
|
|
20
|
+
}),
|
|
21
|
+
id: 'anonymous',
|
|
22
|
+
sessionId: '',
|
|
23
|
+
getSnapshot: () => undefined,
|
|
24
|
+
// TODO: this isn't safe
|
|
25
|
+
[guards_dist_xstateGuards.symbolObservable]: function () {
|
|
26
|
+
return this;
|
|
27
|
+
},
|
|
28
|
+
status: guards_dist_xstateGuards.ActorStatus.Running,
|
|
29
|
+
stop: () => void 0,
|
|
30
|
+
...actorRefLike
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const emptyLogic = promise.fromTransition(_ => undefined, undefined);
|
|
34
|
+
function createEmptyActor() {
|
|
35
|
+
return guards_dist_xstateGuards.createActor(emptyLogic);
|
|
36
|
+
}
|
|
9
37
|
|
|
10
|
-
exports.
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
13
|
-
exports.
|
|
14
|
-
exports.
|
|
15
|
-
exports.
|
|
16
|
-
exports.isActorRef =
|
|
17
|
-
exports.
|
|
18
|
-
exports.startSignal = actors_dist_xstateActors.startSignal;
|
|
19
|
-
exports.startSignalType = actors_dist_xstateActors.startSignalType;
|
|
20
|
-
exports.stopSignal = actors_dist_xstateActors.stopSignal;
|
|
21
|
-
exports.stopSignalType = actors_dist_xstateActors.stopSignalType;
|
|
22
|
-
exports.toActorRef = actors_dist_xstateActors.toActorRef;
|
|
38
|
+
exports.fromCallback = promise.fromCallback;
|
|
39
|
+
exports.fromEventObservable = promise.fromEventObservable;
|
|
40
|
+
exports.fromObservable = promise.fromObservable;
|
|
41
|
+
exports.fromPromise = promise.fromPromise;
|
|
42
|
+
exports.fromTransition = promise.fromTransition;
|
|
43
|
+
exports.createEmptyActor = createEmptyActor;
|
|
44
|
+
exports.isActorRef = isActorRef;
|
|
45
|
+
exports.toActorRef = toActorRef;
|
|
@@ -2,21 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var guards_dist_xstateGuards = require('../../dist/actions-ca622922.development.cjs.js');
|
|
6
|
+
var promise = require('../../dist/promise-3b7e3357.development.cjs.js');
|
|
6
7
|
require('../../dev/dist/xstate-dev.development.cjs.js');
|
|
7
8
|
|
|
9
|
+
function isActorRef(item) {
|
|
10
|
+
return !!item && typeof item === 'object' && typeof item.send === 'function';
|
|
11
|
+
}
|
|
8
12
|
|
|
13
|
+
// TODO: refactor the return type, this could be written in a better way
|
|
14
|
+
// but it's best to avoid unneccessary breaking changes now
|
|
15
|
+
// @deprecated use `interpret(actorLogic)` instead
|
|
16
|
+
function toActorRef(actorRefLike) {
|
|
17
|
+
return {
|
|
18
|
+
subscribe: () => ({
|
|
19
|
+
unsubscribe: () => void 0
|
|
20
|
+
}),
|
|
21
|
+
id: 'anonymous',
|
|
22
|
+
sessionId: '',
|
|
23
|
+
getSnapshot: () => undefined,
|
|
24
|
+
// TODO: this isn't safe
|
|
25
|
+
[guards_dist_xstateGuards.symbolObservable]: function () {
|
|
26
|
+
return this;
|
|
27
|
+
},
|
|
28
|
+
status: guards_dist_xstateGuards.ActorStatus.Running,
|
|
29
|
+
stop: () => void 0,
|
|
30
|
+
...actorRefLike
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const emptyLogic = promise.fromTransition(_ => undefined, undefined);
|
|
34
|
+
function createEmptyActor() {
|
|
35
|
+
return guards_dist_xstateGuards.createActor(emptyLogic);
|
|
36
|
+
}
|
|
9
37
|
|
|
10
|
-
exports.
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
13
|
-
exports.
|
|
14
|
-
exports.
|
|
15
|
-
exports.
|
|
16
|
-
exports.isActorRef =
|
|
17
|
-
exports.
|
|
18
|
-
exports.startSignal = actors_dist_xstateActors.startSignal;
|
|
19
|
-
exports.startSignalType = actors_dist_xstateActors.startSignalType;
|
|
20
|
-
exports.stopSignal = actors_dist_xstateActors.stopSignal;
|
|
21
|
-
exports.stopSignalType = actors_dist_xstateActors.stopSignalType;
|
|
22
|
-
exports.toActorRef = actors_dist_xstateActors.toActorRef;
|
|
38
|
+
exports.fromCallback = promise.fromCallback;
|
|
39
|
+
exports.fromEventObservable = promise.fromEventObservable;
|
|
40
|
+
exports.fromObservable = promise.fromObservable;
|
|
41
|
+
exports.fromPromise = promise.fromPromise;
|
|
42
|
+
exports.fromTransition = promise.fromTransition;
|
|
43
|
+
exports.createEmptyActor = createEmptyActor;
|
|
44
|
+
exports.isActorRef = isActorRef;
|
|
45
|
+
exports.toActorRef = toActorRef;
|
|
@@ -1,2 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
import { a6 as symbolObservable, H as ActorStatus, E as createActor } from '../../dist/actions-9754d2ca.development.esm.js';
|
|
2
|
+
import { d as fromTransition } from '../../dist/promise-2ad94e3b.development.esm.js';
|
|
3
|
+
export { f as fromCallback, a as fromEventObservable, b as fromObservable, c as fromPromise, d as fromTransition } from '../../dist/promise-2ad94e3b.development.esm.js';
|
|
2
4
|
import '../../dev/dist/xstate-dev.development.esm.js';
|
|
5
|
+
|
|
6
|
+
function isActorRef(item) {
|
|
7
|
+
return !!item && typeof item === 'object' && typeof item.send === 'function';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// TODO: refactor the return type, this could be written in a better way
|
|
11
|
+
// but it's best to avoid unneccessary breaking changes now
|
|
12
|
+
// @deprecated use `interpret(actorLogic)` instead
|
|
13
|
+
function toActorRef(actorRefLike) {
|
|
14
|
+
return {
|
|
15
|
+
subscribe: () => ({
|
|
16
|
+
unsubscribe: () => void 0
|
|
17
|
+
}),
|
|
18
|
+
id: 'anonymous',
|
|
19
|
+
sessionId: '',
|
|
20
|
+
getSnapshot: () => undefined,
|
|
21
|
+
// TODO: this isn't safe
|
|
22
|
+
[symbolObservable]: function () {
|
|
23
|
+
return this;
|
|
24
|
+
},
|
|
25
|
+
status: ActorStatus.Running,
|
|
26
|
+
stop: () => void 0,
|
|
27
|
+
...actorRefLike
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const emptyLogic = fromTransition(_ => undefined, undefined);
|
|
31
|
+
function createEmptyActor() {
|
|
32
|
+
return createActor(emptyLogic);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { createEmptyActor, isActorRef, toActorRef };
|
|
@@ -1,2 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
import { a6 as symbolObservable, H as ActorStatus, E as createActor } from '../../dist/actions-020463e9.esm.js';
|
|
2
|
+
import { d as fromTransition } from '../../dist/promise-5b07c38e.esm.js';
|
|
3
|
+
export { f as fromCallback, a as fromEventObservable, b as fromObservable, c as fromPromise, d as fromTransition } from '../../dist/promise-5b07c38e.esm.js';
|
|
2
4
|
import '../../dev/dist/xstate-dev.esm.js';
|
|
5
|
+
|
|
6
|
+
function isActorRef(item) {
|
|
7
|
+
return !!item && typeof item === 'object' && typeof item.send === 'function';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// TODO: refactor the return type, this could be written in a better way
|
|
11
|
+
// but it's best to avoid unneccessary breaking changes now
|
|
12
|
+
// @deprecated use `interpret(actorLogic)` instead
|
|
13
|
+
function toActorRef(actorRefLike) {
|
|
14
|
+
return {
|
|
15
|
+
subscribe: () => ({
|
|
16
|
+
unsubscribe: () => void 0
|
|
17
|
+
}),
|
|
18
|
+
id: 'anonymous',
|
|
19
|
+
sessionId: '',
|
|
20
|
+
getSnapshot: () => undefined,
|
|
21
|
+
// TODO: this isn't safe
|
|
22
|
+
[symbolObservable]: function () {
|
|
23
|
+
return this;
|
|
24
|
+
},
|
|
25
|
+
status: ActorStatus.Running,
|
|
26
|
+
stop: () => void 0,
|
|
27
|
+
...actorRefLike
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const emptyLogic = fromTransition(_ => undefined, undefined);
|
|
31
|
+
function createEmptyActor() {
|
|
32
|
+
return createActor(emptyLogic);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { createEmptyActor, isActorRef, toActorRef };
|
|
@@ -1,2 +1,2 @@
|
|
|
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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}let s=function(t){return t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t}({});function i(t,e){const i=`${s.DoneInvoke}.${t}`,n={type:i,output:e,toString:()=>i};return n}function n(t,e){const i=`${s.ErrorPlatform}.${t}`,n={type:i,data:e,toString:()=>i};return n}function r(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const o=t=>{const e=r();e&&e.register(t)};function a(t){setTimeout((()=>{throw t}))}const c="function"==typeof Symbol&&Symbol.observable||"@@observable";let u=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const d={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class h{constructor(t,s){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=u.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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,...s},{clock:n,logger:r,parent:o,id:a,systemId:c}=i;this.system=o?.system??function(){let t=0;const e=new Map,s=new Map,i=new WeakMap;return{_bookId:()=>"x:"+t++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const n=i.get(t);void 0!==n&&(s.delete(n),i.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const n=s.get(t);if(n&&n!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),i.set(e,t)}}}(),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=r,this.clock=n,this._parent=o,this.options=i,this.src=i.src,this.ref=this,this._actorContext={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()}},this.send=this.send.bind(this),this._initState()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let s;for(;s=this._deferred.shift();)s();for(const t of this.observers)try{t.next?.(e)}catch(t){a(t)}const r=this.logic.getStatus?.(t);switch(r?.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=i(this.id,r.data),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(r.data),this._parent?.send(n(this.id,r.data))}}subscribe(t,e,s){const i=function(t,e,s){const i="object"==typeof t,n=i?t:void 0;return{next:(i?t.next:t)?.bind(n),error:(i?t.error:e)?.bind(n),complete:(i?t.complete:s)?.bind(n)}}(t,e,s);if(this.status!==u.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){a(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this.status===u.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=u.Running;const t=this.logic.getStatus?.(this._state);switch(t?.status){case"done":this.update(this._state);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorContext)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(n(this.id,t)),this}return this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorContext)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._stopProcedure(),this._error(t),void this._parent?.send(n(this.id,t))}this.update(e),t.type===v&&(this._stopProcedure(),this._complete())}_stop(){return this.status===u.Stopped?this:(this.mailbox.clear(),this.status===u.NotStarted?(this.status=u.Stopped,this):(this.mailbox.enqueue({type:v}),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){a(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||a(t));let e=!1;for(const s of this.observers){const i=s.error;e||=!i;try{i?.(t)}catch(t){a(t)}}this.observers.clear(),e&&a(t)}_stopProcedure(){if(this.status!==u.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this.status=u.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==u.Stopped){if(this.status!==u.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend({event:t,id:e,delay:s,to:i}){const n=this.clock.setTimeout((()=>{i?i.send(t):this.send(t)}),s);e&&(this.delayedEventsMap[e]=n)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:o)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[c](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function p(t,e){return{config:t,transition:(e,s,i)=>t(e,s,i),getInitialState:(t,s)=>"function"==typeof e?e({input:s}):e,getSnapshot:t=>t,getPersistedState:t=>t,restoreState:t=>t}}const l="$$xstate.resolve",f="$$xstate.reject";const _="xstate.init",v="xstate.stop";function y(t){return t.type===_||t.type===v}const g=p((t=>{}),void 0);t.createEmptyActor=function(){return new h(g,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:e})=>{e.send({type:_})},transition:(e,s,{self:r,id:o,system:a})=>{if(s.type===_){const s=t=>{e.canceled||r._parent?.send(t)},u=t=>{e.receivers.add(t)};return e.dispose=t({input:e.input,system:a,self:r,sendBack:s,receive:u}),((c=e.dispose)instanceof Promise||null!==c&&("function"==typeof c||"object"==typeof c)&&"function"==typeof c.then)&&e.dispose.then((t=>{r._parent?.send(i(o,t)),e.canceled=!0}),(t=>{e.canceled=!0,r._parent?.send(n(o,t))})),e}var c;return s.type===v?(e.canceled=!0,"function"==typeof e.dispose&&e.dispose(),e):(y(s)||e.receivers.forEach((t=>t(s))),e)},getInitialState:(t,e)=>({canceled:!1,receivers:new Set,dispose:void 0,input:e}),getSnapshot:()=>{},getPersistedState:({input:t,canceled:e})=>({input:t,canceled:e})}},t.fromEventObservable=function(t){const e="$$xstate.error",s="$$xstate.complete";return{config:t,transition:(t,i)=>{if("active"!==t.status)return t;switch(i.type){case e:return{...t,status:"error",input:void 0,data:i.data,subscription:void 0};case s:return{...t,status:"done",input:void 0,subscription:void 0};case v:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(i,{self:n,system:r})=>{"done"!==i.status&&(i.subscription=t({input:i.input,system:r,self:n}).subscribe({next:t=>{n._parent?.send(t)},error:t=>{n.send({type:e,data:t})},complete:()=>{n.send({type:s})}}))},getSnapshot:t=>{},getPersistedState:({status:t,data:e,input:s})=>({status:t,data:e,input:s}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",s="$$xstate.error",i="$$xstate.complete";return{config:t,transition:(t,n,{self:r,id:o,defer:a})=>{if("active"!==t.status)return t;switch(n.type){case e:return a((()=>{r._parent?.send({type:`xstate.snapshot.${o}`,data:n.data})})),{...t,data:n.data};case s:return{...t,status:"error",input:void 0,data:n.data,subscription:void 0};case i:return{...t,status:"done",input:void 0,subscription:void 0};case v:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(n,{self:r,system:o})=>{"done"!==n.status&&(n.subscription=t({input:n.input,system:o,self:r}).subscribe({next:t=>{r.send({type:e,data:t})},error:t=>{r.send({type:s,data:t})},complete:()=>{r.send({type:i})}}))},getSnapshot:t=>t.data,getPersistedState:({status:t,data:e,input:s})=>({status:t,data:e,input:s}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case l:return{...t,status:"done",data:e.data,input:void 0};case f:return{...t,status:"error",data:e.data,input:void 0};case v:return{...t,status:"canceled",input:void 0};default:return t}},start:(e,{self:s,system:i})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:i,self:s})).then((t=>{"active"===s._state.status&&s.send({type:l,data:t})}),(t=>{"active"===s._state.status&&s.send({type:f,data:t})}))},getInitialState:(t,e)=>({status:"active",data:void 0,input:e}),getSnapshot:t=>t.data,getStatus:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=p,t.isActorRef=function(t){return!!t&&"object"==typeof t&&"function"==typeof t.send},t.isSignal=y,t.startSignal={type:"xstate.init"},t.startSignalType=_,t.stopSignal={type:"xstate.stop"},t.stopSignalType=v,t.toActorRef=function(t){return{subscribe:()=>({unsubscribe:()=>{}}),id:"anonymous",sessionId:"",getSnapshot:()=>{},[c]:function(){return this},status:u.Running,stop:()=>{},...t}},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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}let s=function(t){return t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t}({});const i="xstate.init",r="xstate.stop";function n(t,e){const i=function(t){return`${s.DoneInvoke}.${t}`}(t),r={type:i,output:e,toString:()=>i};return r}function o(t,e){const i=function(t){return`${s.ErrorPlatform}.${t}`}(t),r={type:i,data:e,toString:()=>i};return r}function a(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const c=t=>{const e=a();e&&e.register(t)};function u(t){setTimeout((()=>{throw t}))}const d="function"==typeof Symbol&&Symbol.observable||"@@observable";let h=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const p={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class l{constructor(t,s){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=h.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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={...p,...s},{clock:r,logger:n,parent:o,id:a,systemId:c}=i;this.system=o?.system??function(){let t=0;const e=new Map,s=new Map,i=new WeakMap;return{_bookId:()=>"x:"+t++,_register:(t,s)=>(e.set(t,s),t),_unregister:t=>{e.delete(t.sessionId);const r=i.get(t);void 0!==r&&(s.delete(r),i.delete(t))},get:t=>s.get(t),_set:(t,e)=>{const r=s.get(t);if(r&&r!==e)throw new Error(`Actor with system ID '${t}' already exists.`);s.set(t,e),i.set(e,t)}}}(),c&&(this._systemId=c,this.system._set(c,this)),this.sessionId=this.system._bookId(),this.id=a??this.sessionId,this.logger=n,this.clock=r,this._parent=o,this.options=i,this.src=i.src,this.ref=this,this._actorContext={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()}},this.send=this.send.bind(this),this._initState()}_initState(){this._state=this.options.state?this.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let s;for(;s=this._deferred.shift();)s();for(const t of this.observers)try{t.next?.(e)}catch(t){u(t)}const i=this.logic.getStatus?.(t);switch(i?.status){case"done":this._stopProcedure(),this._complete(),this._doneEvent=n(this.id,i.data),this._parent?.send(this._doneEvent);break;case"error":this._stopProcedure(),this._error(i.data),this._parent?.send(o(this.id,i.data))}}subscribe(t,e,s){const i=function(t,e,s){const i="object"==typeof t,r=i?t:void 0;return{next:(i?t.next:t)?.bind(r),error:(i?t.error:e)?.bind(r),complete:(i?t.complete:s)?.bind(r)}}(t,e,s);if(this.status!==h.Stopped)this.observers.add(i);else try{i.complete?.()}catch(t){u(t)}return{unsubscribe:()=>{this.observers.delete(i)}}}start(){if(this.status===h.Running)return this;this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=h.Running;const t=this.logic.getStatus?.(this._state);switch(t?.status){case"done":this.update(this._state);case"error":return this}if(this.logic.start)try{this.logic.start(this._state,this._actorContext)}catch(t){return this._stopProcedure(),this._error(t),this._parent?.send(o(this.id,t)),this}return this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._state,t,this._actorContext)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._stopProcedure(),this._error(t),void this._parent?.send(o(this.id,t))}this.update(e),t.type===r&&(this._stopProcedure(),this._complete())}_stop(){return this.status===h.Stopped?this:(this.mailbox.clear(),this.status===h.NotStarted?(this.status=h.Stopped,this):(this.mailbox.enqueue({type:r}),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){u(t)}this.observers.clear()}_error(t){if(!this.observers.size)return void(this._parent||u(t));let e=!1;for(const s of this.observers){const i=s.error;e||=!i;try{i?.(t)}catch(t){u(t)}}this.observers.clear(),e&&u(t)}_stopProcedure(){if(this.status!==h.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this.status=h.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==h.Stopped){if(this.status!==h.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend({event:t,id:e,delay:s,to:i}){const r=this.clock.setTimeout((()=>{i?i.send(t):this.send(t)}),s);e&&(this.delayedEventsMap[e]=r)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:c)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[d](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function f(t,e){return{config:t,transition:(e,s,i)=>t(e,s,i),getInitialState:(t,s)=>"function"==typeof e?e({input:s}):e,getSnapshot:t=>t,getPersistedState:t=>t,restoreState:t=>t}}const _="$$xstate.resolve",v="$$xstate.reject";const y=f((t=>{}),void 0);t.createEmptyActor=function(){return new l(y,t);var t},t.fromCallback=function(t){return{config:t,start:(t,{self:e})=>{e.send({type:i})},transition:(e,s,{self:a,id:c,system:u})=>{if(s.type===i){const s=t=>{e.canceled||a._parent?.send(t)},i=t=>{e.receivers.add(t)};return e.dispose=t({input:e.input,system:u,self:a,sendBack:s,receive:i}),((d=e.dispose)instanceof Promise||null!==d&&("function"==typeof d||"object"==typeof d)&&"function"==typeof d.then)&&e.dispose.then((t=>{a._parent?.send(n(c,t)),e.canceled=!0}),(t=>{e.canceled=!0,a._parent?.send(o(c,t))})),e}var d;return s.type===r?(e.canceled=!0,"function"==typeof e.dispose&&e.dispose(),e):(e.receivers.forEach((t=>t(s))),e)},getInitialState:(t,e)=>({canceled:!1,receivers:new Set,dispose:void 0,input:e}),getSnapshot:()=>{},getPersistedState:({input:t,canceled:e})=>({input:t,canceled:e})}},t.fromEventObservable=function(t){const e="$$xstate.error",s="$$xstate.complete";return{config:t,transition:(t,i)=>{if("active"!==t.status)return t;switch(i.type){case e:return{...t,status:"error",input:void 0,data:i.data,subscription:void 0};case s:return{...t,status:"done",input:void 0,subscription:void 0};case r:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(i,{self:r,system:n})=>{"done"!==i.status&&(i.subscription=t({input:i.input,system:n,self:r}).subscribe({next:t=>{r._parent?.send(t)},error:t=>{r.send({type:e,data:t})},complete:()=>{r.send({type:s})}}))},getSnapshot:t=>{},getPersistedState:({status:t,data:e,input:s})=>({status:t,data:e,input:s}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",s="$$xstate.error",i="$$xstate.complete";return{config:t,transition:(t,n,{self:o,id:a,defer:c})=>{if("active"!==t.status)return t;switch(n.type){case e:return c((()=>{o._parent?.send({type:`xstate.snapshot.${a}`,data:n.data})})),{...t,data:n.data};case s:return{...t,status:"error",input:void 0,data:n.data,subscription:void 0};case i:return{...t,status:"done",input:void 0,subscription:void 0};case r:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(r,{self:n,system:o})=>{"done"!==r.status&&(r.subscription=t({input:r.input,system:o,self:n}).subscribe({next:t=>{n.send({type:e,data:t})},error:t=>{n.send({type:s,data:t})},complete:()=>{n.send({type:i})}}))},getSnapshot:t=>t.data,getPersistedState:({status:t,data:e,input:s})=>({status:t,data:e,input:s}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromPromise=function(t){return{config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case _:return{...t,status:"done",data:e.data,input:void 0};case v:return{...t,status:"error",data:e.data,input:void 0};case r:return{...t,status:"canceled",input:void 0};default:return t}},start:(e,{self:s,system:i})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:i,self:s})).then((t=>{"active"===s._state.status&&s.send({type:_,data:t})}),(t=>{"active"===s._state.status&&s.send({type:v,data:t})}))},getInitialState:(t,e)=>({status:"active",data:void 0,input:e}),getSnapshot:t=>t.data,getStatus:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=f,t.isActorRef=function(t){return!!t&&"object"==typeof t&&"function"==typeof t.send},t.toActorRef=function(t){return{subscribe:()=>({unsubscribe:()=>{}}),id:"anonymous",sessionId:"",getSnapshot:()=>{},[d]:function(){return this},status:h.Running,stop:()=>{},...t}},Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=xstate-actors.umd.min.js.map
|