deno.land / x / xstate@xstate@4.33.6 / src / model.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
import { assign } from './actions';import { createMachine } from './Machine';import type { Cast, EventObject, BaseActionObject, Prop, IsNever} from './types';import { mapValues } from './utils';import { UnionFromCreatorsReturnTypes, FinalModelCreators, Model, ModelCreators} from './model.types';
export function createModel< TContext, TEvent extends EventObject, TAction extends BaseActionObject = BaseActionObject>(initialContext: TContext): Model<TContext, TEvent, TAction, void>;export function createModel< TContext, TModelCreators extends ModelCreators<TModelCreators>, TFinalModelCreators = FinalModelCreators<TModelCreators>, TComputedEvent = UnionFromCreatorsReturnTypes< Prop<TFinalModelCreators, 'events'> >, TComputedAction = UnionFromCreatorsReturnTypes< Prop<TFinalModelCreators, 'actions'> >>( initialContext: TContext, creators: TModelCreators): Model< TContext, Cast<TComputedEvent, EventObject>, IsNever<TComputedAction> extends true ? BaseActionObject : Cast<TComputedAction, BaseActionObject>, TFinalModelCreators>;export function createModel( initialContext: object, creators?: ModelCreators<any>): unknown { const eventCreators = creators?.events; const actionCreators = creators?.actions;
const model: Model<any, any, any, any> = { initialContext, assign, events: (eventCreators ? mapValues(eventCreators, (fn, eventType) => (...args: any[]) => ({ ...fn(...args), type: eventType })) : undefined) as any, actions: actionCreators ? mapValues(actionCreators, (fn, actionType) => (...args: any[]) => ({ ...fn(...args), type: actionType })) : undefined, reset: () => assign(initialContext), createMachine: (config, implementations) => { return createMachine( 'context' in config ? config : { ...config, context: initialContext }, implementations ); } };
return model;}
xstate

Version Info

Tagged at
2 years ago