This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathfuture.js
More file actions
executable file
·92 lines (92 loc) · 2.79 KB
/
Copy pathfuture.js
File metadata and controls
executable file
·92 lines (92 loc) · 2.79 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
var initializeFuture;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
module.exports = function(context) {
var Future;
if ('function' === typeof context) {
if (context.name) {
return Future = (function() {
__extends(Future, context);
Future.queue || (Future.queue = []);
function Future() {
this.queue = [];
Future.__super__.constructor.apply(this, arguments);
}
initializeFuture.call(Future);
return Future;
})();
} else {
context.isFuture = true;
return context;
}
}
};
initializeFuture = (function() {
var Pipeline, andThen, filter, isFuture, method, methods, next, originalMethods, replaceMethods, slice, _ref;
_ref = require('underscore'), filter = _ref.filter, methods = _ref.methods;
slice = [].slice;
Pipeline = require('./pipeline');
originalMethods = {};
method = function(context, methodName) {
originalMethods[methodName] = context[methodName];
return function() {
this.queue.push({
context: context,
methodName: methodName,
args: slice.call(arguments)
});
return this;
};
};
next = function(pipeline, err) {
var args, context, methodName, queued;
if (err != null) {
return pipeline.callback.call(this, err);
} else {
queued = pipeline.queue.shift();
if (queued != null) {
methodName = queued.methodName, context = queued.context, args = queued.args;
args.unshift(pipeline);
args.push(next.bind(this, pipeline));
try {
return originalMethods[methodName].apply(originalMethods, args);
} catch (e) {
return pipeline.callback.call(this, e, pipeline);
}
} else {
return pipeline.callback.call(this, null, pipeline);
}
}
};
andThen = function(callback) {
var pipeline;
pipeline = new Pipeline([], this.queue, callback);
if (pipeline.queue.length) {
next.call(this, pipeline);
}
this.queue = [];
return this;
};
isFuture = function(methodName) {
return this[methodName].isFuture;
};
replaceMethods = function() {
var methodName, _i, _len, _ref2;
_ref2 = filter(methods(this), isFuture.bind(this));
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
methodName = _ref2[_i];
this[methodName] = method(this, methodName);
}
return this.then = andThen;
};
return function() {
replaceMethods.call(this);
return replaceMethods.call(this.prototype);
};
})();