Skip to content

Commit

Permalink
RealMAX - Attempt to improve resiliency
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jan 30, 2025
1 parent df5f2ba commit 63995c2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
79 changes: 63 additions & 16 deletions plugins/RealMAX/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,75 @@ import { MaxTrack } from "@inrixia/lib/MaxTrack";
import { ContextMenu } from "@inrixia/lib/ContextMenu";
import { AlbumCache } from "@inrixia/lib/Caches/AlbumCache";
import { settings } from "./Settings";
import type { PlayQueueItem } from "neptune-types/tidal";

export { Settings } from "./Settings";

const unloadIntercept = intercept(
"playbackControls/MEDIA_PRODUCT_TRANSITION",
debounce(async () => {
const { elements, currentIndex } = store.getState().playQueue;
const queueId = elements[currentIndex]?.mediaItemId;
const nextQueueId = elements[currentIndex + 1]?.mediaItemId;
const maxQueueItem = async (elements: readonly PlayQueueItem[], currentIndex: number, jumpTo?: number) => {
jumpTo ??= currentIndex;
const newElements = [...elements];
const currentId = newElements[currentIndex].mediaItemId;
const maxItem = await MaxTrack.getMaxTrack(currentId);
MaxTrack.getMaxTrack(newElements[currentIndex + 1].mediaItemId);
if (maxItem !== false && maxItem.id !== undefined) {
newElements[currentIndex].mediaItemId = maxItem.id;
actions.playQueue.reset({
elements: newElements,
currentIndex: jumpTo,
});
return true;
}
return false;
};

const maxItem = await MaxTrack.getMaxTrack(queueId);
if (maxItem === false) return;
if (maxItem.id !== undefined && nextQueueId !== maxItem.id) {
if (settings.displayInfoPopups) trace.msg.log(`Found Max quality for ${maxItem.title}! Adding to queue and skipping...`);
actions.playQueue.addNext({ mediaItemIds: [maxItem.id], context: { type: "user" } });
const unloadTransition = intercept("playbackControls/MEDIA_PRODUCT_TRANSITION", ([{ mediaProduct }]) => {
actions.playbackControls.pause();
(async () => {
const productId: string = (<any>mediaProduct).productId;
const maxItem = await MaxTrack.getMaxTrack(productId);
if (maxItem !== false && maxItem.id !== undefined) {
actions.playQueue.addNext({ mediaItemIds: [maxItem.id], context: { type: "UNKNOWN" } });
actions.playQueue.moveNext();
}
// Preload next two
MaxTrack.getMaxTrack(elements[currentIndex + 1]?.mediaItemId);
MaxTrack.getMaxTrack(elements[currentIndex + 2]?.mediaItemId);
}, 125)
);
actions.playbackControls.play();
})();
});

const unloadAddNow = intercept("playQueue/ADD_NOW", ([payload]) => {
(async () => {
const mediaItemIds = [...payload.mediaItemIds];
const currentIndex = payload.fromIndex ?? 0;
const maxItem = await MaxTrack.getMaxTrack(mediaItemIds[currentIndex]);
if (maxItem !== false && maxItem.id !== undefined) mediaItemIds[currentIndex] = maxItem.id;
actions.playQueue.addNow({ ...payload, mediaItemIds });
})();
return true;
});

const unloadSkip = intercept(["playQueue/MOVE_TO", "playQueue/MOVE_NEXT", "playQueue/MOVE_PREVIOUS"], ([payload, action]) => {
(async () => {
const { elements, currentIndex } = store.getState().playQueue;
switch (action) {
case "playQueue/MOVE_NEXT":
if ((await maxQueueItem(elements, currentIndex + 1)) === false) actions.playQueue.moveNext();
break;
case "playQueue/MOVE_PREVIOUS":
if ((await maxQueueItem(elements, currentIndex - 1)) === false) actions.playQueue.movePrevious();
break;
case "playQueue/MOVE_TO":
if ((await maxQueueItem(elements, payload ?? currentIndex)) === false) actions.playQueue.moveTo(payload ?? currentIndex);
break;
}
actions.playbackControls.play();
})();
return true;
});

const unloadIntercept = () => {
unloadTransition();
unloadAddNow();
unloadSkip();
};

ContextMenu.onOpen(async (contextSource, contextMenu, trackItems) => {
document.getElementById("realMax-button")?.remove();
Expand Down
4 changes: 3 additions & 1 deletion plugins/_lib/Caches/AsyncCachable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export const AsyncCachable = <K extends string | number | symbol, V>(generator: (key: K) => Promise<V>) => {
const _cache: Record<K, Promise<V>> = <any>{};
return (key: K): Promise<V> => {
const _func = (key: K): Promise<V> => {
if (key in _cache) return _cache[key];
return (_cache[key] = generator(key));
};
_func._cache = _cache;
return _func;
};
2 changes: 1 addition & 1 deletion plugins/_lib/MaxTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class MaxTrack {
if (track.type !== "tracks") continue;
if (filter && !filter(track)) continue;
const trackItem = await MediaItemCache.ensureTrack(track.id);
if (trackItem !== undefined) yield trackItem;
if (trackItem?.id !== undefined) yield trackItem;
}
}
public static hasHiRes(trackItem: TrackItem): boolean {
Expand Down
2 changes: 1 addition & 1 deletion plugins/_lib/api/tidal/isrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export async function* fetchIsrcIterable(isrc: string): AsyncIterable<TApiTrack>
const resp: TApiTracks = await fetchTidal<TApiTracks>(next);
if (resp?.data === undefined || resp.data.length === 0) break;
yield* resp.data;
next = `${baseURL}${resp.links.next}`;
next = resp.links.next === undefined ? undefined : `${baseURL}${resp.links.next}`;
}
}

0 comments on commit 63995c2

Please sign in to comment.