-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.js
85 lines (74 loc) · 1.41 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
const shoukaku = require('shoukaku');
/**
* @extends Array<shoukaku.Track>
*/
class Queue extends Array {
/**
* Get the queue size
* @returns {number}
*/
get size() {
return this.length;
}
/**
* Get the queue total size (plus the current track)
* @returns {number}
*/
get totalSize() {
return this.length + (this.current ? 1 : 0);
}
/** @type {shoukaku.Track|null|undefined} */
current = null;
/** @type {shoukaku.Track|null|undefined} */
previous = null;
/**
* Check if the queue is empty
* @returns {boolean}
*/
get isEmpty() {
return this.length === 0;
}
/**
* Get the queue duration
* @returns {number}
*/
get durationLength() {
return this.reduce((acc, cur) => acc + (cur.length || 0), 0);
}
/**
* Add a track to the queue
* @param {shoukaku.Track} track
* @returns {Queue}
*/
add(track, options) {
track.info.requester = options?.requester || null;
this.push(track);
return this;
}
/**
* Remove a track from the queue
* @param {number} index
* @returns {Queue}
*/
remove(index) {
return this.splice(index, 1)[0];
}
/**
* Clear the queue
* @returns {Queue}
*/
clear() {
return this.splice(0);
}
/**
* Randomize the queue
* @returns {void}
*/
shuffle() {
for (let i = this.length - 1; i > 0; i -= 1) {
const j = Math.floor(Math.random() * (i + 1));
[this[i], this[j]] = [this[j], this[i]];
}
}
}
module.exports = Queue;