Skip to content

Commit

Permalink
Fix Maps not supporting array based keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 22, 2024
1 parent c241ccd commit ae81446
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
9 changes: 4 additions & 5 deletions plugins/_lib/Caches/TrackInfoCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type TrackInfo = {
};
const WEEK = 7 * 24 * 60 * 60 * 1000;
export class TrackInfoCache {
private static readonly _listeners: Map<[TrackInfo["trackId"], AudioQuality], ((trackInfo: TrackInfo) => void)[]> = new Map();
private static readonly _listeners: Record<string, ((trackInfo: TrackInfo) => void)[]> = {};
private static readonly _store: SharedObjectStore<[TrackInfo["trackId"], TrackInfo["audioQuality"]], TrackInfo> = new SharedObjectStore("TrackInfoCache", {
keyPath: ["trackId", "audioQuality"],
});
Expand All @@ -34,16 +34,15 @@ export class TrackInfoCache {
}

public static async register(trackId: TrackInfo["trackId"], audioQuality: AudioQuality, onTrackInfo: (trackInfoP: TrackInfo) => void): Promise<void> {
const listeners = this._listeners.get([trackId, audioQuality]);
if (listeners !== undefined) listeners.push(onTrackInfo);
else this._listeners.set([trackId, audioQuality], [onTrackInfo]);
const key = `${trackId}${audioQuality}`;
if (this._listeners[key]?.push(onTrackInfo) === undefined) this._listeners[key] = [onTrackInfo];
const trackInfo = await this._store.getCache([trackId, audioQuality], tracer.err.withContext("register"));
if (trackInfo !== undefined) onTrackInfo(trackInfo);
}

private static put(trackInfo: TrackInfo): void {
this._store.putCache(trackInfo, [trackInfo.trackId, trackInfo.audioQuality], tracer.err.withContext("put"));
for (const listener of TrackInfoCache._listeners.get([trackInfo.trackId, trackInfo.audioQuality]) || []) listener(trackInfo);
for (const listener of TrackInfoCache._listeners[`${trackInfo.trackId}${trackInfo.audioQuality}`] ?? []) listener(trackInfo);
}

public static async ensure(playbackContext: PlaybackContext): Promise<TrackInfo> {
Expand Down
19 changes: 10 additions & 9 deletions plugins/_lib/sharedStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export class SharedObjectStore<K extends IDBValidKey, V> {
return this.db?.then((db) => db.close());
}

private readonly _memCache = new Map<K, V>();
private readonly _memCache: Record<string, V> = <Record<string, V>>{};
private safeKey(key: K): string {
if (typeof key === "string") return key;
return JSON.stringify(key);
}
constructor(private readonly storeName: string, private readonly storeSchema?: IDBObjectStoreParameters) {
SharedObjectStore.openDB(storeName, storeSchema);
}
Expand All @@ -49,16 +53,13 @@ export class SharedObjectStore<K extends IDBValidKey, V> {
delete(key: K) {
return SharedObjectStore.db.then((db) => db.delete(this.storeName, key));
}
getCache(query: K, errorHandler: (err?: Error) => void) {
getCache(query: K, errorHandler: (err?: Error) => void): V | Promise<V> {
const key = this.safeKey(query);
const value = SharedObjectStore.db
.then((db) => db.get(this.storeName, query))
.then((value) => {
this._memCache.set(query, value);
return value;
})
.then((value) => (this._memCache[key] = value))
.catch(errorHandler);
const memValue = this._memCache.get(query);
return memValue ?? value;
return this._memCache[key] ?? value;
}
get(query: K) {
return SharedObjectStore.db.then((db) => db.get(this.storeName, query));
Expand All @@ -73,7 +74,7 @@ export class SharedObjectStore<K extends IDBValidKey, V> {
return SharedObjectStore.db.then((db) => db.getKey(this.storeName, query));
}
putCache(value: V, key: K, errorHandler: (err?: Error) => void) {
this._memCache.set(key, value);
this._memCache[this.safeKey(key)] = value;
SharedObjectStore.db.then((db) => db.put(this.storeName, value, this.storeSchema?.keyPath !== undefined ? undefined : key)).catch(errorHandler);
}
put(value: V, key?: K) {
Expand Down

0 comments on commit ae81446

Please sign in to comment.