forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state-machine.d.ts
83 lines (67 loc) · 2.85 KB
/
state-machine.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
// Type definitions for Finite State Machine 2.2
// Project: https://github.com/jakesgordon/javascript-state-machine
// Definitions by: Boris Yankov <https://github.com/borisyankov/>, Maarten Docter <https://github.com/mdocter>, William Sears <https://github.com/MrBigDog2U>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface StateMachineErrorCallback {
(eventName?: string, from?: string, to?: string, args?: any[], errorCode?: number, errorMessage?: string, ex?: Error): void; // NB. errorCode? See: StateMachine.Error
}
interface StateMachineEventDef {
name: string;
from: any; // string or string[]
to: string;
}
interface StateMachineEvent {
(...args: any[]): void;
}
interface StateMachineConfig {
initial?: any; // string or { state: 'foo', event: 'setup', defer: true|false }
events?: StateMachineEventDef[];
callbacks?: {
[s: string]: (event?: string, from?: string, to?: string, ...args: any[]) => any;
};
target?: StateMachine;
error?: StateMachineErrorCallback;
}
interface StateMachineStatic {
VERSION: string; // = "2.2.0"
WILDCARD: string; // = '*'
ASYNC: string; // = 'async'
Result: {
SUCCEEDED: number; // = 1, the event transitioned successfully from one state to another
NOTRANSITION: number; // = 2, the event was successfull but no state transition was necessary
CANCELLED: number; // = 3, the event was cancelled by the caller in a beforeEvent callback
ASYNC: number; // = 4, the event is asynchronous and the caller is in control of when the transition occurs
};
Error: {
INVALID_TRANSITION: number; // = 100, caller tried to fire an event that was innapropriate in the current state
PENDING_TRANSITION: number; // = 200, caller tried to fire an event while an async transition was still pending
INVALID_CALLBACK: number; // = 300, caller provided callback function threw an exception
};
create(config: StateMachineConfig, target?: StateMachine): StateMachine;
}
interface StateMachineTransition {
(): void;
cancel(): void;
}
interface StateMachineIs {
(state: string): boolean;
}
interface StateMachineCan {
(evt: string): boolean;
}
interface StateMachine {
current: string;
is: StateMachineIs;
can: StateMachineCan;
cannot: StateMachineCan;
error: StateMachineErrorCallback;
/* transition - only available when performing async state transitions; otherwise null. Can be a:
[1] fsm.transition(); // called from async callback
[2] fsm.transition.cancel();
*/
transition: StateMachineTransition;
}
declare var StateMachine: StateMachineStatic;
declare module "state-machine" {
export = StateMachineStatic;
}