Skip to content

Commit

Permalink
fix: getCurrentTrack returns wrong data
Browse files Browse the repository at this point in the history
  • Loading branch information
N0chteil committed Oct 22, 2023
1 parent 99b0117 commit 4452918
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/managers/watchdog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ export class WatchDog {
if (data.type === "res") return;

if (data.playerState === "playing") {
const durationMS = data.duration * 1000,
startTime = data.endTime - durationMS,
currentTime = Date.now(),
elapsedTime = (currentTime - startTime) / 1000,
remainingTime = (durationMS - elapsedTime * 1000) / 1000;
const { elapsedTime, remainingTime } = this.getTimeData(
data.duration,
data.endTime
);

this.emitter.emit("playing", {
name: data.title || "",
Expand Down Expand Up @@ -176,9 +175,9 @@ export class WatchDog {
this.socket.send(message);
}

public getCurrentTrack(): Promise<WatchDogData> {
public getCurrentTrack(): Promise<currentTrack> {
return new Promise((resolve, reject) => {
if (!this.isConnected()) return resolve({} as WatchDogData);
if (!this.isConnected()) return resolve({} as currentTrack);

const gThis = this;

Expand All @@ -200,12 +199,37 @@ export class WatchDog {
return;
}

resolve(data);
const { elapsedTime, remainingTime } = gThis.getTimeData(
data.duration,
data.endTime
);

resolve({
name: data.title || "",
artist: data.artist || "",
album: data.album || "",
duration: data.duration || 0,
elapsedTime,
remainingTime,
endTime: data.endTime,
playerState: data.playerState
} as currentTrack);
gThis.socket.removeEventListener("message", onMessage);
}
});
}

private getTimeData(duration: number, endTime: number) {
const durationMS = duration * 1000,
elapsedTime = (Date.now() - (endTime - durationMS)) / 1000,
remainingTime = (durationMS - elapsedTime * 1000) / 1000;

return {
elapsedTime,
remainingTime
};
}

public on(
event: currentTrack["playerState"],
listener: (currentTrack: currentTrack) => void
Expand Down

0 comments on commit 4452918

Please sign in to comment.