-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
121 lines (92 loc) · 2.67 KB
/
main.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
require("prototype");
require("./cop");
var Bluebird = require("bluebird");
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
function copPromise(originalPromise) {
this._layers = cop.LayerStack.clone();
if (originalPromise instanceof Bluebird) {
this._original = originalPromise;
} else {
var args = this._correctFunctionArgs(arguments);
this._original = construct(Bluebird, arguments);
}
}
copPromise.prototype._correctFunctionArgs = function (args) {
return Array.prototype.slice.call(args).map(function (argument) {
if (typeof argument === "function") {
return this._restoreContextCall(argument);
}
return argument;
}, this);
};
copPromise.prototype._restoreContextCall = function(func) {
var that = this;
return function () {
var oldLayerStack = cop.LayerStack.clone(), error, result;
cop.LayerStack = that._layers;
try {
result = func.apply(that, arguments);
} catch (e) {
error = e;
} finally {
cop.LayerStack = oldLayerStack;
}
if (error) {
throw error;
}
return result;
};
};
copPromise.prototype.withLayers = function (layers) {
this._layers.push({withLayers: layers});
};
copPromise.prototype.withoutLayers = function (layers) {
this._layers.push({withoutLayers: layers});
};
function prototypeOverRide(ownClass, functionName) {
ownClass.prototype[functionName] = function () {
var oldLayerStack = cop.LayerStack.clone();
cop.LayerStack = this._layers;
var resultArgs = this._correctFunctionArgs(arguments);
var result = this._original[functionName].apply(this._original, resultArgs);
if (result instanceof Bluebird) {
result = new copPromise(result);
}
cop.LayerStack = oldLayerStack;
return result;
};
}
function overRide(ownClass, functionName) {
ownClass[functionName] = function () {
var result = Bluebird[functionName].apply(Bluebird, arguments);
if (result instanceof Bluebird) {
result = new copPromise(result);
}
return result;
};
}
var prototypeOverRides = ["then", "catch", "error", "finally", "bind", "isFulfilled", "isRejected", "isPending", "value", "reason", "reflect", "delay", "timeout"];
prototypeOverRides.forEach(function (name) {
prototypeOverRide(copPromise, name);
});
overRides = ["resolve", "reject", "delay", "all"];
overRides.forEach(function (name) {
overRide(copPromise, name);
});
copPromise.promisify = function () {
var result = Bluebird.promisify.apply(Bluebird, arguments);
return function () {
var p = result.apply(this, arguments);
if (p instanceof Bluebird) {
p = new copPromise(p);
}
return p;
};
};
module.exports = copPromise;