-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (54 loc) · 1.25 KB
/
Copy pathindex.js
File metadata and controls
54 lines (54 loc) · 1.25 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
class Queue {
/**
* @param {Function[]} methods Methods to add to queue.
*/
constructor(methods) {
this._queuedMethods = methods || []
this._processedMethods = 0
}
/**
* Run next method on queue.
* @param {} args Arguments to pass to method.
*/
next(...args) {
this._processedMethods++
return this._queuedMethods[this._processedMethods - 1](...args)
}
/**
* Run previous method in queue.
* @param {} args Arguments to pass to method.
*/
prev(...args) {
this._processedMethods--
return this._queuedMethods[this._processedMethods - 1](...args)
}
/**
* Run specific method.
* @param {Number} index The index of the method to call.
* @param {} args Arguments to pass to method.
*/
jump(index, ...args) {
return this._queuedMethods[index](...args)
}
/**
* Add methods to queue.
* @param {Function[]} methods
*/
add(methods) {
this._queuedMethods.push(...methods)
}
/**
* Remove methods from queue.
* @param {Number[]|Number} index
*/
remove(...index) {
if (typeof index == "array") {
index.forEach(i => {
this._queuedMethods.splice(i, 1)
})
} else {
this._queuedMethods.splice(index[0], 1)
}
}
}
module.exports = Queue