jadefin/mods/PatchFixSyncPlay.js

79 lines
2.6 KiB
JavaScript

//@ts-check
import JadefinIntegrity from '../JadefinIntegrity.js';
import Jadefin from "../Jadefin.js";
import JadefinMod from "../JadefinMod.js";
import JadefinModules from "../JadefinModules.js";
import JadefinUtils from "../JadefinUtils.js";
// Mainly spawned from https://github.com/jellyfin/jellyfin-web/issues/5485
export default JadefinIntegrity("PatchFixSyncPlay", import.meta.url, () => new (class PatchFixSyncPlay extends JadefinMod {
constructor() {
super();
this._scheduleMightSkip = 0;
this._scheduleSkip = 0;
}
async init(name, url) {
const self = this;
await super.init(name, url);
await JadefinUtils.waitUntil(() => JadefinModules.syncPlay);
const queueCore = JadefinModules.syncPlay.Manager.queueCore;
const startPlayback = this._startPlayback = queueCore.startPlayback.bind(queueCore);
queueCore.startPlayback = function(apiClient) {
// scheduleReadyRequestOnPlaybackStart must occur BEFORE playerWrapper.localPlay
if (this.manager.isFollowingGroupPlayback() && !this.isPlaylistEmpty()) {
this.scheduleReadyRequestOnPlaybackStart(apiClient, "startPlayback");
self._scheduleMightSkip++;
self.hookPlayerWrapper(this.manager.getPlayerWrapper());
}
return startPlayback(apiClient);
};
const scheduleReadyRequestOnPlaybackStart = this._scheduleReadyRequestOnPlaybackStart = queueCore.scheduleReadyRequestOnPlaybackStart.bind(queueCore);
queueCore.scheduleReadyRequestOnPlaybackStart = function(apiClient, origin) {
if (origin == "startPlayback" && self._scheduleSkip > 0) {
self._scheduleSkip--;
return;
}
return scheduleReadyRequestOnPlaybackStart(apiClient, origin);
};
}
/**
* @param {any} playerWrapper
*/
hookPlayerWrapper(playerWrapper) {
if (playerWrapper._PatchFixSyncPlay_localPlay) {
return;
}
const localPlay = playerWrapper._PatchFixSyncPlay_localPlay = playerWrapper.localPlay.bind(playerWrapper);
playerWrapper.localPlay = (options) => {
const skip = this._scheduleMightSkip > 0;
if (skip) {
this._scheduleMightSkip--;
this._scheduleSkip++;
}
const rv = localPlay(options);
rv.catch(() => {
if (skip) {
this._scheduleSkip--;
}
});
return rv;
};
}
})());