deno.land / x / xstate@xstate@4.33.6 / src / Actor.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
import { EventObject, Subscribable, InvokeDefinition, AnyEventObject, StateMachine, Spawnable, SCXML, ActorRef, BaseActorRef} from './types';import { symbolObservable, isMachine, mapContext, toInvokeSource} from './utils';import * as serviceScope from './serviceScope';
export interface Actor< TContext = any, TEvent extends EventObject = AnyEventObject> extends Subscribable<TContext> { id: string; send: (event: TEvent) => any; // TODO: change to void stop?: () => any | undefined; toJSON: () => { id: string; }; meta?: InvokeDefinition<TContext, TEvent>; state?: any; deferred?: boolean;}
export function createNullActor(id: string): ActorRef<any> { return { id, send: () => void 0, subscribe: () => ({ unsubscribe: () => void 0 }), getSnapshot: () => undefined, toJSON: () => ({ id }), [symbolObservable]: function () { return this; } };}
/** * Creates a deferred actor that is able to be invoked given the provided * invocation information in its `.meta` value. * * @param invokeDefinition The meta information needed to invoke the actor. */export function createInvocableActor<TC, TE extends EventObject>( invokeDefinition: InvokeDefinition<TC, TE>, machine: StateMachine<TC, any, TE, any>, context: TC, _event: SCXML.Event<TE>): ActorRef<any> { const invokeSrc = toInvokeSource(invokeDefinition.src); const serviceCreator = machine?.options.services?.[invokeSrc.type]; const resolvedData = invokeDefinition.data ? mapContext(invokeDefinition.data, context, _event) : undefined; const tempActor = serviceCreator ? createDeferredActor( serviceCreator as Spawnable, invokeDefinition.id, resolvedData ) : createNullActor(invokeDefinition.id);
// @ts-ignore tempActor.meta = invokeDefinition;
return tempActor;}
export function createDeferredActor( entity: Spawnable, id: string, data?: any): ActorRef<any, undefined> { const tempActor = createNullActor(id);
// @ts-ignore tempActor.deferred = true;
if (isMachine(entity)) { // "mute" the existing service scope so potential spawned actors within the `.initialState` stay deferred here const initialState = ((tempActor as any).state = serviceScope.provide( undefined, () => (data ? entity.withContext(data) : entity).initialState )); tempActor.getSnapshot = () => initialState; }
return tempActor;}
export function isActor(item: any): item is ActorRef<any> { try { return typeof item.send === 'function'; } catch (e) { return false; }}
export function isSpawnedActor(item: any): item is ActorRef<any> { return isActor(item) && 'id' in item;}
// TODO: refactor the return type, this could be written in a better way but it's best to avoid unneccessary breaking changes nowexport function toActorRef< TEvent extends EventObject, TEmitted = any, TActorRefLike extends BaseActorRef<TEvent> = BaseActorRef<TEvent>>( actorRefLike: TActorRefLike): ActorRef<TEvent, TEmitted> & Omit<TActorRefLike, keyof ActorRef<any, any>> { return { subscribe: () => ({ unsubscribe: () => void 0 }), id: 'anonymous', getSnapshot: () => undefined, [symbolObservable]: function () { return this; }, ...actorRefLike };}
xstate

Version Info

Tagged at
2 years ago