Skip to content

Commit

Permalink
NoBuffer - Fix STALL handling on first load
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 24, 2024
1 parent abc0b52 commit 5b19811
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions plugins/LastFM/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { debounce } from "@inrixia/lib/debounce";
import safeUnload from "@inrixia/lib/safeUnload";

import { settings } from "./Settings";
import currentPlaybackContext from "@inrixia/lib/currentPlaybackContext";
import getPlaybackControl from "@inrixia/lib/getPlaybackControl";
export { Settings } from "./Settings";

let totalPlayTime = 0;
Expand Down Expand Up @@ -131,7 +131,7 @@ type CurrentTrack = {
const getCurrentTrack = async (playbackContext?: PlaybackContext): Promise<CurrentTrack> => {
const playbackStart = Date.now();

playbackContext = currentPlaybackContext();
playbackContext ??= getPlaybackControl()?.playbackContext;
if (playbackContext === undefined) throw new Error("PlaybackContext is undefined");
const extTrackItem = await ExtendedTrackItem.current(playbackContext);
if (extTrackItem === undefined) throw new Error("Failed to get extTrackItem");
Expand Down
8 changes: 4 additions & 4 deletions plugins/NoBuffer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { intercept } from "@neptune";
import { intercept, store } from "@neptune";
import { TrackItemCache } from "@inrixia/lib/Caches/TrackItemCache";

import { fetchTrack } from "@inrixia/lib/trackBytes/download";
Expand All @@ -7,15 +7,15 @@ import * as stream from "stream";
const { Writable } = <typeof stream>require("stream");

import { Tracer } from "@inrixia/lib/trace";
import currentPlaybackContext from "@inrixia/lib/currentPlaybackContext";
import getPlaybackControl from "@inrixia/lib/getPlaybackControl";
const trace = Tracer("[NoBuffer]");

let unblocking = false;
export const onUnload = intercept("playbackControls/SET_PLAYBACK_STATE", ([state]) => {
if (state === "STALLED" && unblocking === false) {
const { playbackContext, latestCurrentTime } = getPlaybackControl();
if (latestCurrentTime !== 0 && state === "STALLED" && unblocking === false) {
unblocking = true;
(async () => {
const playbackContext = currentPlaybackContext();
if (playbackContext === undefined) return;
const trackItem = await TrackItemCache.current(playbackContext);
if (trackItem === undefined) return;
Expand Down
4 changes: 2 additions & 2 deletions plugins/_lib/Caches/ExtendedTrackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TrackItemCache } from "./TrackItemCache";
import { AlbumCache } from "./AlbumCache";
import { libTrace } from "../trace";
import { store } from "@neptune";
import currentPlaybackContext from "../currentPlaybackContext";
import getPlaybackControl from "../getPlaybackControl";

export class ExtendedTrackItem {
private _album?: Album;
Expand All @@ -18,7 +18,7 @@ export class ExtendedTrackItem {
private constructor(public readonly trackId: ItemId, public readonly trackItem: TrackItem) {}

public static current(playbackContext?: PlaybackContext) {
playbackContext ??= currentPlaybackContext();
playbackContext ??= getPlaybackControl()?.playbackContext;
if (playbackContext?.actualProductId === undefined) return undefined;
return this.get(playbackContext.actualProductId);
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/_lib/Caches/TrackItemCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { store } from "@neptune";
import type { TrackItem, MediaItem, ItemId } from "neptune-types/tidal";
import { interceptPromise } from "../intercept/interceptPromise";
import type { PlaybackContext } from "../AudioQualityTypes";
import currentPlaybackContext from "../currentPlaybackContext";
import getPlaybackControl from "../getPlaybackControl";

export class TrackItemCache {
private static readonly _cache: Record<ItemId, TrackItem> = {};
public static current(playbackContext?: PlaybackContext) {
playbackContext ??= currentPlaybackContext();
playbackContext ??= getPlaybackControl()?.playbackContext;
if (playbackContext?.actualProductId === undefined) return undefined;
return this.ensure(playbackContext.actualProductId);
}
Expand Down
4 changes: 0 additions & 4 deletions plugins/_lib/currentPlaybackContext.ts

This file was deleted.

6 changes: 6 additions & 0 deletions plugins/_lib/getPlaybackControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { store } from "@neptune";
import { PlaybackContext } from "./AudioQualityTypes";
import type { CoreState } from "neptune-types/tidal";

type PlaybackControl = CoreState["playbackControls"] & { playbackContext: PlaybackContext };
export default (): Partial<PlaybackControl> => <PlaybackControl>store.getState()?.playbackControls ?? {};

0 comments on commit 5b19811

Please sign in to comment.