-
Notifications
You must be signed in to change notification settings - Fork 109
/
task.js
575 lines (530 loc) · 20.5 KB
/
task.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
"use strict";
// A Task is a cancelable variant of a promise.
// Like a promise, the observable is a proxy for the result of some work.
// The interface is largely the same, but a observable can only have one
// observer.
// For example, calling `then` a second time will throw an error.
// Instead, if a task has multiple observers, you can sacrifice cancelability
// by coercing it to a promise, or use `fork` before observing it.
// If every fork is cancelled, the cancelation will propagate back to the
// original job.
//
// The price of cancelability is a less robust system and more book keeping.
// A system that makes a great deal of use of tasks allows information to flow
// from any observable to any related task, even if distantly related.
// The cancelation of one task can propagate throughout an entire system of
// tasks, both forward and backward between consumers and producers.
// In exchange the system gains the ability to either free or avoid consuming
// resources proactively.
var asap = require("asap");
var WeakMap = require("weak-map");
// ## Task
//
// The consumer side of a task should receive the task's observable.
// This object provides the ability to register exactly one observer for the
// result of the task, and the ability to cancel the task with an error.
function Task(setup, thisp) {
var deferred = Task.defer();
var handler = handlers.get(deferred.out);
handler.cancel = setup.call(thisp, deferred.in.return, deferred.in.throw);
return deferred.out;
}
/*
TODO Task.prototype = Object.create(Observable);
Such that it is possible to create parallel signaling for status and estimated
time to completion, or other arbitrary signals from the resolver to the
observable.
*/
// The `done` method registers an observer for any combination of completion or
// failure with the given methods and optional context object.
// The `done` method does not return a new task and does not capture errors
// thrown by the observer methods.
Task.prototype.done = function (onreturn, onthrow, thisp) {
var self = this;
var handler = Task_getHandler(self);
handler.done(onreturn, onthrow, thisp);
};
// The `then` method registers an observer for any combination of completion or
// failure, and creates a new task that will be completed with the result of
// either the completion or failure handler.
Task.prototype.then = function (onreturn, onthrow, thisp) {
// TODO accept status and estimated time to completion arguments in
// arbitrary order.
var handler = Task_getHandler(this);
var task = Task.defer(this.cancel, this);
var _onreturn, _onthrow;
if (typeof onreturn === "function") {
_onreturn = function (value) {
try {
task.in.return(onreturn.call(thisp, value));
} catch (error) {
task.in.throw(error);
}
};
}
if (typeof onthrow === "function") {
_onthrow = function (error) {
try {
task.in.return(onthrow.call(thisp, error));
} catch (error) {
task.in.throw(error);
}
};
}
this.done(_onreturn, _onthrow);
return task.out;
};
// The `spread` method fills a temporary need to be able to spread an array
// into the arguments of the completion handler of a `then` observer.
// ECMAScript 6 introduces the ability to spread arguments into an array in the
// signature of the method.
Task.prototype.spread = function (onreturn, onthrow, thisp) {
return this.then(function (args) {
return onreturn.apply(thisp, args);
}, onthrow, thisp);
};
// The `catch` method registers an error observer on a task and returns a new
// task to be completed with the result of the observer.
// The observer may return another task or thenable to transfer responsibility
// to complete this task to another stage of the process.
Task.prototype.catch = function (onthrow, thisp) {
return this.then(null, onthrow, thisp);
};
// The `finally` method registers an observer for when the task either
// completes or fails and returns a new task to perform some further work but
// forward the original value or error otherwise.
Task.prototype.finally = function (onsettle, thisp) {
return this.then(function (value) {
return onsettle.call(thisp).then(function Task_finally_value() {
return value;
})
}, function (error) {
return onsettle.call(thisp).then(function Task_finally_error() {
throw error;
});
});
};
// The `get` method creates a task that will get a property of the completion
// object for this task.
Task.prototype.get = function (key) {
return task.then(function (object) {
return object[key];
});
};
// The `call` method creates a task that will call the function that is the
// completion value of this task with the given spread arguments.
Task.prototype.call = function (thisp /*, ...args*/) {
var args = [];
for (var index = 1; index < arguments.length; index++) {
args[index - 1] = arguments[index];
}
return task.then(function (callable) {
return callable.apply(thisp, args);
});
};
// The `invoke` method creates a task that will invoke a property of the
// completion object for this task.
Task.prototype.invoke = function (name /*, ...args*/) {
var args = [];
for (var index = 1; index < arguments.length; index++) {
args[index - 1] = arguments[index];
}
return task.then(function (object) {
return object[name].apply(object, args);
});
};
// The `thenReturn` method registers an observer for the completion of this
// task and returns a task that will be completed with the given value when
// this task is completed.
Task.prototype.thenReturn = function (value) {
return this.then(function () {
return value;
});
};
// The `thenReturn` method registers an observer for the completion of this
// task and returns a task that will fail with the given error when this task
// is completed.
Task.prototype.thenThrow = function (error) {
return this.then(function () {
return error;
});
};
// Effects cancelation from the consumer side.
Task.prototype.throw = function (error) {
var handler = Task_getHandler(this);
if (handler.cancel) {
handler.throw(error);
}
};
// A task can only be observed once, but it can be forked.
// The `fork` method returns a new task that will observe the same completion
// or failure of this task.
// Hereafter, this task and all forked tasks must *all* be cancelled for this
// task's canceller to propagate.
Task.prototype.fork = function () {
// The fork method works by fiddling with the handler of this task.
// First, we extract this task's handler and make it the new parent for two
// child tasks.
var parentHandler = Task_getHandler(this);
parentHandler.done(function (value) {
left.in.return(value);
right.in.return(value);
}, function (error) {
left.in.throw(error);
right.in.throw(error);
});
/* TODO estimated time to completion forwarding */
/* TODO use a signal operator to propagate cancellation */
var leftCanceled = false, rightCanceled = false;
var left = Task.defer(function (error) {
if (leftCanceled) {
return;
}
leftCanceled = true;
if (rightCanceled) {
parentHandler.throw(error);
}
});
var right = Task.defer(function (error) {
if (rightCanceled) {
return;
}
rightCanceled = true;
if (leftCanceled) {
parentHandler.throw(error);
}
});
// We replace our own handler with the left child
handlers.set(this, Task_getHandler(left.out));
// And return the task with the right child handler
return right.out;
};
// The `delay` method of a task adds a delay of some miliseconds after the task
// *completes*.
// Cancelling the delayed task will cancel either the delay or the delayed
// task.
Task.prototype.delay = function (ms) {
var self = this;
var task = Task.defer(function cancelDelayedTask() {
self.throw();
clearTimeout(handle);
});
var result = Task.defer();
var handle = setTimeout(function taskDelayed() {
task.in.return(result.out);
}, ms);
this.done(function (value) {
result.in.return(value);
}, function (error) {
task.in.throw(error);
});
return task.out;
};
// The `timeout` method will automatically cancel a task if it takes longer
// than a given delay in miliseconds.
Task.prototype.timeout = function (ms, message) {
var self = this;
var task = Task.defer(function cancelTimeoutTask() {
this.throw();
clearTimeout(handle);
}, this);
var handle = setTimeout(function Task_timeout() {
self.throw();
task.in.throw(new Error(message || "Timed out after " + ms + "ms"));
}, ms);
this.done(function Task_timeoutValue(value) {
clearTimeout(handle);
task.in.return(value);
}, function Task_timeoutError(error) {
clearTimeout(handle);
task.in.throw(error);
});
return task.out;
};
// ## Completer
//
// The producer side of a task should get a reference to a task's resolver.
// The object provides the capability to settle the task with a completion
// value or a failure error.
function Completer(handler) {
// The task resolver implicitly binds its return and throw methods so these
// can be passed as free functions.
this.return = this.return.bind(this);
this.throw = this.throw.bind(this);
}
// The `return` method sets the tasks state to "fulfilled" (in the words of
// promises) or "completed" (in the vernacular of tasks), with a given value.
// If the corresponding observer was registered already, this will inform
// the observer as soon as possible.
// If the corresponding observer gets registered later, it will receive the
// result as soon as possible thereafter.
Completer.prototype.return = function (value) {
var handler = Task_getHandler(this);
handler.become(Task.return(value));
};
// The `throw` method sets the tasks state to "rejected" (a term borrowed from
// promises) or "failed" (the corresponding task jargon), with the given error.
// Again, if the corresponding observer was registered already, this will
// inform the observer as soon as possible.
// If the corresponding observer gets registered later, it will receive the
// result as soon as possible thereafter.
Completer.prototype.throw = function (error) {
var handler = Task_getHandler(this);
handler.become(Task.throw(error));
};
// ## Task
//
// The task constructor creates a resolver "in" and an observer "out" pair
// with some shared internal state.
// Particularly, since tasks can be canceled, the task constructor accepts a
// reference to the cancellation method and optionally the instance that hosts
// it.
module.exports = Task;
Task.defer = function (cancel, thisp) { // TODO estimate, label
var handler = new TaskHandler(); // TODO polymorph constructors
var input = Object.create(Completer.prototype);
var output = Object.create(Task.prototype);
Completer_bind(input);
handlers.set(input, handler);
handlers.set(output, handler);
handler.cancel = cancel;
handler.cancelThisp = thisp;
return {in: input, out: output};
}
function Completer_bind(completer) {
completer.return = completer.return.bind(completer);
completer.throw = completer.throw.bind(completer);
};
// The `isTask` utility method allows us to identify a task that was
// constructed by this library.
// This library does not attempt to make it provably impossible to trick the
// Task.
Task.isTask = isTask;
function isTask(object) {
return (
Object(object) === object &&
!!handlers.get(object) &&
object instanceof Task
);
};
// The `isThenable` method is used internally to identify other singular
// asynchronous duck types, including promises, which can be coerced into
// tasks.
function isThenable(object) {
return object && typeof object === "object" && typeof object.then === "function";
}
// The `return` function lifts a value into a task that has already completed
// with a value.
Task.return = function (value) {
if (isTask(value)) {
return value;
} else if (isThenable(value)) {
// FIXME sloppy with closures and should use weakmap of value to task
var deferred = Task.defer();
asap(function () {
value.then(function (value) {
deferred.in.return(value);
}, function (error) {
deferred.in.throw(value);
});
});
return deferred.out;
} else {
var handler = new TaskHandler();
handler.state = "fulfilled";
handler.value = value;
var task = Object.create(Task.prototype);
handlers.set(task, handler);
return task;
}
};
// The `throw` function lifts an error into a task that has already failed with
// that error.
Task.throw = function (error) {
var handler = new TaskHandler();
handler.state = "rejected";
handler.error = error;
var task = Object.create(Task.prototype);
handlers.set(task, handler);
return task;
};
// The `all` function accepts an array of tasks, or values that can be coerced
// into tasks, and produces a task that when completed will produce an array of
// the individual completion values.
Task.all = function Task_all(tasks) {
// If the task is cancelled, or if any individual task fails, all of the
// outstanding individual tasks will be cancelled.
function cancelAll(error) {
for (var otherIndex = 0; otherIndex < tasks.length; otherIndex++) {
// Note that throwing an error upstream consitutes talking back to the producer.
// This is a reminder that tasks are a cooperation between a single
// consumer and a single producer and that information flows both
// ways and in fact allows information to propagate laterally by
// passing up one stream and down another.
tasks[otherIndex].throw(error);
}
result.in.throw(error);
}
// The number of outstanding tasks, tracked to determine when all tasks are
// completed.
var remaining = tasks.length;
var result = Task.defer(cancelAll);
var results = Array(tasks.length);
/* TODO estimated time to completion, label signals */
var estimates = [];
var estimate = -Infinity;
var setEstimate;
var estimates = tasks.map(function Task_all_each(task, index) {
task = tasks[index] = Task.return(task); // Coerce values to tasks
task.done(function Task_all_anyReturn(value) {
results[index] = value;
if (--remaining === 0) {
result.in.return(results);
}
}, cancelAll);
});
return result.out;
};
// The `any` method accepts an array of tasks, or value coercable to tasks, and
// returns a task that will receive the value from the first task that
// completes with a value.
// After one succeeds, all remaining tasks will be cancelled.
// If one of the tasks fails, it will be ignored.
// If all tasks fail, this task will fail with the last error.
Task.any = function (tasks) {
/* TODO */
};
// The `any` method accepts an array of tasks, or value coercable to tasks, and
// returns a task that will receive the value or error of the first task that
// either completes or fails.
// Afterward, all remaining tasks will be cancelled.
Task.race = function (tasks) {
/* TODO */
};
// The `delay` method accepts a duration of time in miliseconds and returns a
// task that will complete with the given value after that amount of time has
// elapsed.
Task.delay = function (ms, value) {
return Task.return(value).delay(ms);
};
// ## TaskHandler
// The resolver and observable side of a task share a hidden internal record
// with their shared state.
// Handlers are an alternative to using closures.
var handlers = new WeakMap();
function TaskHandler() {
// When a task is resolved, it "becomes" a different task and its
// observable, if any, must be forwarded to the new task handler.
// In the `become` method, we also adjust the "handlers" table so any
// subsequent request for this handler jumps to the end of the "became"
// chain.
this.became = null;
// Tasks may be created with a corresponding canceler.
this.cancel = null;
this.cancelThisp = null;
// Tasks may be "pending", "fulfilled" with a value, or "rejected" with an
// error
this.state = "pending";
this.value = null;
this.error = null;
// A task may only be observed once.
// Any future attempt to observe a task will throw an error.
this.observed = false;
// Since a task can only be observed once, we only need to track one
// handler for fulfillment with a value or rejection with an error.
// A promise keeps an array of handlers to forward messages to.
// These handlers can be forgotten once a task settles since thereafter
// the observer would be informed immediately.
this.onreturn = null;
this.onthrow = null;
// The object to use as `this` in the context of `onreturn` and `onthrow`.
this.thisp = null;
}
// Since a task handler can become another task handler, this utility method
// will look up the end of the chain of "became" properties and rewrite the
// handler look up table so we never have to walk the same length of chain
// again.
function Task_getHandler(task) {
var handler = handlers.get(task);
while (handler && handler.became) {
handler = handler.became;
}
handlers.set(task, handler);
return handler;
}
// The `done` method is kernel for subscribing to a task observer.
// If the task has already completed or failed, this will also arrange for the
// observer to be notified as soon as possible.
TaskHandler.prototype.done = function (onreturn, onthrow, thisp) {
if (this.observed) {
throw new Error("Can't observe a task multiple times. Use fork");
}
this.observed = true;
this.onreturn = onreturn;
this.onthrow = onthrow;
this.thisp = thisp;
// If we are observing a task after it completed or failed, we dispatch the
// result immediately.
if (this.state !== "pending") {
// Instead of passing a callable closure, we pass ourself to avoid
// allocating another object.
// The task handler serves as a psuedo-function by implementing "call".
asap(this);
}
// We handle the case of observing *before* completion or failure in the
// `become` method.
};
// Above, we pass the task handler to `asap`.
// The event dispatcher treats functions and callable objects alike.
// This method will get called if this task has settled into a "fulfilled" or
// "rejected" state so we can call the appropriate handler.
TaskHandler.prototype.call = function () {
if (this.state === "fulfilled") {
if (this.onreturn) {
this.onreturn.call(this.thisp, this.value);
}
} else if (this.state === "rejected") {
if (this.onthrow) {
this.onthrow.call(this.thisp, this.error);
} else {
throw this.error;
}
}
// We release the handlers so they can be potentially garbage collected.
this.onreturn = null;
this.onthrow = null;
this.thisp = null;
};
// The `become` method is the kernel of the task resolver.
TaskHandler.prototype.become = function (task) {
var handler = Task_getHandler(task);
// A task can only be resolved once.
// Subsequent resolutions are ignored.
// Ignoring, rather than throwing an error, greatly simplifies a great
// number of cases, like racing tasks and cancelling tasks, where handling
// an error would be unnecessary and inconvenient.
if (this.state !== "pending") {
return;
}
// The `became` property gets used by the internal handler getter to
// rewrite the handler table and shorten chains.
this.became = handler;
// Once a task completes or fails, we no longer need to retain the
// canceler.
this.cancel = null;
this.cancelThisp = null;
// If an observer subscribed before it completed or failed, we forward the
// resolution.
// If an observer subscribes later, we take care of that case in `done`.
if (this.observed) {
handler.done(this.onreturn, this.onthrow, this.thisp);
}
};
// The `throw` method is used by the promise observer to cancel the task from
// the consumer side.
TaskHandler.prototype.throw = function (error) {
if (this.cancel) {
this.cancel.call(this.cancelThisp);
}
this.become(Task.throw(error || new Error("Consumer canceled task")));
};