-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnameToMBIDCache.js
More file actions
53 lines (46 loc) · 1.59 KB
/
Copy pathnameToMBIDCache.js
File metadata and controls
53 lines (46 loc) · 1.59 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
import { SimpleCache } from '@kellnerd/es-utils/cache/SimpleCache.js';
/** @type {SimpleCache<[entityType: CoreEntityTypeT, name: string], MB.MBID>} */
export const nameToMBIDCache = new SimpleCache({
name: 'nameToMBIDCache',
storage: window.localStorage,
});
/**
* Loads the MBIDs of cached entity names for the given release seed.
* @param {MB.ReleaseSeed} release
* @returns Name, type and MBID (if already given or found in the cache) of the related entities.
*/
export async function loadCachedEntitiesForRelease(release) {
return Promise.all([
...loadCachedArtists(release.artist_credit),
...loadCachedLabels(release),
...release.mediums?.flatMap(
(medium) => medium.track?.flatMap(
(track) => loadCachedArtists(track.artist_credit)
) ?? []
) ?? [],
]).then((entities) => entities.filter((entity) => entity));
}
/** @param {MB.ArtistCreditSeed} artistCredit */
function loadCachedArtists(artistCredit) {
return artistCredit?.names.map((credit) => loadCachedMBID(credit, 'artist', credit.artist?.name ?? credit.name)) ?? [];
}
/** @param {MB.ReleaseSeed} release */
function loadCachedLabels(release) {
return release.labels?.map((label) => loadCachedMBID(label, 'label', label.name)) ?? [];
}
/**
* @param {{ mbid: MB.MBID }} entity
* @param {CoreEntityTypeT} type
* @param {string} name
* @returns Type and name of the entity if it was not found in the cache.
*/
async function loadCachedMBID(entity, type, name) {
let mbid = entity.mbid;
if (!mbid) {
mbid = await nameToMBIDCache.get(type, name);
if (mbid) {
entity.mbid = mbid;
}
}
return { type, name, mbid };
}