-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTry.js
More file actions
144 lines (128 loc) · 3.4 KB
/
Copy pathTry.js
File metadata and controls
144 lines (128 loc) · 3.4 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
var Try = (function () {
'use strict';
function Try(func) {
var r = function (func) {
if (func) {
r.addFunc(func);
}
return r.run();
};
for (var i in Try.fn) {
r[i] = Try.fn[i];
}
r.callStack = [];
r.pauseCounter = 0;
r.argsStack = [];
return r(func);
}
Try.throwFirstArgument = function () {
if (arguments[0]) {
throw arguments[0];
}
return arguments[1];
};
Try.throwFirstArgumentInArray = function () {
return Array.prototype.map.call(arguments, function (args) {
if (args[0]) {
throw args[0];
}
return args.slice(1);
});
};
Try.currentTry = null; //is being set in run function
Try.pause = function (n) {
var r = Try.currentTry;
r.pauseCounter += n || 1;
return function resume() {
var args = Array.prototype.slice.call(arguments);
setTimeout(function () {
r.pauseCounter -= 1;
r.argsStack.push(args);
if (r.pauseCounter === 0) {
r.run();
}
});
return arguments[0];
};
}
Try.fn = {
addFunc: function (func, type) {
this.callStack.push({
type: type || 'then',
func: func
});
},
runFunc: function (funcDescription) {
try {
var tryBefore = Try.currentTry;
var last;
Try.currentTry = this;
if (typeof funcDescription.func === 'function') {
last = funcDescription.func.apply(this, this.argsStack[this.argsStack.length - 1]);
} else {
last = this.argsStack;
for (var i = 0; i < funcDescription.func.length; i += 1) {
last = funcDescription.func[i].apply(this, last);
}
}
if (this.pauseCounter > 0 && last !== undefined) {
this.argsStack.push(last);
} else if (typeof last === 'function' && last.callStack) {
Try.currentTry = this;
this.argsStack = [];
last(Try.pause());
} else {
this.argsStack = last ? [[last]] : [];
}
Try.currentTry = tryBefore;
} catch (e) {
this.error = e;
this.asyncThrowError();
}
},
asyncThrowError: function () {
var r = this;
setTimeout(function () {
if (r.error) {
throw r.error;
}
});
},
catch: function (callback) {
this.addFunc(callback, 'catch');
return this.run();
},
finally: function (callback) {
this.addFunc(callback, 'finally');
return this.run();
},
run: function () {
if (this.pauseCounter > 0) {
return this;
}
var func = this.callStack.shift();
if (!func) {
return this;
}
if (func.type === 'then' && !this.error) {
this.runFunc(func);
} else if (func.type === 'catch' && this.error) {
this.argsStack = [[this.error]];
this.error = null;
this.runFunc(func);
} else if (func.type === 'finally') {
if (this.error) {
this.argsStack = [[this.error]];
this.error = null;
}
this.runFunc(func);
}
return this.run();
}
};
return Try;
}());
//nodejs support
if (typeof module !== 'undefined' && module.exports) {
module.exports = Try;
}