Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use requestIdleCallback to wait for updating canvas pages when editor is in idle #412

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ undocumented_unsafe_blocks = "warn"

[patch.crates-io]

# tinymist-assets = { path = "./crates/tinymist-assets/" }
tinymist-assets = { path = "./crates/tinymist-assets/" }

typst = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "tinymist-v0.11.1" }
typst-timing = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "tinymist-v0.11.1" }
Expand Down
15 changes: 9 additions & 6 deletions tools/typst-dom/src/typst-doc.canvas.mts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface CreateCanvasOptions {

export interface UpdateCanvasOptions {
cancel?: TypstCancellationToken;
lazy?: boolean;
}

export interface TypstCanvasDocument {
Expand All @@ -36,7 +37,7 @@ export interface TypstCanvasDocument {
export function provideCanvasDoc<
TBase extends GConstructor<
TypstDocumentContext & Partial<TypstOutlineDocument>
>
>,
>(Base: TBase): TBase & GConstructor<TypstCanvasDocument> {
return class CanvasDocument extends Base {
feat$canvas = true;
Expand Down Expand Up @@ -122,11 +123,13 @@ export function provideCanvasDoc<
// await Promise.all(pagesInfo.map(async (pageInfo) => {
this.kModule.backgroundColor = "#ffffff";
this.kModule.pixelPerPt = this.pixelPerPt;
const timeout = async (ms: number) => {
const waitABit = async () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(undefined);
}, ms);
if (opts?.lazy) {
requestIdleCallback(() => resolve(undefined), { timeout: 100 });
} else {
setTimeout(() => resolve(undefined), 0);
}
});
};
for (const pageInfo of pages) {
Expand Down Expand Up @@ -180,7 +183,7 @@ export function provideCanvasDoc<
pageInfo.elem.setAttribute("data-cache-key", result.cacheKey);
}

await timeout(0);
await waitABit();
}

console.log("updateCanvas done", performance.now() - perf);
Expand Down
24 changes: 14 additions & 10 deletions tools/typst-dom/src/typst-doc.svg.mts
Original file line number Diff line number Diff line change
Expand Up @@ -620,16 +620,20 @@ export function provideSvgDoc<
);

const tok = (this.canvasRenderCToken = new TypstCancellationToken());
setTimeout(async () => {
await waitCancel;
this.updateCanvas(pagesInCanvasMode, {
cancel: tok,
}).finally(() => {
if (tok === this.canvasRenderCToken) {
this.canvasRenderCToken = undefined;
}
});
}, 100);
requestIdleCallback(
async () => {
await waitCancel;
this.updateCanvas(pagesInCanvasMode, {
cancel: tok,
lazy: true,
}).finally(() => {
if (tok === this.canvasRenderCToken) {
this.canvasRenderCToken = undefined;
}
});
},
{ timeout: 1000 }
);
}

if (this.isContentPreview) {
Expand Down