xstate 5.16.0 → 5.17.0
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 +2 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -2
- 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 +1 -1
- package/actors/dist/xstate-actors.development.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.esm.js +1 -1
- package/actors/dist/xstate-actors.esm.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/{raise-a6298350.cjs.js → State-30c95050.cjs.js} +199 -191
- package/dist/{raise-7d030497.development.esm.js → State-34039d2a.development.esm.js} +215 -207
- package/dist/{raise-bad6a97b.development.cjs.js → State-a2464a1e.development.cjs.js} +214 -206
- package/dist/{raise-2cfe6b8f.esm.js → State-cdbc7940.esm.js} +200 -192
- package/dist/declarations/src/State.d.ts +8 -8
- package/dist/declarations/src/StateMachine.d.ts +10 -12
- package/dist/declarations/src/actions/stopChild.d.ts +1 -0
- package/dist/declarations/src/actors/callback.d.ts +3 -2
- package/dist/declarations/src/createActor.d.ts +6 -6
- package/dist/declarations/src/index.d.ts +11 -12
- package/dist/declarations/src/inspection.d.ts +3 -2
- package/dist/declarations/src/setup.d.ts +1 -1
- package/dist/declarations/src/stateUtils.d.ts +1 -13
- package/dist/declarations/src/system.d.ts +3 -2
- package/dist/declarations/src/types.d.ts +7 -1
- package/dist/declarations/src/utils.d.ts +1 -5
- package/dist/{log-f9587b82.cjs.js → log-19086852.cjs.js} +1 -1
- package/dist/{log-8320f5e6.esm.js → log-4a38a98a.esm.js} +1 -1
- package/dist/{log-17f4495d.development.esm.js → log-62f17756.development.esm.js} +1 -1
- package/dist/{log-31321d85.development.cjs.js → log-ec36113c.development.cjs.js} +1 -1
- package/dist/xstate.cjs.js +92 -92
- package/dist/xstate.development.cjs.js +95 -95
- package/dist/xstate.development.esm.js +91 -91
- package/dist/xstate.esm.js +88 -88
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- 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.map +1 -1
- package/package.json +1 -3
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './State-34039d2a.development.esm.js';
|
|
2
|
+
export { C as Actor, I as __unsafe_getAllOwnEventDescriptors, E as and, M as cancel, A as createActor, j as getStateNodes, D as interpret, B as isMachineSnapshot, J as matchesState, F as not, G as or, K as pathToStateValue, O as raise, P as spawnChild, H as stateIn, Q as stop, R as stopChild, L as toObserver } from './State-34039d2a.development.esm.js';
|
|
3
|
+
import { a as assign } from './log-62f17756.development.esm.js';
|
|
4
|
+
export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-62f17756.development.esm.js';
|
|
1
5
|
export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.development.esm.js';
|
|
2
|
-
import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-7d030497.development.esm.js';
|
|
3
|
-
export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, E as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, D as isMachineSnapshot, J as matchesState, F as not, G as or, K as pathToStateValue, O as raise, P as spawnChild, H as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-7d030497.development.esm.js';
|
|
4
|
-
import { a as assign } from './log-17f4495d.development.esm.js';
|
|
5
|
-
export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-17f4495d.development.esm.js';
|
|
6
6
|
import '../dev/dist/xstate-dev.development.esm.js';
|
|
7
7
|
|
|
8
8
|
class SimulatedClock {
|
|
@@ -599,78 +599,35 @@ class StateMachine {
|
|
|
599
599
|
}
|
|
600
600
|
}
|
|
601
601
|
|
|
602
|
-
const defaultWaitForOptions = {
|
|
603
|
-
timeout: Infinity // much more than 10 seconds
|
|
604
|
-
};
|
|
605
|
-
|
|
606
602
|
/**
|
|
607
|
-
*
|
|
608
|
-
*
|
|
609
|
-
* is not reached after an optional timeout. (defaults to Infinity).
|
|
603
|
+
* Asserts that the given event object is of the specified type or types. Throws
|
|
604
|
+
* an error if the event object is not of the specified types.
|
|
610
605
|
*
|
|
611
606
|
* @example
|
|
612
607
|
*
|
|
613
|
-
* ```
|
|
614
|
-
*
|
|
615
|
-
*
|
|
616
|
-
*
|
|
608
|
+
* ```ts
|
|
609
|
+
* // ...
|
|
610
|
+
* entry: ({ event }) => {
|
|
611
|
+
* assertEvent(event, 'doNothing');
|
|
612
|
+
* // event is { type: 'doNothing' }
|
|
613
|
+
* },
|
|
614
|
+
* // ...
|
|
615
|
+
* exit: ({ event }) => {
|
|
616
|
+
* assertEvent(event, 'greet');
|
|
617
|
+
* // event is { type: 'greet'; message: string }
|
|
617
618
|
*
|
|
618
|
-
*
|
|
619
|
+
* assertEvent(event, ['greet', 'notify']);
|
|
620
|
+
* // event is { type: 'greet'; message: string }
|
|
621
|
+
* // or { type: 'notify'; message: string; level: 'info' | 'error' }
|
|
622
|
+
* },
|
|
619
623
|
* ```
|
|
620
|
-
*
|
|
621
|
-
* @param actorRef The actor ref to subscribe to
|
|
622
|
-
* @param predicate Determines if a value matches the condition to wait for
|
|
623
|
-
* @param options
|
|
624
|
-
* @returns A promise that eventually resolves to the emitted value that matches
|
|
625
|
-
* the condition
|
|
626
624
|
*/
|
|
627
|
-
function
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
let done = false;
|
|
634
|
-
if (resolvedOptions.timeout < 0) {
|
|
635
|
-
console.error('`timeout` passed to `waitFor` is negative and it will reject its internal promise immediately.');
|
|
636
|
-
}
|
|
637
|
-
const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
|
|
638
|
-
sub.unsubscribe();
|
|
639
|
-
rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
|
|
640
|
-
}, resolvedOptions.timeout);
|
|
641
|
-
const dispose = () => {
|
|
642
|
-
clearTimeout(handle);
|
|
643
|
-
done = true;
|
|
644
|
-
sub?.unsubscribe();
|
|
645
|
-
};
|
|
646
|
-
function checkEmitted(emitted) {
|
|
647
|
-
if (predicate(emitted)) {
|
|
648
|
-
dispose();
|
|
649
|
-
res(emitted);
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
let sub; // avoid TDZ when disposing synchronously
|
|
653
|
-
|
|
654
|
-
// See if the current snapshot already matches the predicate
|
|
655
|
-
checkEmitted(actorRef.getSnapshot());
|
|
656
|
-
if (done) {
|
|
657
|
-
return;
|
|
658
|
-
}
|
|
659
|
-
sub = actorRef.subscribe({
|
|
660
|
-
next: checkEmitted,
|
|
661
|
-
error: err => {
|
|
662
|
-
dispose();
|
|
663
|
-
rej(err);
|
|
664
|
-
},
|
|
665
|
-
complete: () => {
|
|
666
|
-
dispose();
|
|
667
|
-
rej(new Error(`Actor terminated without satisfying predicate`));
|
|
668
|
-
}
|
|
669
|
-
});
|
|
670
|
-
if (done) {
|
|
671
|
-
sub.unsubscribe();
|
|
672
|
-
}
|
|
673
|
-
});
|
|
625
|
+
function assertEvent(event, type) {
|
|
626
|
+
const types = toArray(type);
|
|
627
|
+
if (!types.includes(event.type)) {
|
|
628
|
+
const typesText = types.length === 1 ? `type "${types[0]}"` : `one of types "${types.join('", "')}"`;
|
|
629
|
+
throw new Error(`Expected event ${JSON.stringify(event)} to have ${typesText}`);
|
|
630
|
+
}
|
|
674
631
|
}
|
|
675
632
|
|
|
676
633
|
// this is not 100% accurate since we can't make parallel regions required in the result
|
|
@@ -836,35 +793,78 @@ function toPromise(actor) {
|
|
|
836
793
|
});
|
|
837
794
|
}
|
|
838
795
|
|
|
796
|
+
const defaultWaitForOptions = {
|
|
797
|
+
timeout: Infinity // much more than 10 seconds
|
|
798
|
+
};
|
|
799
|
+
|
|
839
800
|
/**
|
|
840
|
-
*
|
|
841
|
-
*
|
|
801
|
+
* Subscribes to an actor ref and waits for its emitted value to satisfy a
|
|
802
|
+
* predicate, and then resolves with that value. Will throw if the desired state
|
|
803
|
+
* is not reached after an optional timeout. (defaults to Infinity).
|
|
842
804
|
*
|
|
843
805
|
* @example
|
|
844
806
|
*
|
|
845
|
-
* ```
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
* // event is { type: 'doNothing' }
|
|
850
|
-
* },
|
|
851
|
-
* // ...
|
|
852
|
-
* exit: ({ event }) => {
|
|
853
|
-
* assertEvent(event, 'greet');
|
|
854
|
-
* // event is { type: 'greet'; message: string }
|
|
807
|
+
* ```js
|
|
808
|
+
* const state = await waitFor(someService, (state) => {
|
|
809
|
+
* return state.hasTag('loaded');
|
|
810
|
+
* });
|
|
855
811
|
*
|
|
856
|
-
*
|
|
857
|
-
* // event is { type: 'greet'; message: string }
|
|
858
|
-
* // or { type: 'notify'; message: string; level: 'info' | 'error' }
|
|
859
|
-
* },
|
|
812
|
+
* state.hasTag('loaded'); // true
|
|
860
813
|
* ```
|
|
814
|
+
*
|
|
815
|
+
* @param actorRef The actor ref to subscribe to
|
|
816
|
+
* @param predicate Determines if a value matches the condition to wait for
|
|
817
|
+
* @param options
|
|
818
|
+
* @returns A promise that eventually resolves to the emitted value that matches
|
|
819
|
+
* the condition
|
|
861
820
|
*/
|
|
862
|
-
function
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
821
|
+
function waitFor(actorRef, predicate, options) {
|
|
822
|
+
const resolvedOptions = {
|
|
823
|
+
...defaultWaitForOptions,
|
|
824
|
+
...options
|
|
825
|
+
};
|
|
826
|
+
return new Promise((res, rej) => {
|
|
827
|
+
let done = false;
|
|
828
|
+
if (resolvedOptions.timeout < 0) {
|
|
829
|
+
console.error('`timeout` passed to `waitFor` is negative and it will reject its internal promise immediately.');
|
|
830
|
+
}
|
|
831
|
+
const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
|
|
832
|
+
sub.unsubscribe();
|
|
833
|
+
rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
|
|
834
|
+
}, resolvedOptions.timeout);
|
|
835
|
+
const dispose = () => {
|
|
836
|
+
clearTimeout(handle);
|
|
837
|
+
done = true;
|
|
838
|
+
sub?.unsubscribe();
|
|
839
|
+
};
|
|
840
|
+
function checkEmitted(emitted) {
|
|
841
|
+
if (predicate(emitted)) {
|
|
842
|
+
dispose();
|
|
843
|
+
res(emitted);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
let sub; // avoid TDZ when disposing synchronously
|
|
847
|
+
|
|
848
|
+
// See if the current snapshot already matches the predicate
|
|
849
|
+
checkEmitted(actorRef.getSnapshot());
|
|
850
|
+
if (done) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
sub = actorRef.subscribe({
|
|
854
|
+
next: checkEmitted,
|
|
855
|
+
error: err => {
|
|
856
|
+
dispose();
|
|
857
|
+
rej(err);
|
|
858
|
+
},
|
|
859
|
+
complete: () => {
|
|
860
|
+
dispose();
|
|
861
|
+
rej(new Error(`Actor terminated without satisfying predicate`));
|
|
862
|
+
}
|
|
863
|
+
});
|
|
864
|
+
if (done) {
|
|
865
|
+
sub.unsubscribe();
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
868
|
}
|
|
869
869
|
|
|
870
870
|
export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, setup, toPromise, waitFor };
|
package/dist/xstate.esm.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './State-cdbc7940.esm.js';
|
|
2
|
+
export { C as Actor, I as __unsafe_getAllOwnEventDescriptors, E as and, M as cancel, A as createActor, j as getStateNodes, D as interpret, B as isMachineSnapshot, J as matchesState, F as not, G as or, K as pathToStateValue, O as raise, P as spawnChild, H as stateIn, Q as stop, R as stopChild, L as toObserver } from './State-cdbc7940.esm.js';
|
|
3
|
+
import { a as assign } from './log-4a38a98a.esm.js';
|
|
4
|
+
export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-4a38a98a.esm.js';
|
|
1
5
|
export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.esm.js';
|
|
2
|
-
import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-2cfe6b8f.esm.js';
|
|
3
|
-
export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, E as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, D as isMachineSnapshot, J as matchesState, F as not, G as or, K as pathToStateValue, O as raise, P as spawnChild, H as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-2cfe6b8f.esm.js';
|
|
4
|
-
import { a as assign } from './log-8320f5e6.esm.js';
|
|
5
|
-
export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-8320f5e6.esm.js';
|
|
6
6
|
import '../dev/dist/xstate-dev.esm.js';
|
|
7
7
|
|
|
8
8
|
class SimulatedClock {
|
|
@@ -596,75 +596,35 @@ class StateMachine {
|
|
|
596
596
|
}
|
|
597
597
|
}
|
|
598
598
|
|
|
599
|
-
const defaultWaitForOptions = {
|
|
600
|
-
timeout: Infinity // much more than 10 seconds
|
|
601
|
-
};
|
|
602
|
-
|
|
603
599
|
/**
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
* is not reached after an optional timeout. (defaults to Infinity).
|
|
600
|
+
* Asserts that the given event object is of the specified type or types. Throws
|
|
601
|
+
* an error if the event object is not of the specified types.
|
|
607
602
|
*
|
|
608
603
|
* @example
|
|
609
604
|
*
|
|
610
|
-
* ```
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
605
|
+
* ```ts
|
|
606
|
+
* // ...
|
|
607
|
+
* entry: ({ event }) => {
|
|
608
|
+
* assertEvent(event, 'doNothing');
|
|
609
|
+
* // event is { type: 'doNothing' }
|
|
610
|
+
* },
|
|
611
|
+
* // ...
|
|
612
|
+
* exit: ({ event }) => {
|
|
613
|
+
* assertEvent(event, 'greet');
|
|
614
|
+
* // event is { type: 'greet'; message: string }
|
|
614
615
|
*
|
|
615
|
-
*
|
|
616
|
+
* assertEvent(event, ['greet', 'notify']);
|
|
617
|
+
* // event is { type: 'greet'; message: string }
|
|
618
|
+
* // or { type: 'notify'; message: string; level: 'info' | 'error' }
|
|
619
|
+
* },
|
|
616
620
|
* ```
|
|
617
|
-
*
|
|
618
|
-
* @param actorRef The actor ref to subscribe to
|
|
619
|
-
* @param predicate Determines if a value matches the condition to wait for
|
|
620
|
-
* @param options
|
|
621
|
-
* @returns A promise that eventually resolves to the emitted value that matches
|
|
622
|
-
* the condition
|
|
623
621
|
*/
|
|
624
|
-
function
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
let done = false;
|
|
631
|
-
const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
|
|
632
|
-
sub.unsubscribe();
|
|
633
|
-
rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
|
|
634
|
-
}, resolvedOptions.timeout);
|
|
635
|
-
const dispose = () => {
|
|
636
|
-
clearTimeout(handle);
|
|
637
|
-
done = true;
|
|
638
|
-
sub?.unsubscribe();
|
|
639
|
-
};
|
|
640
|
-
function checkEmitted(emitted) {
|
|
641
|
-
if (predicate(emitted)) {
|
|
642
|
-
dispose();
|
|
643
|
-
res(emitted);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
let sub; // avoid TDZ when disposing synchronously
|
|
647
|
-
|
|
648
|
-
// See if the current snapshot already matches the predicate
|
|
649
|
-
checkEmitted(actorRef.getSnapshot());
|
|
650
|
-
if (done) {
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
sub = actorRef.subscribe({
|
|
654
|
-
next: checkEmitted,
|
|
655
|
-
error: err => {
|
|
656
|
-
dispose();
|
|
657
|
-
rej(err);
|
|
658
|
-
},
|
|
659
|
-
complete: () => {
|
|
660
|
-
dispose();
|
|
661
|
-
rej(new Error(`Actor terminated without satisfying predicate`));
|
|
662
|
-
}
|
|
663
|
-
});
|
|
664
|
-
if (done) {
|
|
665
|
-
sub.unsubscribe();
|
|
666
|
-
}
|
|
667
|
-
});
|
|
622
|
+
function assertEvent(event, type) {
|
|
623
|
+
const types = toArray(type);
|
|
624
|
+
if (!types.includes(event.type)) {
|
|
625
|
+
const typesText = types.length === 1 ? `type "${types[0]}"` : `one of types "${types.join('", "')}"`;
|
|
626
|
+
throw new Error(`Expected event ${JSON.stringify(event)} to have ${typesText}`);
|
|
627
|
+
}
|
|
668
628
|
}
|
|
669
629
|
|
|
670
630
|
// this is not 100% accurate since we can't make parallel regions required in the result
|
|
@@ -830,35 +790,75 @@ function toPromise(actor) {
|
|
|
830
790
|
});
|
|
831
791
|
}
|
|
832
792
|
|
|
793
|
+
const defaultWaitForOptions = {
|
|
794
|
+
timeout: Infinity // much more than 10 seconds
|
|
795
|
+
};
|
|
796
|
+
|
|
833
797
|
/**
|
|
834
|
-
*
|
|
835
|
-
*
|
|
798
|
+
* Subscribes to an actor ref and waits for its emitted value to satisfy a
|
|
799
|
+
* predicate, and then resolves with that value. Will throw if the desired state
|
|
800
|
+
* is not reached after an optional timeout. (defaults to Infinity).
|
|
836
801
|
*
|
|
837
802
|
* @example
|
|
838
803
|
*
|
|
839
|
-
* ```
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
843
|
-
* // event is { type: 'doNothing' }
|
|
844
|
-
* },
|
|
845
|
-
* // ...
|
|
846
|
-
* exit: ({ event }) => {
|
|
847
|
-
* assertEvent(event, 'greet');
|
|
848
|
-
* // event is { type: 'greet'; message: string }
|
|
804
|
+
* ```js
|
|
805
|
+
* const state = await waitFor(someService, (state) => {
|
|
806
|
+
* return state.hasTag('loaded');
|
|
807
|
+
* });
|
|
849
808
|
*
|
|
850
|
-
*
|
|
851
|
-
* // event is { type: 'greet'; message: string }
|
|
852
|
-
* // or { type: 'notify'; message: string; level: 'info' | 'error' }
|
|
853
|
-
* },
|
|
809
|
+
* state.hasTag('loaded'); // true
|
|
854
810
|
* ```
|
|
811
|
+
*
|
|
812
|
+
* @param actorRef The actor ref to subscribe to
|
|
813
|
+
* @param predicate Determines if a value matches the condition to wait for
|
|
814
|
+
* @param options
|
|
815
|
+
* @returns A promise that eventually resolves to the emitted value that matches
|
|
816
|
+
* the condition
|
|
855
817
|
*/
|
|
856
|
-
function
|
|
857
|
-
const
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
818
|
+
function waitFor(actorRef, predicate, options) {
|
|
819
|
+
const resolvedOptions = {
|
|
820
|
+
...defaultWaitForOptions,
|
|
821
|
+
...options
|
|
822
|
+
};
|
|
823
|
+
return new Promise((res, rej) => {
|
|
824
|
+
let done = false;
|
|
825
|
+
const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
|
|
826
|
+
sub.unsubscribe();
|
|
827
|
+
rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
|
|
828
|
+
}, resolvedOptions.timeout);
|
|
829
|
+
const dispose = () => {
|
|
830
|
+
clearTimeout(handle);
|
|
831
|
+
done = true;
|
|
832
|
+
sub?.unsubscribe();
|
|
833
|
+
};
|
|
834
|
+
function checkEmitted(emitted) {
|
|
835
|
+
if (predicate(emitted)) {
|
|
836
|
+
dispose();
|
|
837
|
+
res(emitted);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
let sub; // avoid TDZ when disposing synchronously
|
|
841
|
+
|
|
842
|
+
// See if the current snapshot already matches the predicate
|
|
843
|
+
checkEmitted(actorRef.getSnapshot());
|
|
844
|
+
if (done) {
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
sub = actorRef.subscribe({
|
|
848
|
+
next: checkEmitted,
|
|
849
|
+
error: err => {
|
|
850
|
+
dispose();
|
|
851
|
+
rej(err);
|
|
852
|
+
},
|
|
853
|
+
complete: () => {
|
|
854
|
+
dispose();
|
|
855
|
+
rej(new Error(`Actor terminated without satisfying predicate`));
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
if (done) {
|
|
859
|
+
sub.unsubscribe();
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
862
|
}
|
|
863
863
|
|
|
864
864
|
export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getInitialSnapshot, getNextSnapshot, setup, toPromise, waitFor };
|