-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
301 lines (260 loc) · 8.03 KB
/
index.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const EventEmitter = require('events');
const Optional = require('optional-js');
class StateType {
constructor(value) {
this._value = value;
}
value() {
return this._value;
}
update(val) {
if (val !== null && typeof val !== 'object') {
if (this._value.prototype.name = 'RState') {
this._value.update(val);
}
else {
this._value = val;
}
return this;
}
for (let key in val) {
if (!Object.getOwnPropertyDescriptor(this._value, key)) {
throw new Error('Property ' + key + ' does not exist');
}
if (this._value[key].constructor && this._value[key].constructor.name === 'RState') {
this._value[key].update(val[key]);
}
else {
this._value[key] = val[key];
}
}
return this;
}
}
function defState(__enum__) {
const enums = Array.prototype.slice.call(arguments);
function typeName(val) {
return typeof val === 'object' && '' + val === '[object Object]' && val.constructor && val.constructor.name ? val.constructor.name : val;
}
function getType(val) {
const typeIdx = enums.findIndex(e => {
return e === val || (e.prototype instanceof StateType && val instanceof e);
});
if (typeIdx == -1) {
return Optional.empty();
}
else {
return Optional.of(enums[typeIdx]);
}
}
class RState {
constructor(val_or_func) {
this.set(val_or_func);
}
when(val, f) {
if (arguments.length != 2 && arguments.length != 1) {
throw new Error('Invalid arguments: ' + Array.prototype.join.call(arguments, ', '));
}
if (!this.is(val)) {
return this;
}
const stateValue = this._state instanceof StateType ? this._state.value() : this._state;
const value = f ? f(stateValue) : stateValue;
return Object.assign({}, this, {
when: function () {
return this;
},
collect: function () {
return value;
}
});
}
collect(defaultValue) {
return defaultValue;
}
is(val) {
if (this._state instanceof StateType) {
return this._state instanceof val;
}
return this._state === val;
}
get() {
return enums.find(e => {
return e === this._state || (e.prototype instanceof StateType && this._state instanceof e);
});
}
allowed(val) {
return getType(val)
.filter(type => {
if (typeof this._state === 'undefined' && this._transitions && this._transitions.initial) {
if (this._transitions.initial.indexOf(type) == -1) {
return false;
}
}
if (typeof this._state !== 'undefined' && this._transitions && this._transitions.map) {
let currentType = getType(this._state).get();
if (typeof currentType !== 'function') {
currentType = currentType.toString();
}
else {
currentType = currentType.name;
}
if (!this._transitions.map[currentType] || this._transitions.map[currentType].indexOf(type) == -1) {
return false;
}
}
return true;
})
.isPresent();
}
set(val) {
return getType(val)
.ifPresentOrElse(type => {
if (typeof this._state === 'undefined' && this._transitions && this._transitions.initial) {
if (this._transitions.initial.indexOf(type) == -1) {
if (!this._default || val === this._default) {
throw new Error('Illegal transition: ' + typeName(this._state) + ' -> ' + typeName(val));
}
return this.set(this._default);
}
}
if (typeof this._state !== 'undefined' && this._transitions && this._transitions.map) {
let currentType = getType(this._state).get();
if (typeof currentType !== 'function') {
currentType = currentType.toString();
}
else {
currentType = currentType.name;
}
if (!this._transitions.map[currentType] || this._transitions.map[currentType].indexOf(type) == -1) {
if (!this._default || val === this._default) {
throw new Error('Illegal transition: ' + typeName(this._state) + ' -> ' + typeName(val));
}
return this.set(this._default);
}
}
this._state = val;
return this;
},
() => {
if (!this._default) {
throw new Error('Illegal argument: ' + typeName(val));
}
return this.set(this._default);
});
}
update(val) {
if (val !== null && typeof val !== 'object') {
return this.set(val);
}
if (this._state instanceof StateType) {
this._state.update(val);
return this;
}
for (let key in val) {
if (!Object.getOwnPropertyDescriptor(this._state, key)) {
throw new Error('Property ' + key + ' does not exist');
}
else {
this._state[key] = val[key];
}
}
return this;
}
static transitions(transition_map, initial_array) {
this.prototype._transitions = { initial: initial_array, map: transition_map };
return this;
}
static withDefault(defaultValue) {
this.prototype._default = defaultValue;
return this;
}
static create(initialValue) {
return new this(initialValue);
}
}
return RState;
}
function intersectState(statesMap) {
class StateIntersection extends EventEmitter {
constructor(statesMap) {
super();
this._states = Object.assign({}, statesMap);
this._first = Object.keys(statesMap)[0];
}
get () {
return this._states[this._first].get();
}
allowed (value) {
for (let key in this._states) {
if (!this._states[key].allowed(value)) {
return false;
}
}
return true;
}
set (value) {
const maxAttempts = Object.keys(this._states).length + 1;
let val = value;
let change = false;
for (let i = 0; i < maxAttempts; ++i) {
for (let key in this._states) {
this._states[key].set(val);
if (!this._states[key].is(val)) {
val = this._states[key].when(this._states[key].get()).collect();
change = true;
break;
}
}
if (!change) {
this.emit('change', val);
return this;
}
change = false;
}
if (typeof this._default === 'undefined') {
throw new Error('Mutually acceptable state does not exist for ' + JSON.stringify(value, 2, null));
}
for (let key in this._states) {
this._states[key].set(this._default);
if (!this._states[key].is(this._default)) {
throw new Error('Default invalid for ' + key);
}
}
this.emit('change', this._default);
return this;
}
reset (rstate, opt_value) {
// assert Object.keys(value).length == 1
const key = Object.keys(rstate)[0];
const currentValue = arguments.length > 1 ? opt_value : this._states[key].when(this._states[key].get()).collect();
this._states[key] = rstate[key];
return this.set(currentValue);
}
subscribe (callback) {
this.addListener('change', callback);
return () => this.removeListener('change', callback);
}
static withDefault(defaultValue) {
this.prototype._default = defaultValue;
return this;
}
static create(newValue) {
if (arguments.length == 0) {
const [ first, ...rest ] = Object.keys(statesMap);
rest.forEach(r => {
if (!statesMap[r].is(statesMap[first].get())) {
throw new Error('inconsistent initial state');
}
});
}
let instance = new this(statesMap);
if (arguments.length != 0) {
instance.set(newValue);
}
return instance;
}
};
return StateIntersection;
}
module.exports = { StateType, defState, intersectState };