Skip to content

Commit

Permalink
refactor(wrapper): use update event for generating Player events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SunsetTechuila authored Dec 10, 2023
1 parent 45bc8a0 commit 174e5c6
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,31 +685,22 @@ window.Spicetify = {
return;
}

function objectsAreEqual(obj1, obj2) {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
const interval = setInterval(() => {
if (!Spicetify.Player.origin._state) return;
Spicetify.Player.data = Spicetify.Player.origin._state;
clearInterval(interval);
}, 10);

const playerState = {
cache: Spicetify.Player.data,
current: null
};

setInterval(() => {
if (
objectsAreEqual(Spicetify.Platform.PlayerAPI._state, playerState.cache) ||
(!Spicetify.Platform.PlayerAPI._state.item && !Spicetify.Player.data)
)
return;

if (!Spicetify.Platform.PlayerAPI._state.item) {
Spicetify.Player.data = null;
return;
}

playerState.current = Spicetify.Platform.PlayerAPI._state;
Spicetify.Player.origin._events.addListener("update", ({ data: playerEventData }) => {
playerState.current = playerEventData;
Spicetify.Player.data = playerState.current;

if (playerState.cache?.item.uri !== playerState.current?.item?.uri) {
if (playerState.cache?.item?.uri !== playerState.current?.item?.uri) {
const event = new Event("songchange");
event.data = Spicetify.Player.data;
Spicetify.Player.dispatchEvent(event);
Expand All @@ -722,12 +713,21 @@ window.Spicetify = {
}

playerState.cache = playerState.current;
}, 100);
});

Spicetify.Player.origin._events.addListener("error", ({ data: error }) => {
if (error.code === "all_tracks_unplayable_auto_stopped") {
Spicetify.Player.data = null;
playerState.cache = null;
}
});

setInterval(() => {
const event = new Event("onprogress");
event.data = Spicetify.Player.getProgress();
Spicetify.Player.dispatchEvent(event);
if (playerState.cache?.isPaused === false) {
const event = new Event("onprogress");
event.data = Spicetify.Player.getProgress();
Spicetify.Player.dispatchEvent(event);
}
}, 100);

Spicetify.addToQueue = uri => {
Expand Down

0 comments on commit 174e5c6

Please sign in to comment.