-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectionManager.js
74 lines (63 loc) · 2.43 KB
/
CollectionManager.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
import { GlobalUI } from './UiGlobal.js';
import { groupAndIndexGenres, getInterestingAttrsFromSpotifyArtistList } from './GenreClassifier.js';
export class CollectionManager {
FOLLOWED_ARTISTS_STORAGE_KEY = "FOLLOWED_ARTISTS_STORAGE_KEY";
constructor(storage, spotify) {
this.storage = storage;
this.spotify = spotify;
this.ready = $.Deferred();
GlobalUI.notifyNewRequestOnFlight();
this.artistIndex = null;
this.genresIndex = null;
const cache = this.storage.get(this.FOLLOWED_ARTISTS_STORAGE_KEY);
if (cache && cache.artistIndex) {
try {
this.artistIndex = new Map(JSON.parse(cache.artistIndex));
} catch (e) {
console.log("Cached artist index isn't valid");
}
}
if (cache && cache.genresIndex) {
try {
this.genresIndex = new Map(JSON.parse(cache.genresIndex));
} catch (e) {
console.log("Cached genre index isn't valid");
}
}
let valid_cache = true;
valid_cache &= this.genresIndex && this.genresIndex.size > 0;
valid_cache &= this.artistIndex && this.artistIndex.size > 0;
if (valid_cache) {
console.log("Got collection from cache");
GlobalUI.notifyRequestFinished();
this.ready.resolve();
} else {
console.log("Collection from cache is not valid, reload collection");
this._refreshFollowedArtists(this.ready);
}
}
refreshFollowedArtists() {
const ready = $.Deferred();
this._refreshFollowedArtists(ready);
return ready;
}
_refreshFollowedArtists(promise) {
console.log("Refreshing collection from Spotify");
this.spotify.ready.then(() => {
this.spotify.fetchFollowedArtists().then(lst => {
console.log("Retrieved full followed artist list, got", lst.length, "artists");
const raw_arts = getInterestingAttrsFromSpotifyArtistList(lst);
console.log("Transmogrifying artist list, have", raw_arts.length, "artists");
const arts = groupAndIndexGenres(raw_arts);
this.genresIndex = arts.genresIndex;
this.artistIndex = arts.artistIndex;
const serializedArts = arts;
serializedArts.artistIndex = JSON.stringify(Array.from(arts.artistIndex.entries()));
serializedArts.genresIndex = JSON.stringify(Array.from(arts.genresIndex.entries()));
this.storage.save(this.FOLLOWED_ARTISTS_STORAGE_KEY, serializedArts);
GlobalUI.notifyRequestFinished();
promise.resolve();
});
});
}
}