-
Notifications
You must be signed in to change notification settings - Fork 0
/
rt.ts
314 lines (285 loc) · 7.52 KB
/
rt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Descriptor of a persistent instance.
*/
export type PersistentInstanceDescriptor = {
/**
* The name of the machine.
*/
machineName: string;
/**
* The name of the machine instance.
* A UUID will be generated if not provided.
*/
machineInstanceName?: string;
/**
* The version of the machine to use.
* If not provided, the current machine version will be used.
*/
machineVersionId?: string;
};
/**
* Reference to a persistent actor.
*/
export type PersistentActorRef = {
type: "statebacked.instance";
/**
* Local ID of the actor.
* This is not globally unique but may be used in calls to `sendTo` to refer to the actor.
*/
id: string;
machineName: string;
machineInstanceName: string;
};
/**
* Options for spawning a persistent instance.
*/
export type SpawnOptions = {
/**
* Initial context to use for the instance.
*/
context?: Record<string, unknown>;
/**
* Local name of the instance.
* If specified, may be used in calls to `sendTo` to refer to the actor.
*/
name?: string;
};
/**
* Spawn a persistent instance of a machine.
*
* THIS **MUST** BE CALLED WITHIN AN `assign` ACTION TO HAVE THE INTENDED EFFECT.
*
* @param instanceDescriptor descriptor of the instance to spawn
* @param opts spawn options
* @returns A PersistentActorRef
*/
export const spawnPersistentInstance = (
instanceDescriptor: PersistentInstanceDescriptor,
opts?: SpawnOptions
): PersistentActorRef => {
if (typeof (globalThis as any)?.__statebacked_rt?.spawn === "function") {
return (globalThis as any).__statebacked_rt.spawn(instanceDescriptor, opts);
}
return {
...instanceDescriptor,
type: "statebacked.instance",
id: opts?.name ?? crypto.randomUUID(),
machineInstanceName:
instanceDescriptor.machineInstanceName ?? crypto.randomUUID(),
};
};
/**
* Event object.
*/
export type XStateEventObject = { type: string };
/**
* Any event object.
*/
export interface AnyXStateEventObject extends XStateEventObject {
[key: string]: any;
}
/**
* String or object event.
*/
export type XStateEvent<TEvent extends XStateEventObject> = TEvent["type"] | TEvent;
/**
* Event sender.
*/
export type XStateSender<TEvent extends XStateEventObject> = (event: XStateEvent<TEvent>) => void;
/**
* Event receiver.
*/
export type XStateReceiver<TEvent extends XStateEventObject> = (
listener: {
bivarianceHack(event: TEvent): void;
}["bivarianceHack"]
) => void;
/**
* Invoke callback spawnable.
*/
export type XStateInvokeCallback<
TEvent extends XStateEventObject = AnyXStateEventObject,
TSentEvent extends XStateEventObject = AnyXStateEventObject
> = (
callback: XStateSender<TSentEvent>,
onReceive: XStateReceiver<TEvent>
) => (() => void) | Promise<any> | void;
/**
* Subscription.
*/
export interface XStateSubscription {
unsubscribe(): void;
}
/**
* Observer spawnable.
*/
export interface XStateObserver<T> {
next: (value: T) => void;
error: (err: any) => void;
complete: () => void;
}
/**
* Subscribable for an InteropObservable.
*/
export interface XStateInteropSubscribable<T> {
subscribe(observer: XStateObserver<T>): XStateSubscription;
}
declare global {
interface SymbolConstructor {
readonly observable: symbol;
}
}
/**
* Interop observable spawnable.
*/
export interface XStateInteropObservable<T> {
[Symbol.observable]: () => XStateInteropSubscribable<T>;
}
/**
* Subscribable spawnable.
*/
export interface XStateSubscribable<T> extends XStateInteropSubscribable<T> {
subscribe(observer: XStateObserver<T>): XStateSubscription;
subscribe(
next: (value: T) => void,
error?: (error: any) => void,
complete?: () => void
): XStateSubscription;
}
/**
* Actor reference.
*/
export interface XStateActorRef<TEvent extends XStateEventObject, TEmitted = any>
extends XStateSubscribable<TEmitted>,
XStateInteropObservable<TEmitted> {
send: XStateSender<TEvent>; // TODO: this should just be TEvent
id: string;
getSnapshot: () => TEmitted | undefined;
stop?: () => void;
toJSON?: () => any;
}
/**
* XState actor context.
*/
export interface XStateActorContext<TEvent extends XStateEventObject, TEmitted> {
parent?: XStateActorRef<any, any>;
self: XStateActorRef<TEvent, TEmitted>;
id: string;
observers: Set<XStateObserver<TEmitted>>;
}
/**
* Behavior spawnable.
*/
export interface XStateBehavior<TEvent extends XStateEventObject, TEmitted = any> {
transition: (
state: TEmitted,
event: TEvent,
actorCtx: XStateActorContext<TEvent, TEmitted>
) => TEmitted;
initialState: TEmitted;
start?: (actorCtx: XStateActorContext<TEvent, TEmitted>) => TEmitted;
}
/**
* Any state machine.
*/
export interface AnyXStateStateMachine {
__xstatenode: true;
}
/**
* Spawnable for spawnEphemeralInstance.
*/
export type XStateSpawnable =
| AnyXStateStateMachine
| PromiseLike<any>
| XStateInvokeCallback
| XStateInteropObservable<any>
| XStateSubscribable<any>
| XStateBehavior<any>;
/**
* Options for spawnEphemeralInstance.
*/
export type EphemeralSpawnOptions = {
name?: string;
autoForward?: boolean;
sync?: boolean;
};
/**
* Spawn an ephemeral child instance.
*
* Same as `spawn` from xstate but ensures compatibility with State Backed packaging and runtime.
*
* @param entity
* @param nameOrOptions
* @returns
*/
export const spawnEphemeralInstance = (
entity: XStateSpawnable,
nameOrOptions?: string | EphemeralSpawnOptions
): XStateActorRef<any> => {
return (globalThis as any).__statebacked_rt.spawn(entity, nameOrOptions);
};
/**
* Format a persistent instance descriptor to use as an xstate invoke source.
*
* Use like this:
* ```
* createMachine({
* states: {
* idle: {
* invoke: {
* src: invocableSource({
* machineName: "myMachine",
* machineInstanceName: "myMachineInstance",
* machineVersionId: "ver_myMachineVersion",
* }),
* }
* }
* })
* ```
*
* @param instanceDescriptor PersistentInstanceDescriptor
* @returns An object to be provided as the `src` property of an xstate invoke.
*/
export const persistentInvocableSource = (
instanceDescriptor: PersistentInstanceDescriptor
) => ({
type: "statebacked.invoke",
machineName: instanceDescriptor.machineName,
machineInstanceName:
instanceDescriptor.machineInstanceName ?? crypto.randomUUID(),
machineVersionId: instanceDescriptor.machineVersionId,
});
/**
* Action creator to send an event to a persistent actor.
*
* @param actor The name of the actor to send the event to, or a PersistentActorRef, or a function that takes the current context and event and returns a string or PersistentActorRef
* @param event The event to send or a function that takes the current context and event and returns an event
* @param opts Send options including delay and event ID (which can be used to cancel the event)
* @returns Action object to be used in an xstate definition
*/
export const sendTo = <
TContext = Record<string, unknown>,
TEvent = { type: string } & Record<string, unknown>
>(
actor:
| string
| PersistentActorRef
| ((ctx: TContext, event: TEvent) => string | PersistentActorRef),
event: any,
opts?: {
delay:
| string
| number
| ((ctx: TContext, event: TEvent) => string | number);
id: string | number;
}
) => {
// we return our own action object so we don't force clients to deal with type issues of passing a PersistentActorRef to sendTo
return {
type: "xstate.send",
to: actor,
event: typeof event === "function" ? event : () => event,
delay: opts?.delay,
id: opts?.id,
};
};