fix: check existence of requestIdleCallback before uses (#643)

This commit is contained in:
Myriad-Dreamin 2024-10-07 14:18:45 +08:00 committed by GitHub
parent 24751130a5
commit 3aa8f1233d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View file

@ -159,7 +159,7 @@ export function provideCanvasDoc<
this.kModule.pixelPerPt = this.pixelPerPt;
const waitABit = async () => {
return new Promise((resolve) => {
if (opts?.lazy) {
if (opts?.lazy && "requestIdleCallback" in window) {
requestIdleCallback(() => resolve(undefined), { timeout: 100 });
} else {
resolve(undefined);

View file

@ -620,7 +620,8 @@ export function provideSvgDoc<
);
const tok = (this.canvasRenderCToken = new TypstCancellationToken());
requestIdleCallback(
renderCanvasWhenIdle(
async () => {
await waitCancel;
this.updateCanvas(pagesInCanvasMode, {
@ -710,3 +711,8 @@ export function provideSvgDoc<
}
};
}
const renderCanvasWhenIdle =
"requestIdleCallback" in window
? requestIdleCallback
: (cb: (args: void) => void, { timeout }: any) => setTimeout(cb, timeout);