Skip to content

Commit

Permalink
TidalTags - Native Working
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 25, 2024
1 parent b005f1c commit 1037060
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion plugins/TidalTags/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intercept, store, actions } from "@neptune";
import { intercept, store } from "@neptune";

import { setFLACInfo } from "./setFLACInfo";

Expand Down
12 changes: 7 additions & 5 deletions plugins/TidalTags/src/setFLACInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 5 additions & 4 deletions plugins/_lib/Caches/TrackInfoCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ export class TrackInfoCache {
}

public static async ensure(playbackContext: PlaybackContext): Promise<TrackInfo> {
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<TrackInfo> {
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;
}
Expand Down
30 changes: 15 additions & 15 deletions plugins/_lib/nativeBridge/index.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends (...args: any[]) => infer R ? R : any;
type UnsafeParameters<T> = T extends (...args: infer P) => any ? P : never;
type PromisefulModule<M> = {
Expand All @@ -10,17 +12,15 @@ type PromisefulModule<M> = {
: (...args: UnsafeParameters<M[K]>) => Promise<UnsafeReturnType<M[K]>>
: () => Promise<M[K]>;
};

const invoke: (method: string, ...args: any[]) => Promise<any> = (<any>window).electron.ipcRenderer.invoke;
module.exports = new Proxy(<PromisefulModule<typeof nativeBridge>>{}, {
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<typeof nb>;
const _invoke: (method: string, ...args: any[]) => Promise<any> = (<any>window).electron.ipcRenderer.invoke;
const invoke =
<K extends keyof NativeBridge>(method: K) =>
(...args: Parameters<NativeBridge[K]>): ReturnType<NativeBridge[K]> =>
<any>_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");
6 changes: 0 additions & 6 deletions plugins/_lib/nativeBridge/nativeBridge.d.ts

This file was deleted.

0 comments on commit 1037060

Please sign in to comment.