This repository has been archived by the owner on Sep 19, 2020. It is now read-only.
/
nicely.js
209 lines (197 loc) · 6.06 KB
/
nicely.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
;(function(root) {
var fnBind = Function.prototype.bind;
var arraySlice = Array.prototype.slice;
var bind = function(fn, context) {
var args;
if (fnBind && fn.bind)
return fnBind.apply(fn, arraySlice.call(arguments, 1));
args = arraySlice.call(arguments, 2);
return function bound() {
return fn.apply(context, args.concat(arraySlice.call(arguments)));
};
};
var defer = (function() {
if (typeof process !== 'undefined')
return bind(process.nextTick, process);
return function(fn) {
setTimeout(fn, 1);
};
}());
var nicely = function nicely(times, callback) {
if (typeof callback !== 'function')
throw new TypeError('callback must be a function');
// whether an error has occured
var errored = false;
// the result parameters
var params = {};
// create an anonymous function that sets the field
return function next(field) {
// handles a response
// *must be called only once*
return function after(err, result) {
// ignore if errored or done
if (errored || times <= 0)
return;
// callback with error
if (err)
return callback(errored = err, field);
// set parameter
params[field] = result;
// call back if done
if (--times === 0)
callback(null, params);
};
};
};
nicely.simply = function simply(times, callback) {
if (typeof callback !== 'function')
throw new TypeError('callback must be a function');
// whether an error has occurred
var errored = false;
// array of active tasks
// handles a response
// *must be called #times*
return function next(err) {
// ignore if errored or done
if (errored || times <= 0)
return;
// callback with error
if (err)
return callback(errored = err);
// call back if done
if (--times === 0)
callback(null);
};
};
nicely.directly = function directly(times, callback) {
if (typeof callback !== 'function')
throw new TypeError('callback must be a function');
// whether an error has occured
var errored = false;
// the result parameters
var params = new Array(times);
// the ticker
var ticker = 0;
// handles a response
// *must be called #times*
return function next(err, result) {
// ignore if errored or done
if (errored || ticker >= times)
return;
// callback with error
if (err)
return callback(errored = err, ticker);
// set parameter
params[ticker++] = result;
// call back if done
if (ticker === times)
callback(null, params);
};
};
// sequentially fits with the theme, but is hard to type right
nicely.sequentially =
nicely.sequence = function sequentially(callback) {
if (typeof callback !== 'function')
throw new TypeError('callback must be a function');
// current done state
var state = false;
// the result parameters
var params = {};
// the task queue
var taskQueue = [];
// handles the end of one task
var endTask = function(name, err, result) {
// just in case they called back and then threw
if (state)
return;
if (err) {
state = true;
return callback.call(params, err, name);
}
params[name] = result;
processTask();
};
// processes one task
var processTask = function() {
// fetch the next task
var item = taskQueue.shift();
if (!item) {
// we're done!
state = true;
return callback.call(params, null, params);
}
// make sure we don't allow uncaught exceptions, a common problem here
try {
item[0].apply(params, item[2]);
} catch (err) {
endTask(item[1], err);
}
};
// begins queue processing on (start of) next tick
// should defer happen in processTask, for every call?
defer(processTask);
// queues another function
return function queue(/*name, fn, args...*/) {
var args = arraySlice.call(arguments);
var name = args.shift();
args.push(bind(endTask, this, name));
taskQueue.push([args.shift(), name, args]);
};
};
var intentlyDefaults = {
times: 0,
backoff: 2,
initial: 100,
maximum: 5000
// defer: false
// abort: function(err) -> boolean
};
// TODO: use different variables for callback and fn in different scores?
// to avoid confusion.
// TODO: need to make opts optional
nicely.intently = function intently(opts, callback) {
var callback = arguments[arguments.length - 1];
if (typeof callback !== 'function')
throw new TypeError('callback must be a function');
opts = typeof opts === 'object' ? opts : {};
var options = {};
options.defer = !!opts.defer;
options.abort = typeof opts.abort === 'function' ? opts.abort : null;
for (var key in intentlyDefaults)
options[key] = typeof opts[key] === 'number' ? opts[key] : intentlyDefaults[key];
return function begin(fn) {
var calls = options.times || null;
var delay = options.initial;
var retry = function(err, result) {
if (!err)
return callback.apply(null, arguments);
if ((calls !== null && --calls === 0) || (options.abort && options.abort(err)))
return callback(err);
setTimeout(boundFn, delay);
delay = Math.min(delay * options.backoff, options.maximum);
};
var fnArgs = arraySlice.call(arguments, 1);
fnArgs.push(retry);
var boundFn = bind.apply(root, [fn, root].concat(fnArgs));
if (options.defer)
defer(boundFn);
else
fn.apply(root, fnArgs);
};
};
nicely.version = nicely.VERSION = '0.1.4';
nicely._ = {
nicely: nicely
};
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined')
exports = module.exports = nicely;
exports.nicely = nicely;
} else {
var previousNicely = root.nicely;
root.nicely = nicely;
nicely.noConflict = function() {
root.nicely = previousNicely;
};
}
})(this);