-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathRegistry.ts
51 lines (40 loc) · 1.25 KB
/
Registry.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
import {SessionObserver} from "./Session";
import Session from "./Session";
import FSM, {FSMCollectionJson} from "./FSM";
export type FSMRegistry = {[key:string]:FSM};
export default class Registry {
private static registris_ : FSMRegistry[] = [];
private static fsm_: FSMRegistry = null;
static Parse(fsmc: FSMCollectionJson) {
fsmc.forEach(fsmJson => {
if ( Registry.Get(fsmJson.name)===null ) {
const fsm = new FSM(fsmJson);
Registry.fsm_[fsm.name] = fsm;
}
});
}
static Get(s: string) : FSM {
const fsm = Registry.fsm_[s];
if (typeof fsm === 'undefined') {
return null;
}
return fsm;
}
static SessionFor<T>(s: string, state: T, observer?: SessionObserver<T>) {
const fsm = Registry.Get(s);
if (fsm === null) {
throw new Error(`FSM ${s} does not exist.`);
}
const session= new Session(state);
session.initialize(fsm, observer);
return session;
}
static PushRegistry() {
Registry.fsm_ = {};
Registry.registris_.push(this.fsm_);
}
static PopRegistry() {
Registry.fsm_ = this.registris_.pop();
}
}
Registry.PushRegistry();