diff --git a/plugins/TidalTags/src/index.ts b/plugins/TidalTags/src/index.ts index 9f6d68c..0a73886 100644 --- a/plugins/TidalTags/src/index.ts +++ b/plugins/TidalTags/src/index.ts @@ -1,4 +1,4 @@ -import { intercept, store, actions } from "@neptune"; +import { intercept, store } from "@neptune"; import { setFLACInfo } from "./setFLACInfo"; diff --git a/plugins/TidalTags/src/setFLACInfo.ts b/plugins/TidalTags/src/setFLACInfo.ts index 19bc6f1..86d4252 100644 --- a/plugins/TidalTags/src/setFLACInfo.ts +++ b/plugins/TidalTags/src/setFLACInfo.ts @@ -96,11 +96,13 @@ export const setFLACInfo = async ([{ playbackContext }]: [{ playbackContext?: Pl } try { - const { bitDepth, sampleRate, bitrate } = await TrackInfoCache.ensure(playbackContext); - flacInfoElem.textContent = ""; - if (!!sampleRate) flacInfoElem.textContent += `${sampleRate / 1000}kHz `; - if (!!bitDepth) flacInfoElem.textContent += `${bitDepth}bit `; - if (!!bitrate) flacInfoElem.textContent += `${Math.floor(bitrate / 1000).toLocaleString()}kb/s`; + await TrackInfoCache.ensure(playbackContext); + await TrackInfoCache.register(playbackContext.actualProductId, playbackContext.actualAudioQuality, ({ sampleRate, bitDepth, bitrate }) => { + flacInfoElem.textContent = ""; + if (!!sampleRate) flacInfoElem.textContent += `${sampleRate / 1000}kHz `; + if (!!bitDepth) flacInfoElem.textContent += `${bitDepth}bit `; + if (!!bitrate) flacInfoElem.textContent += `${Math.floor(bitrate / 1000).toLocaleString()}kb/s`; + }); } catch (err) { flacInfoElem.style.maxWidth = "256px"; flacInfoElem.style.border = "solid 1px red"; diff --git a/plugins/_lib/Caches/TrackInfoCache.ts b/plugins/_lib/Caches/TrackInfoCache.ts index 9628752..0945a68 100644 --- a/plugins/_lib/Caches/TrackInfoCache.ts +++ b/plugins/_lib/Caches/TrackInfoCache.ts @@ -29,17 +29,18 @@ export class TrackInfoCache { } public static async ensure(playbackContext: PlaybackContext): Promise { - let { actualProductId: trackId, actualAudioQuality: audioQuality, bitDepth, sampleRate, codec, actualDuration: duration } = playbackContext; + let { actualProductId: trackId, actualAudioQuality } = playbackContext; // If a promise for this key is already in the cache, await it - const { expired, value: trackInfo } = await this._store.getWithExpiry([trackId, audioQuality]); + const { expired, value: trackInfo } = await this._store.getWithExpiry([trackId, actualAudioQuality]); - if (expired === true) this.update(playbackContext); + this.update(playbackContext); if (trackInfo === undefined) return this.update(playbackContext); return trackInfo; } private static async update(playbackContext: PlaybackContext): Promise { - const trackInfo = await getTrackInfo(playbackContext, await PlaybackInfoCache.ensure(+playbackContext.actualProductId, playbackContext.actualAudioQuality)); + const extPlaybackInfo = await PlaybackInfoCache.ensure(+playbackContext.actualProductId, playbackContext.actualAudioQuality); + const trackInfo = await getTrackInfo(playbackContext, extPlaybackInfo); this.put(trackInfo); return trackInfo; } diff --git a/plugins/_lib/nativeBridge/index.ts b/plugins/_lib/nativeBridge/index.ts index 652344f..90cf36d 100644 --- a/plugins/_lib/nativeBridge/index.ts +++ b/plugins/_lib/nativeBridge/index.ts @@ -1,6 +1,8 @@ -import type * as nativeBridge from "./native"; +import type * as nb from "./native"; import "./nativeBridge.native"; +export type * from "./native"; + type UnsafeReturnType = T extends (...args: any[]) => infer R ? R : any; type UnsafeParameters = T extends (...args: infer P) => any ? P : never; type PromisefulModule = { @@ -10,17 +12,15 @@ type PromisefulModule = { : (...args: UnsafeParameters) => Promise> : () => Promise; }; - -const invoke: (method: string, ...args: any[]) => Promise = (window).electron.ipcRenderer.invoke; -module.exports = new Proxy(>{}, { - get: - (_, key: string, __) => - (...args: any[]) => - invoke("___nativeBridge___", key, ...args).catch((err: Error) => { - err.message = err.message.replaceAll("Error invoking remote method '___nativeBridge___': ", ""); - throw err; - }), - set: () => { - throw new Error("You cannot set properties of nativeBridge"); - }, -}); +type NativeBridge = PromisefulModule; +const _invoke: (method: string, ...args: any[]) => Promise = (window).electron.ipcRenderer.invoke; +const invoke = + (method: K) => + (...args: Parameters): ReturnType => + _invoke("___nativeBridge___", method, ...args).catch((err: Error) => { + err.message = err.message.replaceAll("Error invoking remote method '___nativeBridge___': ", ""); + throw err; + }); +export const getTrackInfo = invoke("getTrackInfo"); +export const parseDasha = invoke("parseDasha"); +export const requestJson = invoke("requestJson"); diff --git a/plugins/_lib/nativeBridge/nativeBridge.d.ts b/plugins/_lib/nativeBridge/nativeBridge.d.ts deleted file mode 100644 index 4a2769a..0000000 --- a/plugins/_lib/nativeBridge/nativeBridge.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import * as nativeBridge from "./native"; - -// Augment the module with the inferred types -declare module "." { - export = nativeBridge; -}