This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
forked from matthewp/robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
195 lines (168 loc) · 7.21 KB
/
index.d.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
declare module 'robot3' {
/**
* The debugging object contains an _onEnter method, wich can be set to invoke
* this function on every transition.
*/
export const d: {
_onEnter?: OnEnterFunction<Machine>
}
/**
* The `createMachine` function creates a state machine. It takes an object of *states* with the key being the state name.
* The value is usually *state* but might also be *invoke*.
*
* @param initial - Creates a machine that has *initial* as it's initial state.
* @param states - An object of states, where each key is a state name, and the values are one of *state* or *invoke*.
* @param context - A function that returns an object of extended state values. The function can receive an `event` argument.
*/
export function createMachine<S = {}, C = {}>(
initial: keyof S,
states: { [K in keyof S]: MachineState },
context?: ContextFunction<C>
): Machine<typeof states, C, keyof typeof states>
/**
* The `createMachine` function creates a state machine. It takes an object of *states* with the key being the state name.
* The value is usually *state* but might also be *invoke*.
*
* @param states - An object of states, where each key is a state name, and the values are one of *state* or *invoke*.
* @param context - A function that returns an object of extended state values. The function can receive an `event` argument.
*/
export function createMachine<S = {}, C = {}>(
states: { [K in keyof S]: MachineState },
context?: ContextFunction<C>
): Machine<typeof states, C, keyof typeof states>
/**
* The `state` function returns a state object. A state can take transitions and immediates as arguments.
*
* @param args - Any argument needs to be of type Transition or Immediate.
*/
export function state(...args: (Transition | Immediate)[]): MachineState
/**
* A `transition` function is used to move from one state to another.
*
* @param event - This will give the name of the event that triggers this transition.
* @param state - The name of the destination state.
* @param args - Any extra argument will be evaluated to check if they are one of Reducer, Guard or Action.
*/
export function transition<C, E>(
event: string,
state: string,
...args: (Reducer<C, E> | Guard<C, E> | Action<C, E>)[]
): Transition
/**
* An `immediate` function is a type of transition that occurs immediately; it doesn't wait for an event to proceed.
* This is a state that immediately proceeds to the next.
*
* @param state - The name of the destination state.
* @param args - Any extra argument will be evaluated to check if they are a Reducer or a Guard.
*/
export function immediate<C, E>(
state: string,
...args: (Reducer<C, E> | Guard<C, E>)[]
): Transition
/**
* A `guard` is a method that determines if a transition can proceed.
* Returning true allows the transition to occur, returning false prevents it from doing so and leaves the state in its current place.
*
* @param guardFunction A Function that can receive *context* and *event* and will return true or false.
*/
export function guard<C, E>(guardFunction?: GuardFunction<C, E>): Guard<C, E>
/**
* A `reduce` takes a reducer function for changing the context of the machine. A common use case is to set values coming from form fields.
*
* @param reduceFunction A Function that can receive *context* and *event* and will return the context.
*/
export function reduce<C, E>(reduceFunction?: ReduceFunction<C, E>): Reducer<C, E>
/**
* An `action` function takes a function that will be run during a transition. The primary purpose of using action is to perform side-effects.
*
* @param actionFunction A Function that can receive *context* and *event*. Returned values are discarded.
*/
export function action<C, E>(actionFunction?: ActionFunction<C, E>): Action<C, E>
/**
* The `interpret` function takes a machine and creates a service that can send events into the machine, changing its states.
* A service does not mutate a machine, but rather creates derived machines with the current state set.
*
* @param machine The state `machine`, created with *createMachine* to create a new service for.
* @param onChange A callback that is called when the machine completes a transition. Even if the transition results in returning to the same state, the `onChange` callback is still called.
* @param event The `event` can be any object. It is passed to the context function
*/
export function interpret<M extends Machine, E>(
machine: M,
onChange?: InterpretOnChangeFunction<typeof machine>,
initialContext?: M['context'],
event?: { [K in keyof E]: any }
): Service<typeof machine>
/**
* The `invoke` is a special type of state that immediately invokes a Promise-returning function or another machine.
*
* @param fn - Promise-returning function
* @param args - Any argument needs to be of type Transition or Immediate.
*/
export function invoke<C, T>(fn: (ctx: C) => Promise<T>, ...args: (Transition | Immediate)[]): MachineState
/**
* The `invoke` is a special type of state that immediately invokes a Promise-returning function or another machine.
*
* @param machine - Machine
* @param args - Any argument needs to be of type Transition or Immediate.
*/
export function invoke<M extends Machine>(machine: M, ...args: (Transition | Immediate)[]): MachineState
/* General Types */
export type ContextFunction<T> = (initialContext: T) => T
export type GuardFunction<C, E> = (context: C, event: E) => boolean
export type ActionFunction<C, E> = (context: C, event: E) => unknown
export type ReduceFunction<C, E> = (context: C, event: E) => C
export type InterpretOnChangeFunction<T extends Machine> = (
service: Service<T>
) => void
export type SendEvent = string | { type: string; [key: string]: any }
export type SendFunction<T = SendEvent> = (event: T) => void
/**
* This function is invoked before entering a new state and is bound to the debug
* object. It is usable to inspect or log changes.
*
* @param machine - Machine
* @param to - name of the target state
* @param state - current state
* @param prevState - previous state
* @param event - event provoking the state change
*/
export type OnEnterFunction<M extends Machine> =
<C = M['state']>(machine: M, to: string, state: C, prevState: C, event?: SendEvent) => void
export type Machine<S = {}, C = {}, K = string> = {
context: C
current: K
states: S
state: {
name: K
value: MachineState
}
}
export type Action<C, E> = {
fn: ActionFunction<C, E>
}
export type Reducer<C, E> = {
fn: ReduceFunction<C, E>
}
export type Guard<C, E> = {
fn: GuardFunction<C, E>
}
export interface MachineState {
final: boolean
transitions: Map<string, Transition[]>
immediates?: Map<string, Immediate[]>
enter?: any
}
export interface Transition {
from: string | null
to: string
guards: any[]
reducers: any[]
}
export interface Service<M extends Machine> {
machine: M
context: M['context']
onChange: InterpretOnChangeFunction<M>
send: SendFunction
}
export type Immediate = Transition
}