-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelayedQueueService.js
More file actions
118 lines (103 loc) · 2.79 KB
/
delayedQueueService.js
File metadata and controls
118 lines (103 loc) · 2.79 KB
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
/**
* Implementation of simple delayed queue.
* Note that you should create an instance of the queue to use it.
*
* The queue accepts a delay for each item and returns a promise.
* It also makes sure no item from the same queue is being executed in the same time.
*
* Instance init options object:
* isAutoExecutable - (optional, default: false) if the queue will start executing once an item is added
* defaultDelay - (optional) Default delay for all items, can be overwritten for a specific item
*
* Usage example A:
* ===============
* var queue = new Queue({ isAutoExecutable: true });
*
* queue.enqueue().then(function() {
* console.log("foo");
* });
*
* queue.enqueue().then(function() {
* console.log("bar");
* });
*
* Usage example B:
* ===============
* var config = {
* defaultDelay: 500
* };
*
* var queue = new Queue();
*
* queue.enqueue(1000).then(function() {
* console.log("foo");
* queue.executeNext();
* });
*
* queue.enqueue().then(function() {
* console.log("bar");
* queue.executeNext();
* });
*
* queue.executeNext();
*
*/
angular.module("app").factory("queueService", [
"$q",
"$timeout",
function($q, $timeout) {
var Queue = function(options) {
/** will hold the elements in the queue */
this.list = [];
/** Flag to indicate if the queue is executing an item */
this.isExecuting = false;
/** Flag to indicate if the queue will start executing once an item is added */
this.isAutoExecutable = (options && options.isAutoExecutable) || false;
/** Default delay for all items, can be overwritten for a specific item */
this.defaultDelay = (options && typeof options.defaultDelay !== "undefined")
? options.defaultDelay
: 0;
};
/**
* Add an element to queue
* @param delay - time in ms to wait before execute next item in the queue
* @return promise
*/
Queue.prototype.enqueue = function(delay) {
var deferred = $q.defer();
this.list.push({
deferred: deferred,
delay: this.delay >= 0 ? delay : this.defaultDelay
});
this.isAutoExecutable && this.executeNext();
return deferred.promise;
};
/**
* Executes the next item in the queue
*/
Queue.prototype.executeNext = function() {
if (this.isExecuting || this.size() === 0) return;
this.isExecuting = true;
var item = this.dequeue();
$timeout(function() {
item.deferred.resolve();
this.isExecuting = false;
this.isAutoExecutable && this.executeNext();
}.bind(this), item.delay);
};
/**
* removes an element from queue
* @returns the element that was removed
*/
Queue.prototype.dequeue = function() {
return this.list.shift();
};
/**
* Returns the size of the queue
* @returns size of the queue
*/
Queue.prototype.size = function() {
return this.list.length;
};
return Queue;
}]);