-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (114 loc) · 3.9 KB
/
index.js
File metadata and controls
159 lines (114 loc) · 3.9 KB
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
var createStateHandler,
StateStore;
/* this function replaces the "official" api call `Alexa.CreateStateHandler(state.name, state.handlers);` because the library function mutates the handlers object and at this point I am not sure, whether this might potentially cause issues. */
createStateHandler = function (state, obj = {}) {
var target;
target = Object.assign({}, obj);
Object.defineProperty(target, 'STATE', {
"value": state || ''
});
return target;
};
StateStore = function (separator = '->') {
var addHandler,
create,
createState,
createSubState,
getHandlers,
getState,
getStates,
states = {};
create = function (...args) {
var result;
if (args.length === 3) {
result = createSubState(...args);
} else if (args.length === 2) {
if (typeof args[1] === 'string') {
result = createSubState(args[0], args[1], {});
} else {
result = createState(...args);
}
} else if (args.length === 1) {
result = createState(args[0], {});
}
return result;
};
createState = function (name, handlers) {
return createSubState(name, null, handlers);
};
createSubState = function (name, superstate, handlers) {
var ssHandlers,
ssName,
ssObject,
state = {};
state.superstate = superstate;
if (superstate === null) {
state.name = name;
state.handlers = Object.assign({}, handlers);
} else {
if (typeof superstate === 'string') {
ssObject = getState(superstate);
if (typeof ssObject === 'undefined') {
throw new Error('Superstate ' + superstate + ' does not exist.');
}
ssHandlers = ssObject.handlers;
ssName = ssObject.name;
} else if (typeof superstate === 'object') {
ssHandlers = superstate.handlers;
ssName = superstate.name;
}
state.name = ssName + separator + name;
state.handlers = Object.assign({}, ssHandlers, handlers);
}
states[state.name] = state;
state.handlers = createStateHandler(state.name, state.handlers);
return state;
};
getState = function (name) {
var matches = Object.keys(states).filter((state) => state.match(new RegExp(name + '$')));
if (matches.length > 1) {
throw new Error('Multiple states match the search string '
+ name
+ '. Unable to uniquely identify the correct state.');
} else if (matches.length === 0) {
throw new Error('No state matching the search string ' + name + '.');
} else {
return states[matches[0]];
}
};
getStates = function () {
return states;
};
addHandler = function (state, name, handler) {
var localState;
if (typeof state === 'string') {
localState = states[state];
localState.handlers[name] = handler;
} else if (typeof state === 'object') {
localState = states[state.name];
if (typeof state.handlers === 'undefined') {
localState.handlers = {};
}
localState.handlers[name] = handler;
}
};
addHandlers = function (state, handlers) {
for (var i in handlers) {
addHandler(state, i, handlers[i]);
}
};
getHandlers = function () {
return Object.keys(states).map((key) => states[key].handlers);
};
return {
create,
createState,
createSubState,
getState,
getStates,
getHandlers,
addHandler,
addHandlers
};
};
module.exports = StateStore;