This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
/
multi.js
213 lines (152 loc) · 4.66 KB
/
multi.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
Multi
Run asynchronous methods sequentially or in parallel.
Allows methods from context to be queued and executed in asynchronously
in order, returning the results & errors of all operations.
@param {object} context
@param {object} config
*/
var _ = require('underscore');
var slice = Array.prototype.slice;
var EventEmitter = require('events').EventEmitter;
function Multi(context, config) {
var self = this;
var restricted = ['exec', 'multi', 'constructor'];
// Ensure we get all methods including those from the prototype
var properties = _.uniq(Object.getOwnPropertyNames(context).concat(_.methods(context)));
setInitialState();
config = _.extend({
interrupt: false,
parallel: false,
flush: true,
}, config || {});
Object.defineProperty(this, '__config', {
value: config,
writable: true,
enumerable: false,
configurable: true
});
var callback, counter, errored, errors, stack, results; // Initial state
this.exec = function(cb) {
this.emit('pre_exec');
callback = cb;
if (stack.length > 0) {
if (config.parallel) {
this.promise = new EventEmitter();
this.promise.on('finished', function() {
var preparedArgs = setInitialState(true); // Reset state, then return
self.emit('post_exec');
callback.apply(self, preparedArgs);
});
for (var ob,i=0,len=stack.length; i < len; i++) {
ob = stack[i];
context[ob.caller].apply(context, ob.args);
}
} else {
var first = stack[0];
context[first.caller].apply(context, first.args);
}
} else {
self.emit('post_exec');
callback.call(context, new Error("The call stack is empty"), []);
}
}
// Handles the internal async execution loop in order
function resultsCallback() {
var args = slice.call(arguments, 0);
var err = args.shift();
if (err) {
if (config.interrupt === true) {
errors.push(err);
results.push(null);
callback.call(self, errors, results);
return;
} else {
errored = true;
args = null;
}
} else if (args.length === 0) {
args = 'OK';
} else if (args.length == 1) {
args = args[0];
}
errors.push(err);
results.push(args);
self.emit('each', err, counter, args);
if (config.parallel) {
// Parallel
if (++counter == stack.length) {
self.promise.emit('finished');
}
} else {
// Sequential
if (++counter == stack.length) {
var preparedArgs = setInitialState(true); // Reset state, then return
self.emit('post_exec');
callback.apply(self, preparedArgs);
} else {
var next = stack[counter];
context[next.caller].apply(context, next.args);
}
}
}
// Queues the callback
function queue(args) {
args.push(resultsCallback);
stack.push({caller: this.caller, args: args});
}
// Generates the queuing function
function dummy(caller) {
return function() {
queue.call({caller: caller}, slice.call(arguments, 0));
return self;
}
}
// Sets the initial state of multi
function setInitialState(ret) {
var e, r;
// Before resetting, keep a copy of args
if (ret) {
e = (errored ? errors : null);
r = results;
}
// Reset runtime vars to their default state
counter = 0;
errored = false;
errors = [];
results = [];
// Flush stack on first run or when config.flush is true
if (stack == null || config.flush) stack = [];
// Return prepared arguments
if (ret) return [e, r];
}
// Create the queuing functions for the storage methods of the multi
for (var key,i=0,len=properties.length; i < len; i++) {
key = properties[i];
if (context[key] instanceof Function) {
if (restricted.indexOf(key) >= 0) continue;
this[key] = dummy(key);
}
}
// Get Prototype methods
// Prevent conflicts with context's exec method if it exists.
// Move original exec method from context into __exec.
if (context.exec instanceof Function) {
this.__exec = dummy('exec');
}
// We need a new event emitter per instance, can't use util.inherits()
_.extend(this, new EventEmitter());
}
function setNonEnumerable(props) {
var p, self = this;
for (var i=0,len=props.length; i < len; i++) {
p = props[i];
Object.defineProperty(this, p, {
value: self[p],
writable: true,
enumerable: false,
configurable: true
});
}
}
module.exports = Multi;