Skip to content

Commit

Permalink
fix(webpage): improve logic to detect play timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Feb 6, 2024
1 parent 315961f commit 83791b5
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions apps/app/src/lib/remote-player/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,48 @@ export class WebiviewMediaProvider implements MediaProviderAdapter {
}

handlePlayReady() {
const finishLoad = new Promise<void>((_resolve, _reject) => {
const resolve = () => {
_resolve();
this.webview.removeEventListener("did-fail-load", reject);
};
const reject = (evt: Electron.DidFailLoadEvent) => {
_reject(new WebviewLoadError(evt));
this.webview.removeEventListener("did-finish-load", resolve);
};
this.webview.addEventListener("did-finish-load", resolve, {
once: true,
} as any);
this.webview.addEventListener("did-fail-load", reject, {
once: true,
} as any);
});
let timeoutId: number;
const timeout = (ms: number) =>
new Promise<void>((_, reject) => {
timeoutId = window.setTimeout(() => reject(new TimeoutError(ms)), ms);
});
const playReady = new Promise<void>((resolve) => {
const cancel = this._port.once("mx-play-ready", () => {
this._port.once("mx-play-ready", () => {
resolve();
window.clearTimeout(timeoutId);
});
const timeoutId = window.setTimeout(() => {
cancel();
resolve();
new Notice("Play ready timeout");
}, 10e3);
});
playReady.then(() => {
this.togglePlayReady(true);
});

finishLoad
.then(() => Promise.race([playReady, timeout(10e3)]))
.then(() => {
this.togglePlayReady(true);
})
.catch((err) => {
if (err instanceof TimeoutError) {
new Notice("Webview failed to load plugin in time");
} else if (err instanceof WebviewLoadError) {
new Notice("Webview failed to load website: " + err.message);
} else {
throw err;
}
});
}

onDomReady = async (evt: Event) => {
Expand Down Expand Up @@ -265,3 +293,9 @@ export class WebiviewMediaProvider implements MediaProviderAdapter {
// console.log("vidstack player loaded");
}
}

class WebviewLoadError extends Error {
constructor(evt: Electron.DidFailLoadEvent) {
super(`${evt.errorCode}: ${evt.errorDescription}`);
}
}

0 comments on commit 83791b5

Please sign in to comment.