Skip to content

Commit

Permalink
RealMAX - Actually working
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 20, 2024
1 parent 7b34f74 commit 76f48b4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
12 changes: 8 additions & 4 deletions lib/Caches/ExtendedTrackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export class ExtendedTrackItem {
if (trackId === undefined) return undefined;
return this._cache[trackId] ?? (this._cache[trackId] = new this(trackId));
}
public async isrcs(): Promise<string[] | undefined> {
const trackItem = this.trackItem();
if (trackItem?.isrc !== undefined) return [trackItem.isrc];
public async isrcs(): Promise<Set<string> | undefined> {
let isrcs = [];

const recording = await this.recording();
if (recording?.isrcs !== undefined) return recording.isrcs;
if (recording?.isrcs) isrcs.push(...recording.isrcs);

const trackItem = this.trackItem();
if (trackItem?.isrc) isrcs.push(trackItem.isrc);

return new Set(isrcs);
}
public trackItem(): TrackItem | undefined {
if (this._trackItem !== undefined) return this._trackItem;
Expand Down
59 changes: 33 additions & 26 deletions plugins/RealMAX/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { ItemId, TrackItem } from "neptune-types/tidal";
import { TrackItemCache } from "../../../lib/Caches/TrackItemCache";
import { fetchIsrcIterable } from "../../../lib/tidalDevApi/isrc";
import { actions, intercept, store } from "@neptune";
import { PlaybackContext } from "../../../lib/AudioQualityTypes";
import { ExtendedTrackItem } from "../../../lib/Caches/ExtendedTrackItem";
import { Resource } from "../../../lib/tidalDevApi/types/ISRC";
import { interceptPromise } from "../../../lib/intercept/interceptPromise";
import { debounce } from "../../../lib/debounce";
import { messageInfo } from "../../../lib/messageLogging";

const hasHiRes = (trackItem: TrackItem) => {
const tags = trackItem.mediaMetadata?.tags;
Expand All @@ -15,23 +14,23 @@ const hasHiRes = (trackItem: TrackItem) => {
};

class MaxTrack {
private static readonly _idMap: Record<ItemId, Promise<Resource | undefined>> = {};
public static async fastCacheMaxId(itemId: ItemId): Promise<Resource | undefined> {
if (itemId === undefined) return undefined;
private static readonly _idMap: Record<ItemId, Promise<Resource | false>> = {};
public static async fastCacheMaxId(itemId: ItemId): Promise<Resource | false> {
if (itemId === undefined) return false;
return MaxTrack._idMap[itemId];
}
public static async getMaxId(itemId: ItemId | undefined): Promise<Resource | undefined> {
if (itemId === undefined) return undefined;
public static async getMaxId(itemId: ItemId | undefined): Promise<Resource | false> {
if (itemId === undefined) return false;

const idMapping = MaxTrack._idMap[itemId];
if (idMapping !== undefined) return idMapping;

const extTrackItem = await ExtendedTrackItem.get(itemId);
const trackItem = extTrackItem?.trackItem();
if (trackItem !== undefined && hasHiRes(trackItem)) return undefined;
if (trackItem !== undefined && hasHiRes(trackItem)) return false;

const isrcs = await extTrackItem?.isrcs();
if (isrcs === undefined) return (this._idMap[itemId] = Promise.resolve(undefined));
if (isrcs === undefined) return (this._idMap[itemId] = Promise.resolve(false));

return (this._idMap[itemId] = (async () => {
for (const isrc of isrcs) {
Expand All @@ -44,25 +43,33 @@ class MaxTrack {
}
}
}
return undefined;
return false;
})());
}
}

// export const onUnload = intercept(
// "playbackControls/TIME_UPDATE",
// debounce(async () => {
// const { elements, currentIndex } = store.getState().playQueue;
// const queueId = elements[currentIndex]?.mediaItemId;
export const onUnload = 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 maxItem = await MaxTrack.getMaxId(queueId);
// if (maxItem !== undefined) {
// actions.playQueue.clearActiveItems();
// await interceptPromise(() => actions.content.fetchAndPlayMediaItem({ itemId: maxItem?.id!, itemType: "track", sourceContext: { type: "user" } }), ["playbackControls/MEDIA_PRODUCT_TRANSITION"], []);
// const mediaItemIds = elements.slice(currentIndex + 1).map(({ mediaItemId }) => mediaItemId);
// actions.playQueue.addMediaItemsToQueue({ mediaItemIds, position: "next", options: { overwritePlayQueue: true }, sourceContext: { type: "user" } });
// }
// // Preload next
// await MaxTrack.getMaxId(elements[currentIndex + 1]?.mediaItemId);
// }, 125)
// );
const maxItem = await MaxTrack.getMaxId(queueId);
if (maxItem === false) return;
if (maxItem.id !== undefined && nextQueueId !== maxItem.id) {
if (TrackItemCache.get(maxItem.id) === undefined) {
// Force load
const currentPage = window.location.pathname;
actions.router.replace(<any>`/album/${maxItem.album!.id}`);
setTimeout(() => actions.router.replace(<any>currentPage), 50);
}
messageInfo(`Found Max quality for ${maxItem.title}! Adding to queue and skipping...`);
actions.playQueue.addNext({ mediaItemIds: [maxItem.id], context: { type: "user" } });
actions.playQueue.moveNext();
}
// Preload next two
MaxTrack.getMaxId(elements[currentIndex + 1]?.mediaItemId);
MaxTrack.getMaxId(elements[currentIndex + 2]?.mediaItemId);
}, 125)
);

0 comments on commit 76f48b4

Please sign in to comment.