-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatemachine.js
More file actions
318 lines (272 loc) · 7.21 KB
/
Copy pathstatemachine.js
File metadata and controls
318 lines (272 loc) · 7.21 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Object files go here */
/*
* Creates a new state machine. f is the success function and e is the error function.
*/
function StateMachine(f, e) {
if (!(f instanceof Function) || !(e instanceof Function)) {
throw new Error("f and e both need to be functions.");
}
this.errorHandler = e;
this.successHandler = f;
}
/*
* Returns a state machine.
*/
StateMachine.prototype.StateMachine = function () {
return this;
}
/*
* Returns an error state machine. This is an internally used function, not for user's use.
*/
StateMachine.prototype.ErrorStateMachine = function() {
return this;
}
/*
* Creates a new state machine where this is the function to be ran before moving to the next state.
*/
Function.prototype.StateMachine = function() {
var f = this; // the function
var error = function(e, ek) {
ek(e)
}
var success = function(x, k, ek) {
try {
var res = f(x);
schedule(k, res);
} catch(err) {
ek(err)
}
}
return new StateMachine(success, error);
}
/*
* Creates a new error state machine where this is a function that handles the error. Not to be used directly by the user.
*/
Function.prototype.ErrorStateMachine = function() {
var h = this
var success = function(x, k) {
schedule(k, x)
}
var error = function(err, k, ek) {
try {
var res = h(err)
schedule(k, res)
} catch (err) {
ek(err)
}
}
return new StateMachine(success, error)
}
/*
* Chains a call to g on to the end of this. Returns a state machine.
*/
StateMachine.prototype.next = function (g) {
var f = this;
g = g.StateMachine();
var error = function(e, ek) {
ek(e)
}
var success = function (x, k, ek) {
f.successHandler(x,
function (y) { g.successHandler(y, k, ek); },
function(err) { g.errorHandler(err, ek); }
)
};
return new StateMachine(success, error);
}
/*
* Creates a state machine that will handle an error by calling h. This has no effect if there isn't an error.
*/
StateMachine.prototype.error = function(h) {
var f = this
h = h.ErrorStateMachine()
var error = function(e, ek) {
ek(e)
}
var success = function(x, k, ek) {
f.successHandler(x, function(y) {h.successHandler(y, k)}, function(err) {h.errorHandler(err, k, ek)})
}
return new StateMachine(success, error);
}
/*
* Runs an errorlets chain with x as the argument.
*/
StateMachine.prototype.run = function(x) {
this.successHandler(x, function() {}, function(err) {throw err;})
}
/*
* This runs an errorlets chain with x as the argument and f as a cleanup function to be ran at the very end.
*/
StateMachine.prototype.done = function(f, x) {
/* No function passed in */
if (arguments.length == 0) {
this.run(x)
} else {
this.next(f).run(x)
}
}
/*
* Starts a stream on source.
*/
StateMachine.prototype.stream = function(source) {
if (source instanceof Array) {
return this._stream_arr(source);
} else if (source instanceof Function) {
return this._stream_iter(source);
} else {
return this._stream_req(source);
}
}
// internal function that creates a stream over an array
StateMachine.prototype._stream_arr = function(arr) {
var i = 0;
// turn the array into an iterator
var iter = function() {
if (i < arr.length) {
return arr[i++];
} else {
return undefined;
}
}
var f = this;
var error = function(err, ek) {
ek(err)
}
var hasCalled = false;
resultOfPrev = null;
var success = function (x, k, ek, id, until) {
// we only want to call everything before the stream once
if (!hasCalled) {
f.successHandler(x,
function (y) {
resultOfPrev = y;
var next = iter();
if (next != undefined && !until) {
schedule(k, next);
}
},
function (err) {
throw err;
});
hasCalled = true;
} else {
// Once we've proccessed the chain before us once, we are getting here through until's setInterval.
// If we either reach the end of the array or have met the until condition, we want to clear in the interval.
var next = iter();
if (next != undefined && !until) {
schedule(k, next);
} else {
clearInterval(id)
}
}
}
return new Stream(success, error);
}
// an internal function that creates a stream from a server
StateMachine.prototype._stream_req = function(req) {
var f = this;
var curPendingReq = 0;
var totalReqMade = 0;
var error = function(err, ek) {
ek(err)
}
var hasCalled = false;
var success = function (x, k, ek, id, until) {
if (until.stop) return;
var handleResponse = function(xhr) {
if (until.stop) return;
if (xhr.status == 200) {
schedule(k, xhr.response, id, until);
} else {
ek(xhr.status);
}
}
var handleLoadFactory = function() {
var curReq = totalReqMade;
totalReqMade++;
return function (xhr) {
fireWhenTrue(function() {
return curPendingReq == curReq; },
function() {
curPendingReq++;
handleResponse(xhr);
});
}
}
if (!hasCalled) {
f.successHandler(x,
function (y) {
var handleLoad = handleLoadFactory();
buildAndSendXhr(req, handleLoad);
},
function (err) {
throw err;
});
hasCalled = true;
} else if (!until.stop) {
var handleLoad = handleLoadFactory();
buildAndSendXhr(req, handleLoad);
}
}
return new Stream(success, error);
}
StateMachine.prototype._stream_iter = function(iter) {
var f = this;
var error = function(err, ek) {
ek(err)
}
var hasCalled = false;
var resultOfPrev = null;
var success = function (x, k, ek, id, until) {
// we only want to call everything before the stream once
if (!hasCalled) {
f.successHandler(x,
function (y) {
resultOfPrev = y;
schedule(k, iter())
},
function (err) {
throw err;
});
hasCalled = true;
} else if (!until.stop) {
schedule(k, iter());
}
}
return new Stream(success, error);
}
/**
Makes a single request with req and passes the result down the chain.
example request:
{
type: 'POST',
url: 'ignore.com',
headers: [
['Content-type': 'application/x-www-form-urlencoded'],
...
]
data: 'user=person&pwd=password&organization=place&requiredkey=key'
}
*/
StateMachine.prototype.request = function (req) {
var f = this;
var error = function(e, ek) {
ek(e)
}
var success = function (x, k, ek) {
f.successHandler(x,
function (y) {
var handleLoad = function (xhr) {
if (xhr.status == 200) {
schedule(k, xhr.response);
} else {
ek(xhr.status);
}
}
buildAndSendXhr(req, handleLoad);
},
function(err) { ek(err) }
)
};
return new StateMachine(success, error);
}