xstate 4.30.6 → 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 +33 -0
- package/dist/xstate.interpreter.js +1 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.web.js +2 -2
- package/es/State.d.ts +2 -2
- package/es/StateNode.js +1 -2
- package/es/interpreter.js +11 -3
- package/es/stateUtils.js +2 -4
- package/es/typegenTypes.d.ts +24 -15
- package/es/types.d.ts +7 -7
- package/es/waitFor.d.ts +32 -0
- package/es/waitFor.js +66 -0
- package/lib/State.d.ts +2 -2
- package/lib/StateNode.js +1 -2
- package/lib/interpreter.js +11 -3
- package/lib/stateUtils.js +2 -4
- package/lib/typegenTypes.d.ts +24 -15
- package/lib/types.d.ts +7 -7
- package/lib/waitFor.d.ts +32 -0
- package/lib/waitFor.js +70 -0
- package/package.json +1 -1
package/lib/stateUtils.js
CHANGED
|
@@ -11,6 +11,8 @@ var isLeafNode = function (stateNode) {
|
|
|
11
11
|
function getChildren(stateNode) {
|
|
12
12
|
return Object.keys(stateNode.states).map(function (key) {
|
|
13
13
|
return stateNode.states[key];
|
|
14
|
+
}).filter(function (sn) {
|
|
15
|
+
return sn.type !== 'history';
|
|
14
16
|
});
|
|
15
17
|
}
|
|
16
18
|
function getAllStateNodes(stateNode) {
|
|
@@ -75,10 +77,6 @@ function getConfiguration(prevStateNodes, stateNodes) {
|
|
|
75
77
|
for (var _e = (e_3 = void 0, _tslib.__values(getChildren(s))), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
76
78
|
var child = _f.value;
|
|
77
79
|
|
|
78
|
-
if (child.type === 'history') {
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
80
|
if (!configuration.has(child)) {
|
|
83
81
|
configuration.add(child);
|
|
84
82
|
|
package/lib/typegenTypes.d.ts
CHANGED
|
@@ -77,21 +77,27 @@ export interface TypegenMeta extends TypegenEnabled {
|
|
|
77
77
|
eventsCausingServices: Record<string, string>;
|
|
78
78
|
}
|
|
79
79
|
export interface ResolvedTypegenMeta extends TypegenMeta {
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
resolved: TypegenMeta & {
|
|
81
|
+
indexedActions: Record<string, BaseActionObject>;
|
|
82
|
+
indexedEvents: Record<string, EventObject>;
|
|
83
|
+
};
|
|
82
84
|
}
|
|
83
85
|
export declare type TypegenConstraint = TypegenEnabled | TypegenDisabled;
|
|
84
|
-
export declare type AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta, TMissingImplementations = Prop<TResolvedTypesMeta, 'missingImplementations'>> = IsAny<TResolvedTypesMeta> extends true ? true : TResolvedTypesMeta extends TypegenEnabled ? IsNever<Values<{
|
|
86
|
+
export declare type AreAllImplementationsAssumedToBeProvided<TResolvedTypesMeta, TMissingImplementations = Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'missingImplementations'>> = IsAny<TResolvedTypesMeta> extends true ? true : TResolvedTypesMeta extends TypegenEnabled ? IsNever<Values<{
|
|
85
87
|
[K in keyof TMissingImplementations]: TMissingImplementations[K];
|
|
86
88
|
}>> extends true ? true : false : true;
|
|
87
|
-
|
|
89
|
+
interface AllImplementationsProvided {
|
|
88
90
|
missingImplementations: {
|
|
89
91
|
actions: never;
|
|
90
92
|
delays: never;
|
|
91
93
|
guards: never;
|
|
92
94
|
services: never;
|
|
93
95
|
};
|
|
94
|
-
}
|
|
96
|
+
}
|
|
97
|
+
export interface MarkAllImplementationsAsProvided<TResolvedTypesMeta> {
|
|
98
|
+
'@@xstate/typegen': Prop<TResolvedTypesMeta, '@@xstate/typegen'>;
|
|
99
|
+
resolved: Prop<TResolvedTypesMeta, 'resolved'> & AllImplementationsProvided;
|
|
100
|
+
}
|
|
95
101
|
declare type GenerateServiceEvent<TServiceName, TEventType, TServiceMap extends ServiceMap> = TEventType extends any ? {
|
|
96
102
|
type: TEventType;
|
|
97
103
|
} & Prop<TServiceMap, TServiceName> : never;
|
|
@@ -105,17 +111,20 @@ declare type AllowAllEvents = {
|
|
|
105
111
|
eventsCausingGuards: Record<string, string>;
|
|
106
112
|
eventsCausingServices: Record<string, string>;
|
|
107
113
|
};
|
|
108
|
-
export
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
export interface ResolveTypegenMeta<TTypesMeta extends TypegenConstraint, TEvent extends EventObject, TAction extends BaseActionObject, TServiceMap extends ServiceMap> {
|
|
115
|
+
'@@xstate/typegen': TTypesMeta['@@xstate/typegen'];
|
|
116
|
+
resolved: TTypesMeta extends TypegenEnabled ? TTypesMeta & {
|
|
117
|
+
indexedActions: IndexByType<TAction>;
|
|
118
|
+
indexedEvents: MergeWithInternalEvents<IndexByType<(string extends TEvent['type'] ? never : TEvent) | GenerateServiceEvents<TServiceMap, Prop<TTypesMeta, 'invokeSrcNameMap'>>>, Prop<TTypesMeta, 'internalEvents'>>;
|
|
119
|
+
} : TypegenDisabled & AllImplementationsProvided & AllowAllEvents & {
|
|
120
|
+
indexedActions: IndexByType<TAction>;
|
|
121
|
+
indexedEvents: Record<string, TEvent> & {
|
|
122
|
+
__XSTATE_ALLOW_ANY_INVOKE_DATA_HACK__: {
|
|
123
|
+
data: any;
|
|
124
|
+
};
|
|
116
125
|
};
|
|
126
|
+
invokeSrcNameMap: Record<string, '__XSTATE_ALLOW_ANY_INVOKE_DATA_HACK__'>;
|
|
117
127
|
};
|
|
118
|
-
|
|
119
|
-
};
|
|
128
|
+
}
|
|
120
129
|
export {};
|
|
121
130
|
//# sourceMappingURL=typegenTypes.d.ts.map
|
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,7 +519,7 @@ 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
|
*/
|
|
@@ -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, MarkAllImplementationsAsProvided<TResolvedTypesMeta
|
|
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)> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, infer TStateSchema, infer TEvent, infer TTypestate, any, any, infer TResolvedTypesMeta> ? Interpreter<TContext, TStateSchema, TEvent, TTypestate, MarkAllImplementationsAsProvided<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/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;
|