-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecentlyPlayed.js
54 lines (40 loc) · 1.06 KB
/
RecentlyPlayed.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
window.foo = (l, art, max=5) => {
const newLastPlayed = l.reverse().filter(x => x != art);
if (art != null) {
newLastPlayed.push(art);
}
while (newLastPlayed.length > max) {
newLastPlayed.shift();
}
return newLastPlayed.reverse();
}
export class RecentlyPlayed {
constructor(storage, maxEntries) {
this.storage = storage;
this.maxEntries = maxEntries;
}
setRecentlyPlayedCount(cnt) {
console.log(`Will remember ${cnt} recently played entries`);
this.maxEntries = cnt;
// Trigger rebuild to remove entries over limit
this.add(null);
}
_get() {
var lastPlayed = this.storage.get('lastPlayed', []);
if (!Array.isArray(lastPlayed)) return [];
return lastPlayed;
}
get() {
return this._get().reverse();
}
add(art) {
const newLastPlayed = this._get().filter(x => x != art);
if (art != null) {
newLastPlayed.push(art);
}
while (newLastPlayed.length > this.maxEntries) {
newLastPlayed.shift();
}
this.storage.save('lastPlayed', newLastPlayed);
}
};