xstate 4.30.4 → 4.31.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/CHANGELOG.md +69 -0
- package/dist/xstate.interpreter.js +1 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.web.js +2 -2
- package/es/SimulatedClock.js +81 -0
- package/es/State.d.ts +2 -2
- package/es/State.js +15 -10
- package/es/StateNode.js +11 -11
- package/es/actions.js +1 -1
- package/es/behaviors.js +71 -5
- package/es/devTools.js +5 -2
- package/es/each.js +13 -0
- package/es/index.js +5 -5
- package/es/interpreter.d.ts +1 -3
- package/es/interpreter.js +36 -29
- package/es/invokeUtils.js +4 -4
- package/es/json.js +86 -0
- package/es/model.js +50 -0
- package/es/patterns.js +48 -0
- package/es/stateUtils.js +2 -4
- package/es/typegenTypes.d.ts +24 -15
- package/es/types.d.ts +9 -9
- package/es/utils.js +52 -2
- package/es/waitFor.d.ts +32 -0
- package/es/waitFor.js +66 -0
- package/lib/SimulatedClock.js +83 -81
- package/lib/State.d.ts +2 -2
- package/lib/State.js +14 -8
- package/lib/StateNode.js +8 -8
- package/lib/actions.js +1 -1
- package/lib/behaviors.js +72 -4
- package/lib/devTools.js +5 -2
- package/lib/each.js +14 -12
- package/lib/index.js +21 -27
- package/lib/interpreter.d.ts +1 -3
- package/lib/interpreter.js +36 -28
- package/lib/invokeUtils.js +4 -4
- package/lib/json.js +81 -72
- package/lib/model.js +49 -69
- package/lib/patterns.js +45 -53
- package/lib/stateUtils.js +2 -4
- package/lib/typegenTypes.d.ts +24 -15
- package/lib/types.d.ts +9 -9
- package/lib/types.js +4 -0
- package/lib/utils.js +53 -0
- package/lib/waitFor.d.ts +32 -0
- package/lib/waitFor.js +70 -0
- package/package.json +3 -2
package/lib/types.d.ts
CHANGED
|
@@ -489,16 +489,16 @@ export declare type ActionFunctionMap<TContext, TEvent extends EventObject, TAct
|
|
|
489
489
|
export declare type DelayFunctionMap<TContext, TEvent extends EventObject> = Record<string, DelayConfig<TContext, TEvent>>;
|
|
490
490
|
export declare type ServiceConfig<TContext, TEvent extends EventObject = AnyEventObject> = string | AnyStateMachine | InvokeCreator<TContext, TEvent>;
|
|
491
491
|
export declare type DelayConfig<TContext, TEvent extends EventObject> = number | DelayExpr<TContext, TEvent>;
|
|
492
|
-
declare type MachineOptionsActions<TContext, TResolvedTypesMeta, TEventsCausingActions = Prop<TResolvedTypesMeta, 'eventsCausingActions'>, TIndexedEvents = Prop<TResolvedTypesMeta, 'indexedEvents'>, TIndexedActions = Prop<TResolvedTypesMeta, 'indexedActions'>> = {
|
|
492
|
+
declare type MachineOptionsActions<TContext, TResolvedTypesMeta, TEventsCausingActions = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'eventsCausingActions'>, TIndexedEvents = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'indexedEvents'>, TIndexedActions = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'indexedActions'>> = {
|
|
493
493
|
[K in keyof TEventsCausingActions]?: ActionObject<TContext, Cast<Prop<TIndexedEvents, TEventsCausingActions[K]>, EventObject>> | ActionFunction<TContext, Cast<Prop<TIndexedEvents, TEventsCausingActions[K]>, EventObject>, Cast<Prop<TIndexedActions, K>, BaseActionObject>>;
|
|
494
494
|
};
|
|
495
|
-
declare type MachineOptionsDelays<TContext, TResolvedTypesMeta, TEventsCausingDelays = Prop<TResolvedTypesMeta, 'eventsCausingDelays'>, TIndexedEvents = Prop<TResolvedTypesMeta, 'indexedEvents'>> = {
|
|
495
|
+
declare type MachineOptionsDelays<TContext, TResolvedTypesMeta, TEventsCausingDelays = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'eventsCausingDelays'>, TIndexedEvents = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'indexedEvents'>> = {
|
|
496
496
|
[K in keyof TEventsCausingDelays]?: DelayConfig<TContext, Cast<Prop<TIndexedEvents, TEventsCausingDelays[K]>, EventObject>>;
|
|
497
497
|
};
|
|
498
|
-
declare type MachineOptionsGuards<TContext, TResolvedTypesMeta, TEventsCausingGuards = Prop<TResolvedTypesMeta, 'eventsCausingGuards'>, TIndexedEvents = Prop<TResolvedTypesMeta, 'indexedEvents'>> = {
|
|
498
|
+
declare type MachineOptionsGuards<TContext, TResolvedTypesMeta, TEventsCausingGuards = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'eventsCausingGuards'>, TIndexedEvents = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'indexedEvents'>> = {
|
|
499
499
|
[K in keyof TEventsCausingGuards]?: ConditionPredicate<TContext, Cast<Prop<TIndexedEvents, TEventsCausingGuards[K]>, EventObject>>;
|
|
500
500
|
};
|
|
501
|
-
declare type MachineOptionsServices<TContext, TResolvedTypesMeta, TEventsCausingServices = Prop<TResolvedTypesMeta, 'eventsCausingServices'>, TIndexedEvents = Prop<TResolvedTypesMeta, 'indexedEvents'>, TInvokeSrcNameMap = Prop<TResolvedTypesMeta, 'invokeSrcNameMap'>> = {
|
|
501
|
+
declare type MachineOptionsServices<TContext, TResolvedTypesMeta, TEventsCausingServices = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'eventsCausingServices'>, TIndexedEvents = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'indexedEvents'>, TInvokeSrcNameMap = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'invokeSrcNameMap'>> = {
|
|
502
502
|
[K in keyof TEventsCausingServices]?: AnyStateMachine | InvokeCreator<TContext, Cast<Prop<TIndexedEvents, TEventsCausingServices[K]>, EventObject>, Prop<Prop<TIndexedEvents, Prop<TInvokeSrcNameMap, K>>, 'data'>, EventObject, Cast<TIndexedEvents[keyof TIndexedEvents], EventObject>>;
|
|
503
503
|
};
|
|
504
504
|
declare type MakeKeysRequired<T extends string> = {
|
|
@@ -519,14 +519,14 @@ declare type GenerateGuardsConfigPart<TContext, TResolvedTypesMeta, TRequireMiss
|
|
|
519
519
|
declare type GenerateServicesConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> = MaybeMakeMissingImplementationsRequired<'services', Prop<TMissingImplementations, 'services'>, TRequireMissingImplementations> & {
|
|
520
520
|
services?: MachineOptionsServices<TContext, TResolvedTypesMeta>;
|
|
521
521
|
};
|
|
522
|
-
export declare type InternalMachineOptions<TContext, TEvent extends EventObject, TResolvedTypesMeta, TRequireMissingImplementations extends boolean = false, TMissingImplementations = Prop<TResolvedTypesMeta, 'missingImplementations'>> = GenerateActionsConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & GenerateDelaysConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & GenerateGuardsConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & GenerateServicesConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & {
|
|
522
|
+
export declare type InternalMachineOptions<TContext, TEvent extends EventObject, TResolvedTypesMeta, TRequireMissingImplementations extends boolean = false, TMissingImplementations = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'missingImplementations'>> = GenerateActionsConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & GenerateDelaysConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & GenerateGuardsConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & GenerateServicesConfigPart<TContext, TResolvedTypesMeta, TRequireMissingImplementations, TMissingImplementations> & {
|
|
523
523
|
/**
|
|
524
524
|
* @deprecated Use `services` instead.
|
|
525
525
|
*/
|
|
526
526
|
activities?: Record<string, ActivityConfig<TContext, TEvent>>;
|
|
527
527
|
};
|
|
528
528
|
export declare type MachineOptions<TContext, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta extends TypegenConstraint = TypegenDisabled> = InternalMachineOptions<TContext, TEvent, ResolveTypegenMeta<TTypesMeta, TEvent, TAction, TServiceMap>>;
|
|
529
|
-
export interface MachineConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta = TypegenDisabled> extends StateNodeConfig<NoInfer<TContext>, TStateSchema, TEvent
|
|
529
|
+
export interface MachineConfig<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TTypesMeta = TypegenDisabled> extends StateNodeConfig<NoInfer<TContext>, TStateSchema, NoInfer<TEvent>, TAction> {
|
|
530
530
|
/**
|
|
531
531
|
* The initial context (extended state)
|
|
532
532
|
*/
|
|
@@ -572,7 +572,7 @@ export interface HistoryStateNode<TContext> extends StateNode<TContext> {
|
|
|
572
572
|
export interface StateMachine<TContext, TStateSchema extends StateSchema, TEvent extends EventObject, TTypestate extends Typestate<TContext> = {
|
|
573
573
|
value: any;
|
|
574
574
|
context: TContext;
|
|
575
|
-
}, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, TEvent
|
|
575
|
+
}, TAction extends BaseActionObject = BaseActionObject, TServiceMap extends ServiceMap = ServiceMap, TResolvedTypesMeta = ResolveTypegenMeta<TypegenDisabled, NoInfer<TEvent>, TAction, TServiceMap>> extends StateNode<TContext, TStateSchema, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta> {
|
|
576
576
|
id: string;
|
|
577
577
|
states: StateNode<TContext, TStateSchema, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta>['states'];
|
|
578
578
|
withConfig(options: InternalMachineOptions<TContext, TEvent, TResolvedTypesMeta, true>, context?: TContext | (() => TContext)): StateMachine<TContext, TStateSchema, TEvent, TTypestate, TAction, TServiceMap, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta>;
|
|
@@ -1011,9 +1011,9 @@ export declare type ActorRefWithDeprecatedState<TContext, TEvent extends EventOb
|
|
|
1011
1011
|
*/
|
|
1012
1012
|
state: State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>;
|
|
1013
1013
|
};
|
|
1014
|
-
export declare type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? ActorRefWithDeprecatedState<TContext, TEvent, TTypestate, TResolvedTypesMeta> : R extends Promise<infer U> ? ActorRef<never, U> : R extends Behavior<infer TEvent, infer TEmitted> ? ActorRef<TEvent, TEmitted> : never : never;
|
|
1014
|
+
export declare type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, any, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? ActorRefWithDeprecatedState<TContext, TEvent, TTypestate, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta> : R extends Promise<infer U> ? ActorRef<never, U> : R extends Behavior<infer TEvent, infer TEmitted> ? ActorRef<TEvent, TEmitted> : never : never;
|
|
1015
1015
|
export declare type AnyInterpreter = Interpreter<any, any, any, any, any>;
|
|
1016
|
-
export declare type InterpreterFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = T extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate, TResolvedTypesMeta>
|
|
1016
|
+
export declare type InterpreterFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate, AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta> extends false ? MarkAllImplementationsAsProvided<TResolvedTypesMeta> : TResolvedTypesMeta> : never;
|
|
1017
1017
|
export declare type MachineOptionsFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine), TRequireMissingImplementations extends boolean = false> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, any, infer TEvent, any, any, any, infer TResolvedTypesMeta> ? InternalMachineOptions<TContext, TEvent, TResolvedTypesMeta, TRequireMissingImplementations> : never;
|
|
1018
1018
|
export declare type __ResolvedTypesMetaFrom<T> = T extends StateMachine<any, any, any, any, any, any, infer TResolvedTypesMeta> ? TResolvedTypesMeta : never;
|
|
1019
1019
|
export interface ActorContext<TEvent extends EventObject, TEmitted> {
|
package/lib/types.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
exports.ActionTypes = void 0;
|
|
6
|
+
|
|
5
7
|
(function (ActionTypes) {
|
|
6
8
|
ActionTypes["Start"] = "xstate.start";
|
|
7
9
|
ActionTypes["Stop"] = "xstate.stop";
|
|
@@ -25,6 +27,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
25
27
|
ActionTypes["Choose"] = "xstate.choose";
|
|
26
28
|
})(exports.ActionTypes || (exports.ActionTypes = {}));
|
|
27
29
|
|
|
30
|
+
exports.SpecialTargets = void 0;
|
|
31
|
+
|
|
28
32
|
(function (SpecialTargets) {
|
|
29
33
|
SpecialTargets["Parent"] = "#_parent";
|
|
30
34
|
SpecialTargets["Internal"] = "#_internal";
|
package/lib/utils.js
CHANGED
|
@@ -7,6 +7,9 @@ var constants = require('./constants.js');
|
|
|
7
7
|
var environment = require('./environment.js');
|
|
8
8
|
|
|
9
9
|
var _a;
|
|
10
|
+
function keys(value) {
|
|
11
|
+
return Object.keys(value);
|
|
12
|
+
}
|
|
10
13
|
function matchesState(parentStateId, childStateId, delimiter) {
|
|
11
14
|
if (delimiter === void 0) {
|
|
12
15
|
delimiter = constants.STATE_DELIMITER;
|
|
@@ -43,6 +46,13 @@ function getEventType(event) {
|
|
|
43
46
|
throw new Error('Events must be strings or objects with a string event.type property.');
|
|
44
47
|
}
|
|
45
48
|
}
|
|
49
|
+
function getActionType(action) {
|
|
50
|
+
try {
|
|
51
|
+
return isString(action) || typeof action === 'number' ? "".concat(action) : isFunction(action) ? action.name : action.type;
|
|
52
|
+
} catch (e) {
|
|
53
|
+
throw new Error('Actions must be strings or objects with a string action.type property.');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
46
56
|
function toStatePath(stateId, delimiter) {
|
|
47
57
|
try {
|
|
48
58
|
if (isArray(stateId)) {
|
|
@@ -217,6 +227,46 @@ function toStatePaths(stateValue) {
|
|
|
217
227
|
}));
|
|
218
228
|
return result;
|
|
219
229
|
}
|
|
230
|
+
function pathsToStateValue(paths) {
|
|
231
|
+
var e_4, _a;
|
|
232
|
+
|
|
233
|
+
var result = {};
|
|
234
|
+
|
|
235
|
+
if (paths && paths.length === 1 && paths[0].length === 1) {
|
|
236
|
+
return paths[0][0];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
for (var paths_1 = _tslib.__values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
|
|
241
|
+
var currentPath = paths_1_1.value;
|
|
242
|
+
var marker = result; // tslint:disable-next-line:prefer-for-of
|
|
243
|
+
|
|
244
|
+
for (var i = 0; i < currentPath.length; i++) {
|
|
245
|
+
var subPath = currentPath[i];
|
|
246
|
+
|
|
247
|
+
if (i === currentPath.length - 2) {
|
|
248
|
+
marker[subPath] = currentPath[i + 1];
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
marker[subPath] = marker[subPath] || {};
|
|
253
|
+
marker = marker[subPath];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
} catch (e_4_1) {
|
|
257
|
+
e_4 = {
|
|
258
|
+
error: e_4_1
|
|
259
|
+
};
|
|
260
|
+
} finally {
|
|
261
|
+
try {
|
|
262
|
+
if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return)) _a.call(paths_1);
|
|
263
|
+
} finally {
|
|
264
|
+
if (e_4) throw e_4.error;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
220
270
|
function flatten(array) {
|
|
221
271
|
var _a;
|
|
222
272
|
|
|
@@ -580,6 +630,7 @@ function createInvokeId(stateNodeId, index) {
|
|
|
580
630
|
exports.createInvokeId = createInvokeId;
|
|
581
631
|
exports.evaluateGuard = evaluateGuard;
|
|
582
632
|
exports.flatten = flatten;
|
|
633
|
+
exports.getActionType = getActionType;
|
|
583
634
|
exports.getEventType = getEventType;
|
|
584
635
|
exports.interopSymbols = interopSymbols;
|
|
585
636
|
exports.isActor = isActor;
|
|
@@ -592,6 +643,7 @@ exports.isObservable = isObservable;
|
|
|
592
643
|
exports.isPromiseLike = isPromiseLike;
|
|
593
644
|
exports.isStateLike = isStateLike;
|
|
594
645
|
exports.isString = isString;
|
|
646
|
+
exports.keys = keys;
|
|
595
647
|
exports.mapContext = mapContext;
|
|
596
648
|
exports.mapFilterValues = mapFilterValues;
|
|
597
649
|
exports.mapValues = mapValues;
|
|
@@ -601,6 +653,7 @@ exports.normalizeTarget = normalizeTarget;
|
|
|
601
653
|
exports.partition = partition;
|
|
602
654
|
exports.path = path;
|
|
603
655
|
exports.pathToStateValue = pathToStateValue;
|
|
656
|
+
exports.pathsToStateValue = pathsToStateValue;
|
|
604
657
|
exports.reportUnhandledExceptionOnInvocation = reportUnhandledExceptionOnInvocation;
|
|
605
658
|
exports.symbolObservable = symbolObservable;
|
|
606
659
|
exports.toArray = toArray;
|
package/lib/waitFor.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ActorRef, EmittedFrom } from '.';
|
|
2
|
+
interface WaitForOptions {
|
|
3
|
+
/**
|
|
4
|
+
* How long to wait before rejecting, if no emitted
|
|
5
|
+
* state satisfies the predicate.
|
|
6
|
+
*
|
|
7
|
+
* @default 10_000 (10 seconds)
|
|
8
|
+
*/
|
|
9
|
+
timeout: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Subscribes to an actor ref and waits for its emitted value to satisfy
|
|
13
|
+
* a predicate, and then resolves with that value.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```js
|
|
17
|
+
* const state = await waitFor(someService, state => {
|
|
18
|
+
* return state.hasTag('loaded');
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* state.hasTag('loaded'); // true
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @param actorRef The actor ref to subscribe to
|
|
25
|
+
* @param predicate Determines if a value matches the condition to wait for
|
|
26
|
+
* @param options
|
|
27
|
+
* @returns A promise that eventually resolves to the emitted value
|
|
28
|
+
* that matches the condition
|
|
29
|
+
*/
|
|
30
|
+
export declare function waitFor<TActorRef extends ActorRef<any, any>>(actorRef: TActorRef, predicate: (emitted: EmittedFrom<TActorRef>) => boolean, options?: Partial<WaitForOptions>): Promise<EmittedFrom<TActorRef>>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=waitFor.d.ts.map
|
package/lib/waitFor.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _tslib = require('./_virtual/_tslib.js');
|
|
6
|
+
|
|
7
|
+
var defaultWaitForOptions = {
|
|
8
|
+
timeout: 10000 // 10 seconds
|
|
9
|
+
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Subscribes to an actor ref and waits for its emitted value to satisfy
|
|
13
|
+
* a predicate, and then resolves with that value.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```js
|
|
17
|
+
* const state = await waitFor(someService, state => {
|
|
18
|
+
* return state.hasTag('loaded');
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* state.hasTag('loaded'); // true
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @param actorRef The actor ref to subscribe to
|
|
25
|
+
* @param predicate Determines if a value matches the condition to wait for
|
|
26
|
+
* @param options
|
|
27
|
+
* @returns A promise that eventually resolves to the emitted value
|
|
28
|
+
* that matches the condition
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
function waitFor(actorRef, predicate, options) {
|
|
32
|
+
var resolvedOptions = _tslib.__assign(_tslib.__assign({}, defaultWaitForOptions), options);
|
|
33
|
+
|
|
34
|
+
return new Promise(function (res, rej) {
|
|
35
|
+
var done = false;
|
|
36
|
+
var handle = setTimeout(function () {
|
|
37
|
+
sub.unsubscribe();
|
|
38
|
+
rej(new Error("Timeout of ".concat(resolvedOptions.timeout, " ms exceeded")));
|
|
39
|
+
}, resolvedOptions.timeout);
|
|
40
|
+
|
|
41
|
+
var dispose = function () {
|
|
42
|
+
clearTimeout(handle);
|
|
43
|
+
done = true;
|
|
44
|
+
sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
var sub = actorRef.subscribe({
|
|
48
|
+
next: function (emitted) {
|
|
49
|
+
if (predicate(emitted)) {
|
|
50
|
+
dispose();
|
|
51
|
+
res(emitted);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
error: function (err) {
|
|
55
|
+
dispose();
|
|
56
|
+
rej(err);
|
|
57
|
+
},
|
|
58
|
+
complete: function () {
|
|
59
|
+
dispose();
|
|
60
|
+
rej(new Error("Actor terminated without satisfying predicate"));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (done) {
|
|
65
|
+
sub.unsubscribe();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
exports.waitFor = waitFor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xstate",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.31.0",
|
|
4
4
|
"description": "Finite State Machines and Statecharts for the Modern Web.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -48,12 +48,13 @@
|
|
|
48
48
|
"@rollup/plugin-babel": "^5.3.0",
|
|
49
49
|
"@scion-scxml/test-framework": "^2.0.15",
|
|
50
50
|
"babel-plugin-annotate-pure-calls": "^0.4.0",
|
|
51
|
+
"fast-glob": "^3.2.11",
|
|
51
52
|
"jest": "^26.6.3",
|
|
52
53
|
"jsdom": "^14.0.0",
|
|
53
54
|
"jsdom-global": "^3.0.2",
|
|
54
55
|
"lerna-alias": "3.0.3-0",
|
|
55
56
|
"pkg-up": "^3.1.0",
|
|
56
|
-
"rollup": "^2.
|
|
57
|
+
"rollup": "^2.69.0",
|
|
57
58
|
"rollup-plugin-filesize": "^6.2.1",
|
|
58
59
|
"rollup-plugin-replace": "^2.2.0",
|
|
59
60
|
"rollup-plugin-terser": "^5.1.2",
|