-
Notifications
You must be signed in to change notification settings - Fork 15
/
SessionStateHelper.ts
43 lines (32 loc) · 1.35 KB
/
SessionStateHelper.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
import Session from "./Session";
import Transition, {Message} from "./Transition";
export interface StateInvocationParams<T> {
session : Session<T>;
transition : Transition;
msg : Message; // transition call
guarded? : boolean; // check guard callback
}
export default class SessionStateHelper {
static Invoke<T>(method: string, st: StateInvocationParams<T>) {
const target = st.session.state;
if (typeof target !== 'undefined' &&
typeof method !== 'undefined' &&
typeof target[method] !== 'undefined' &&
typeof(target[method]) === 'function') {
return target[method](st);
}
return true;
}
static EnterState<T>(session: Session<T>, st: string, cst: StateInvocationParams<T>) {
SessionStateHelper.Invoke(st + "_enter", cst);
}
static ExitState<T>(session: Session<T>, st: string, cst: StateInvocationParams<T>) {
SessionStateHelper.Invoke(st + "_exit", cst);
}
static Transition<T>(session: Session<T>, event: string, cst: StateInvocationParams<T>) {
SessionStateHelper.Invoke(event + "_transition", cst);
}
static CheckGuard<T>(session: Session<T>, event: string, cst: StateInvocationParams<T>): boolean {
return SessionStateHelper.Invoke(event + "_guard", cst);
}
}