aboutsummaryrefslogtreecommitdiff
path: root/node_modules/orchestrator/lib
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-10-10 03:43:44 +0200
committerFlorian Dold <florian.dold@gmail.com>2016-10-10 03:43:44 +0200
commitabd94a7f5a50f43c797a11b53549ae48fff667c3 (patch)
treeab8ed457f65cdd72e13e0571d2975729428f1551 /node_modules/orchestrator/lib
parenta0247c6a3fd6a09a41a7e35a3441324c4dcb58be (diff)
downloadwallet-core-abd94a7f5a50f43c797a11b53549ae48fff667c3.tar.xz
add node_modules to address #4364
Diffstat (limited to 'node_modules/orchestrator/lib')
-rw-r--r--node_modules/orchestrator/lib/runTask.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/node_modules/orchestrator/lib/runTask.js b/node_modules/orchestrator/lib/runTask.js
new file mode 100644
index 000000000..97dae22cb
--- /dev/null
+++ b/node_modules/orchestrator/lib/runTask.js
@@ -0,0 +1,66 @@
+/*jshint node:true */
+
+"use strict";
+
+var eos = require('end-of-stream');
+var consume = require('stream-consume');
+
+module.exports = function (task, done) {
+ var that = this, finish, cb, isDone = false, start, r;
+
+ finish = function (err, runMethod) {
+ var hrDuration = process.hrtime(start);
+
+ if (isDone && !err) {
+ err = new Error('task completion callback called too many times');
+ }
+ isDone = true;
+
+ var duration = hrDuration[0] + (hrDuration[1] / 1e9); // seconds
+
+ done.call(that, err, {
+ duration: duration, // seconds
+ hrDuration: hrDuration, // [seconds,nanoseconds]
+ runMethod: runMethod
+ });
+ };
+
+ cb = function (err) {
+ finish(err, 'callback');
+ };
+
+ try {
+ start = process.hrtime();
+ r = task(cb);
+ } catch (err) {
+ return finish(err, 'catch');
+ }
+
+ if (r && typeof r.then === 'function') {
+ // wait for promise to resolve
+ // FRAGILE: ASSUME: Promises/A+, see http://promises-aplus.github.io/promises-spec/
+ r.then(function () {
+ finish(null, 'promise');
+ }, function(err) {
+ finish(err, 'promise');
+ });
+
+ } else if (r && typeof r.pipe === 'function') {
+ // wait for stream to end
+
+ eos(r, { error: true, readable: r.readable, writable: r.writable && !r.readable }, function(err){
+ finish(err, 'stream');
+ });
+
+ // Ensure that the stream completes
+ consume(r);
+
+ } else if (task.length === 0) {
+ // synchronous, function took in args.length parameters, and the callback was extra
+ finish(null, 'sync');
+
+ //} else {
+ // FRAGILE: ASSUME: callback
+
+ }
+};