/
index.js
180 lines (142 loc) · 4.21 KB
/
index.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
module.exports = chainit;
function chainit(Constructor) {
function Chain() {
Constructor.apply(this, arguments);
}
Chain.prototype = Object.create(Constructor.prototype);
var Queue = require('queue');
var queues = [];
var currentDepth = 0;
var flushTimeout;
var flushedTasks = [];
function pushTo(depth, task) {
var queue = queues[depth] || (queues[depth] = getNewQueue(depth));
if (depth > 0) {
return queue.push(task);
}
// hack to handle cases where first chained calls
// are not added synchronously
// it means first chain start will occur after 4ms max
clearTimeout(flushTimeout);
process.nextTick(function() {
flushedTasks.unshift(function() {
queue.push(task);
});
flushTimeout = setTimeout(flush, 4);
});
}
function flush() {
var addTask;
while (addTask = flushedTasks.pop()) {
addTask();
}
}
function getNewQueue(newDepth) {
var queue = new Queue({
timeout: 0,
concurrency: 1
});
queue.on('drain', function() {
if (newDepth > 0) {
wakeupChain(newDepth);
}
if (!queues.slice(newDepth).some(hasPending)) {
currentDepth = newDepth;
}
});
function wakeupChain(depth) {
if (!queues[depth + 1] ||
!queues.slice(depth).some(hasPending)) {
queues[depth - 1].concurrency = 1;
queues[depth - 1].process();
}
if (depth > 1) {
wakeupChain(depth - 1);
} else {
if (!queues.some(hasPending)) {
depth = 0;
}
}
}
return queue;
}
// static methods, not chained
Object.keys(Constructor)
.forEach(function(name) {
Chain[name] = Constructor[name];
});
// prototype methods, chained
var allFn = Object.keys(Constructor.prototype);
if (allFn.length === 0) {
allFn = Object.keys(Object.getPrototypeOf(Constructor.prototype));
}
allFn
.forEach(function(fnName) {
Chain.prototype[fnName] = makeChain(fnName, Constructor.prototype[fnName]);
});
function makeChain(fnName, fn) {
return function chained() {
var ctx = this;
var callArguments = Array.prototype.slice.call(arguments);
var args = Array.prototype.slice.call(arguments);
var customCb;
if (callArguments[callArguments.length - 1] instanceof Function) {
callArguments.pop();
}
if((typeof args[args.length - 1] === 'function') && (!ctx.constructor || ctx.constructor.name !== 'WebdriverIO' || fnName.indexOf('execute') === -1 || args.length > 1)) {
customCb = args.pop();
}
var ldepth = currentDepth;
if (currentDepth > 0 && queues[currentDepth - 1].concurrency > 0) {
queues[currentDepth - 1].concurrency = 0;
}
var task = function(cb) {
process.nextTick(function() {
currentDepth = ldepth + 1;
args.push(function() {
var cbArgs = arguments,
err = arguments[0];
if (err instanceof Error && typeof err.addToCallStack === 'function') {
err.addToCallStack({
name: fnName,
args: niceArgs(callArguments)
});
} else if (err instanceof Error) {
err.message = '[' + fnName + niceArgs(callArguments) + '] <= \n ' + err.message;
}
if (customCb) {
customCb.apply(ctx, cbArgs);
} else if (err instanceof Error) {
// throw error if it isn't handled by a custom callback
throw err;
}
cb();
});
fn.apply(ctx, args);
});
}
pushTo(currentDepth, task);
return this;
}
}
Chain.prototype.__addToChain = function(fnName, fn) {
this[fnName] = makeChain(fnName, fn);
}
return Chain;
}
chainit.add = function add(to, fnName, fn) {
if (to.prototype && to.prototype.__addToChain) {
to.prototype.__addToChain(fnName, fn);
} else {
to.__addToChain(fnName, fn);
}
}
function hasPending(queue) {
return queue.length >= 1;
}
// c/p from https://github.com/admc/wd/blob/311c39ff2a2a3005405bc5872f420b359a5aa397/lib/utils.js#L108
function niceArgs(args) {
return JSON.stringify(args)
.replace(/^\[/, '(')
.replace(/\]$/, ')');
};