-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
250 lines (217 loc) · 4.94 KB
/
queue.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
/**
* Queue - Simple queue engine for Node.js
*
* Example usage:
* var Queue = require('mel-queue');
* var q = new Queue();
* q.add(function(queue) {
* setTimeout(function() {
* console.log('Action 1 called');
* queue.next(['new_param']);
* }, 200);
* });
* q.add(function(param, queue) {
* setTimeout(function() {
* console.log('Action 2 called with param = '+param);
* queue.next();
* }, 100);
* }, ['default_param']);
* q.on('end', function() {
* console.log('Queue ended');
* });
* q.run();
*/
// Load modules
var util = require('util');
var events = require('events');
/**
* Queue model
*
* @param [store] {Object} - Optional, store to transfer some vars between actions
* @param [context] {Object} - Optional [default: Queue instance], "this" context for all actions and events
* @constructor
*/
var Queue = function(store, context) {
// Custom variables store
this.store = store || {};
// "this" context for callbacks
this.context = context || this;
// Actions queue
this.queue = [];
// Pointers of started actions
this.activeActions = {};
events.EventEmitter.call(this);
};
// Add EventEmitter as parent
util.inherits(Queue, events.EventEmitter);
/**
* Add an action to the end of the queue
*
* @param fn {Function} - Action
* @param [args] {Array} - Optional, Default action arguments
*
* @returns {Object} - This queue
*/
Queue.prototype.add = function(fn, args) {
var action = {
fn: fn,
args: args || []
};
this.queue.push(action);
return this;
};
/**
* Insert an action to the front of the queue
*
* @param fn {Function} - Action
* @param [args] {Array} - Optional, Default action arguments
*
* @returns {Object} - This queue
*/
Queue.prototype.insert = function(fn, args) {
var action = {
fn: fn,
args: args || []
};
this.queue.unshift(action);
return this;
};
/**
* Run actions queue
*
* @param [args] {Array} - Optional, Default action arguments
*
* @returns {Object} - This queue
*/
Queue.prototype.run = function(args) {
this.next(args);
return this;
};
/**
* Call next action in queue
*
* @param [args] {Array} - Optional, New action arguments
*
* @returns {Object} - This queue
*/
Queue.prototype.next = function(args) {
if (this.queue.length) {
var action = this.queue.shift();
if (args === undefined) {
args = action.args;
}
// Push queue as last argument
if (typeof args === 'object') {
args.push(this);
}
action.fn.apply(this.context, args);
}
else {
if (args === undefined) {
args = [];
}
if (typeof args === 'object') {
// Push event name 'end' as first argument
args.unshift('end');
// Push queue object as last argument
args.push(this);
}
this.emit.apply(this, args);
}
return this;
};
/**
* Skip 'num' of next action(s)
*
* @param num {Number}
*
* @returns {Object} - This queue
*/
Queue.prototype.skip = function(num) {
if (num > 0) {
for (var i = 0; i < num; i++) {
if (this.queue.length) {
this.queue.shift();
}
}
}
return this;
};
/**
* Add pointer about started action
*
* @param name {String}
*/
Queue.prototype.started = function(name) {
if ( ! name) throw new Error('Name of action must be given');
(this.activeActions[name]) ? this.activeActions[name]++ : this.activeActions[name] = 1;
};
/**
* Remove pointer about finished action and emit "finish" event if no more active actions
*
* @param name {String}
*/
Queue.prototype.finished = function(name) {
if ( ! name) throw new Error('Name of action must be given');
(this.activeActions[name]) ? this.activeActions[name]-- : this.activeActions[name] = 0;
if (this.activeActions[name] <= 0) {
delete this.activeActions[name];
}
if (this.isAllFinished()) {
this.emit('finish');
}
};
/**
* Check for all finished actions
*
* @returns {Boolean}
*/
Queue.prototype.isAllFinished = function() {
for (var key in this.activeActions) {
if (this.activeActions.hasOwnProperty(key)) {
return false;
}
}
return true;
};
/**
* Custom emit method for custom context for event callbacks
*
* @param event {String}
*/
Queue.prototype.emit = function(event) {
// Check type of event argument
if (typeof event !== 'string') {
return;
}
// Remove event name from the function arguments list
delete arguments['0'];
// Create args array
var args = [];
// Pass all remaining arguments to args array
for (var i in arguments) {
if (arguments.hasOwnProperty(i)) {
args.push(arguments[i]);
}
}
// Check events registered
if ( ! this._events) {
return;
}
// Get callbacks for current event
var callbacks = this._events[event] || null;
// Check event callbacks
if ( ! callbacks) {
return;
}
// If just one callback registered normalize it to array of callbacks
if (typeof callbacks === 'function') {
callbacks = [ callbacks ];
}
// Call each callback with queue or custom context
for (var j = 0; j < callbacks.length; j++) {
callbacks[j].apply(this.context, args);
}
};
// Exports Queue constructor
module.exports = Queue;