deno.land / x / xstate@xstate@4.33.6 / src / State.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, StateValueMap, StateConfig, SCXML, StateSchema, TransitionDefinition, Typestate, ActorRef, StateMachine, SimpleEventsOf} from './types';import { EMPTY_ACTIVITY_MAP } from './constants';import { matchesState, isString, warn } from './utils';import { StateNode } from './StateNode';import { getMeta, nextEvents } from './stateUtils';import { initEvent } from './actions';import { IS_PRODUCTION } from './environment';import { TypegenDisabled, TypegenEnabled } from './typegenTypes';import { BaseActionObject, Prop } from './types';
export function stateValuesEqual( a: StateValue | undefined, b: StateValue | undefined): boolean { if (a === b) { return true; }
if (a === undefined || b === undefined) { return false; }
if (isString(a) || isString(b)) { return a === b; }
const aKeys = Object.keys(a as StateValueMap); const bKeys = Object.keys(b as StateValueMap);
return ( aKeys.length === bKeys.length && aKeys.every((key) => stateValuesEqual(a[key], b[key])) );}
export function isStateConfig<TContext, TEvent extends EventObject>( state: any): state is StateConfig<TContext, TEvent> { if (typeof state !== 'object' || state === null) { return false; }
return 'value' in state && '_event' in state;}
/** * @deprecated Use `isStateConfig(object)` or `state instanceof State` instead. */export const isState = isStateConfig;
export function bindActionToState<TC, TE extends EventObject>( action: ActionObject<TC, TE>, state: State<TC, TE, any, any, any>): ActionObject<TC, TE> { const { exec } = action; const boundAction: ActionObject<TC, TE> = { ...action, exec: exec !== undefined ? () => exec(state.context, state.event as TE, { action, state, _event: state._event }) : undefined };
return boundAction;}
export class State< TContext, TEvent extends EventObject = EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = { value: any; context: TContext }, TResolvedTypesMeta = TypegenDisabled> { public value: StateValue; public context: TContext; public historyValue?: HistoryValue | undefined; public history?: State< TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta >; public actions: Array<ActionObject<TContext, TEvent>> = []; public activities: ActivityMap = EMPTY_ACTIVITY_MAP; public meta: any = {}; public events: TEvent[] = []; public event: TEvent; public _event: SCXML.Event<TEvent>; public _sessionid: string | null; /** * Indicates whether the state has changed from the previous state. A state is considered "changed" if: * * - Its value is not equal to its previous value, or: * - It has any new actions (side-effects) to execute. * * An initial state (with no history) will return `undefined`. */ public changed: boolean | undefined; /** * Indicates whether the state is a final state. */ public done: boolean | undefined; /** * The enabled state nodes representative of the state value. */ public configuration: Array<StateNode<TContext, any, TEvent, any, any>>; /** * The next events that will cause a transition from the current state. */ // @ts-ignore - getter for this gets configured in constructor so this property can stay non-enumerable public nextEvents: Array<TEvent['type']>; /** * The transition definitions that resulted in this state. */ public transitions: Array<TransitionDefinition<TContext, TEvent>>; /** * An object mapping actor IDs to spawned actors/invoked services. */ public children: Record<string, ActorRef<any>>; public tags: Set<string>; public machine: | StateMachine< TContext, any, TEvent, TTypestate, BaseActionObject, any, TResolvedTypesMeta > | undefined; /** * Creates a new State instance for the given `stateValue` and `context`. * @param stateValue * @param context */ public static from<TC, TE extends EventObject = EventObject>( stateValue: State<TC, TE, any, any, any> | StateValue, context?: TC | undefined ): State<TC, TE, any, any, any> { if (stateValue instanceof State) { if (stateValue.context !== context) { return new State<TC, TE>({ value: stateValue.value, context: context as TC, _event: stateValue._event, _sessionid: null, historyValue: stateValue.historyValue, history: stateValue.history, actions: [], activities: stateValue.activities, meta: {}, events: [], configuration: [], // TODO: fix, transitions: [], children: {} }); }
return stateValue; }
const _event = initEvent as SCXML.Event<TE>;
return new State<TC, TE>({ value: stateValue, context: context as TC, _event, _sessionid: null, historyValue: undefined, history: undefined, actions: [], activities: undefined, meta: undefined, events: [], configuration: [], transitions: [], children: {} }); } /** * Creates a new State instance for the given `config`. * @param config The state config */ public static create<TC, TE extends EventObject = EventObject>( config: StateConfig<TC, TE> ): State<TC, TE, any, any, any> { return new State(config); } /** * Creates a new `State` instance for the given `stateValue` and `context` with no actions (side-effects). * @param stateValue * @param context */ public static inert<TC, TE extends EventObject = EventObject>( stateValue: State<TC, TE, any, any, any> | StateValue, context: TC ): State<TC, TE> { if (stateValue instanceof State) { if (!stateValue.actions.length) { return stateValue as State<TC, TE>; } const _event = initEvent as SCXML.Event<TE>;
return new State<TC, TE>({ value: stateValue.value, context, _event, _sessionid: null, historyValue: stateValue.historyValue, history: stateValue.history, activities: stateValue.activities, configuration: stateValue.configuration, transitions: [], children: {} }); }
return State.from<TC, TE>(stateValue, context); }
/** * Creates a new State instance. * @param value The state value * @param context The extended state * @param historyValue The tree representing historical values of the state nodes * @param history The previous state * @param actions An array of action objects to execute as side-effects * @param activities A mapping of activities and whether they are started (`true`) or stopped (`false`). * @param meta * @param events Internal event queue. Should be empty with run-to-completion semantics. * @param configuration */ constructor(config: StateConfig<TContext, TEvent>) { this.value = config.value; this.context = config.context; this._event = config._event; this._sessionid = config._sessionid; this.event = this._event.data; this.historyValue = config.historyValue; this.history = config.history as this; this.actions = config.actions || []; this.activities = config.activities || EMPTY_ACTIVITY_MAP; this.meta = getMeta(config.configuration); this.events = config.events || []; this.matches = this.matches.bind(this); this.toStrings = this.toStrings.bind(this); this.configuration = config.configuration; this.transitions = config.transitions; this.children = config.children; this.done = !!config.done; this.tags = (Array.isArray(config.tags) ? new Set(config.tags) : config.tags) ?? new Set(); this.machine = config.machine;
Object.defineProperty(this, 'nextEvents', { get: () => { return nextEvents(this.configuration); } }); }
/** * Returns an array of all the string leaf state node paths. * @param stateValue * @param delimiter The character(s) that separate each subpath in the string state node path. */ public toStrings( stateValue: StateValue = this.value, delimiter: string = '.' ): string[] { if (isString(stateValue)) { return [stateValue]; } const valueKeys = Object.keys(stateValue);
return valueKeys.concat( ...valueKeys.map((key) => this.toStrings(stateValue[key], delimiter).map( (s) => key + delimiter + s ) ) ); }
public toJSON() { const { configuration, transitions, tags, machine, ...jsonValues } = this;
return { ...jsonValues, tags: Array.from(tags) }; }
/** * Whether the current state value is a subset of the given parent state value. * @param parentStateValue */ public matches< TSV extends TResolvedTypesMeta extends TypegenEnabled ? Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'matchesStates'> : never >(parentStateValue: TSV): boolean; public matches< TSV extends TResolvedTypesMeta extends TypegenDisabled ? TTypestate['value'] : never >( parentStateValue: TSV ): this is State< (TTypestate extends any ? { value: TSV; context: any } extends TTypestate ? TTypestate : never : never)['context'], TEvent, TStateSchema, TTypestate, TResolvedTypesMeta > & { value: TSV }; public matches(parentStateValue: StateValue): any { return matchesState(parentStateValue as StateValue, this.value); }
/** * Whether the current state configuration has a state node with the specified `tag`. * @param tag */ public hasTag( tag: TResolvedTypesMeta extends TypegenEnabled ? Prop<Prop<TResolvedTypesMeta, 'resolved'>, 'tags'> : string ): boolean { return this.tags.has(tag as string); }
/** * Determines whether sending the `event` will cause a non-forbidden transition * to be selected, even if the transitions have no actions nor * change the state value. * * @param event The event to test * @returns Whether the event will cause a transition */ public can(event: TEvent | SimpleEventsOf<TEvent>['type']): boolean { if (IS_PRODUCTION) { warn( !!this.machine, `state.can(...) used outside of a machine-created State object; this will always return false.` ); }
const transitionData = this.machine?.getTransitionData(this, event);
return ( !!transitionData?.transitions.length && // Check that at least one transition is not forbidden transitionData.transitions.some( (t) => t.target !== undefined || t.actions.length ) ); }}
xstate

Version Info

Tagged at
2 years ago