-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
do.js
282 lines (266 loc) · 5.81 KB
/
do.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use strict';
/**
* Do constructor.
*
* Examples:
*
* // Create 1 todo
* var todo = new Do();
* // Create 3 todos.
* var todo = new Do(3);
* // Without a "new" statement.
* var todo = Do();
*
* @param {Number} [amount] amount of todos
* @api public
*/
function Do(amount) {
if (!(this instanceof Do)) {
return new Do(amount);
}
this._amount = amount || 0;
this.errors = [];
this.done = this.done.bind(this);
}
module.exports = Do;
/**
* Accumulated errors.
*
* @api public
* @property errors
*/
Do.prototype.errors;
/**
* Setter/getter for amount of todos.
*
* Examples:
*
* // Get the current amount.
* todo.amount();
* // Set a new amount.
* todo.amount(3);
*
* @param {Number} [value] if passed works as a setter, otherwise as a getter.
* @return {Number|Do} amount or this
* @api public
*/
Do.prototype.amount = function(value) {
if (value !== null) {
this._amount = value;
return this;
}
return this._amount;
};
/**
* Increment amount of todos.
*
* Examles:
*
* // Add 1 todo.
* todo.inc();
* // Add 3 todos.
* todo.inc(3)
*
* @param {Number} [value] add the value to the current amount or just 1.
* @return {Do} this
* @api public
*/
Do.prototype.inc = function(value) {
this._amount += value || 1;
return this;
};
/**
* Decrement amount of todos.
*
* Examples:
*
* // Reduce at 1 todo.
* todo.dec();
* // Reduce at 3 todos.
* todo.dec(3)
*
* @param {Number} [value] substitute the value from the current amount or 1
* @return {Do} this
* @api public
*/
Do.prototype.dec = function(value) {
this._amount -= value || 1;
return this;
};
/**
* Set an error callback or trigger an error.
*
* Error callback is called EVERY time an error is passed
* to `Do#done` or `Do#error`.
*
* Examples:
*
* // Define error callback.
* var send;
* todo.error(function(err) {
* console.error(err);
* // Ensure sending response only once.
* if (!send) {
* req.send('Error');
* send = true;
* }
* });
* // Trigger an error manually.
* todo.error(new Error());
*
* @param {Function|Error} [err] if function passed, it is used as an
* error callback, otherwise it can be an error which is passed
* to the error callback defined before.
* @return {Do} this
* @api public
*/
Do.prototype.error = function(err) {
if (typeof(err) === 'function') {
this._errorCallback = err;
} else {
this.done(err);
}
return this;
};
/**
* Set a success callback or trigger success.
*
* If all todos are done without errors - success callback will be
* called by Do#done.
* Success callback is called ONLY ONCE.
*
* Examples:
*
* // Define a success callback.
* todo.success(function() {
* res.send('Success');
* });
* // Trigger success manually.
* todo.success();
*
* @param {Function} [fn] if function passed, it is used as success callback,
* otherwise, success callback defined before, will be invoked.
* @return {Do} this
* @api public
*/
Do.prototype.success = function(fn) {
if (typeof(fn) === 'function') {
this._successCallback = fn;
} else {
this._validateCallbacks();
this._complete();
}
return this;
};
/**
* Set a complete callback or trigger complete.
*
* Complete callback is always called ONCE if it is defined
* independent of errors.
*
* Examples:
*
* // Define a complete callback.
* todo.complete(function() {
* console.error(this.errors);
* res.send('Complete');
* });
* // Trigger complete manually.
* todo.complete();
*
* @param {Function} [fn] - if function passed, it is used as complete callback,
* otherwise, complete callback defined before, will be invoked.
* @return {Do} this
* @api public
*/
Do.prototype.complete = function(fn) {
if (typeof(fn) === 'function') {
this._completeCallback = fn;
} else {
this._validateCallbacks();
this._complete();
}
return this;
};
/**
* Indicate a done task. If an error is passed as first parameter - error will
* be triggered.
*
* Examples:
*
* // with error
* todo.done(err);
* // without error
* todo.done();
* // context of `todo.done` is ensured.
* someTask(todo.done);
*
* @param {Error} [err] - if error passed, error callback will be called,
* otherwise if all todos without errors are done, success callback
* will be called.
* @return {Do} this
* @api public
*/
Do.prototype.done = function(err) {
this._validateCallbacks();
this._amount--;
this._error(err);
if (this._amount === 0) {
this._complete();
} else if (this._amount < 0) {
this._error(new Error('Do#done called more times than expected.'));
}
return this;
};
/**
* Ensure callbacks are defined.
*
* @api private
*/
Do.prototype._validateCallbacks = function() {
if (
!this._completeCallback &&
!(this._errorCallback && this._successCallback)
) {
throw new Error(
'Either "error" and "success" or "complete" callback should be defined.'
);
}
};
/**
* Execute `success` and `complete` callbacks.
*
* @api private
*/
Do.prototype._complete = function() {
if (this._successCallback && !this.errors.length) {
if (this._successCalled) {
this._error(new Error('Success can be called only once.'));
} else {
this._successCalled = true;
this._successCallback();
}
}
if (this._completeCallback) {
if (this._completeCalled) {
this._error(new Error('Complete can be called only once.'));
} else {
this._completeCalled = true;
this._completeCallback();
}
}
};
/**
* Execute error callback, accumulate errors.
*
* @param {Error} [err]
* @api private
*/
Do.prototype._error = function(err) {
if (err) {
this.errors.push(err);
if (this._errorCallback) {
this._errorCallback(err);
}
}
};