Dispatch isolated editor edits promptly and measure browser paints
This commit is contained in:
1 parent
bf42a88dff
commit
473b3beace
6 files changed
+91
-32
No files matched your search
@@ -22,7 +22,8 @@ let publishedRevision = null;
|
||||
let draft = null;
|
||||
let composing = false;
|
||||
let compileTimer;
|
||||
let frame;
|
||||
let lastTypingAt = -Infinity;
|
||||
let promptCompilePending = false;
|
||||
const projectPrefix = location.pathname.startsWith("/p/") ? location.pathname.slice(0, -1) : "";
|
||||
const previewDraft = new URLSearchParams(location.hash.slice(1)).get("preview") !== "published";
|
||||
|
||||
@@ -63,7 +64,7 @@ function selectFile(filename) {
|
||||
source.blur();
|
||||
if (composing) {
|
||||
composing = false;
|
||||
queueCompile();
|
||||
queueCompile(true);
|
||||
}
|
||||
}
|
||||
fileViews.set(activeFile, {
|
||||
@@ -244,27 +245,32 @@ async function compile(edit, submitted, changes) {
|
||||
function invalidatePreview() {
|
||||
++sequence;
|
||||
clearTimeout(compileTimer);
|
||||
cancelAnimationFrame(frame);
|
||||
compileTimer = undefined;
|
||||
promptCompilePending = false;
|
||||
restorePublishedPages();
|
||||
}
|
||||
|
||||
function queueCompile(delay = EDIT_IDLE_MS) {
|
||||
invalidatePreview();
|
||||
const edit = sequence;
|
||||
function queueCompile(immediate = false) {
|
||||
const submitted = performance.now();
|
||||
// Edits arriving before the first dispatch share its prompt submission.
|
||||
const prompt = immediate || promptCompilePending || submitted - lastTypingAt >= EDIT_IDLE_MS;
|
||||
invalidatePreview();
|
||||
if (!immediate) lastTypingAt = submitted;
|
||||
promptCompilePending = prompt;
|
||||
const edit = sequence;
|
||||
if (status.textContent !== "正在编译") status.textContent = "正在编译";
|
||||
const submit = () => {
|
||||
compileTimer = setTimeout(() => {
|
||||
if (edit !== sequence || composing) return;
|
||||
compileTimer = undefined;
|
||||
promptCompilePending = false;
|
||||
compile(edit, submitted, Object.fromEntries(edits));
|
||||
};
|
||||
if (delay) compileTimer = setTimeout(submit, delay);
|
||||
else frame = requestAnimationFrame(submit);
|
||||
}, prompt ? 0 : EDIT_IDLE_MS);
|
||||
}
|
||||
|
||||
function recordEdit(forceCompile = false) {
|
||||
const changed = source.value !== (edits.get(activeFile) ?? files.get(activeFile));
|
||||
if (changed) edits.set(activeFile, source.value);
|
||||
if (changed || forceCompile) queueCompile();
|
||||
if (changed || forceCompile) queueCompile(forceCompile);
|
||||
}
|
||||
|
||||
function replaceSourceRange(text, start, end, selectionStart, selectionEnd, direction) {
|
||||
@@ -415,7 +421,7 @@ async function openProject() {
|
||||
event.preventDefault();
|
||||
indentSource();
|
||||
});
|
||||
queueCompile(0);
|
||||
queueCompile(true);
|
||||
} catch (failure) {
|
||||
showError(failure.message);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user