Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

perf(lint-framework): remove unconditional lint polling (#3678)

Elias04199ffc854e4c38
diff --git a/packages/lint-framework/src/lint/LintFramework.ts b/packages/lint-framework/src/lint/LintFramework.ts
index a66caf5c9..c1e6ed6d6 100644
--- a/packages/lint-framework/src/lint/LintFramework.ts
+++ b/packages/lint-framework/src/lint/LintFramework.ts
@@ -41,6 +41,7 @@ export default class LintFramework {
private scrollableAncestors: Set<HTMLElement>;
private lintRequested = false;
private renderRequested = false;
+ private lintDelayTimer: number | null = null;
private lastInputAt = 0;
private lastLints: { target: HTMLElement; lints: UnpackedLintGroups }[] = [];
private lastBoxes: IgnorableLintBox[] = [];
@@ -85,14 +86,6 @@ export default class LintFramework {
this.update();
};
- const timeoutCallback = () => {
- this.update();
-
- setTimeout(timeoutCallback, 100);
- };
-
- timeoutCallback();
-
this.attachWindowListeners();
}
@@ -114,16 +107,30 @@ export default class LintFramework {
this.requestLintUpdate();
}
- async requestLintUpdate() {
- if (this.lintRequested) {
+ async requestLintUpdate(force = false) {
+ if (this.lintRequested || this.targets.size === 0) {
return;
}
const delay = (await this.actions.getDelay?.()) ?? 0;
- if (delay > 0 && Date.now() - this.lastInputAt < delay) {
+ const remainingDelay = delay - (Date.now() - this.lastInputAt);
+ if (!force && delay > 0 && remainingDelay > 0) {
+ if (this.lintDelayTimer != null) {
+ window.clearTimeout(this.lintDelayTimer);
+ }
+
+ this.lintDelayTimer = window.setTimeout(() => {
+ this.lintDelayTimer = null;
+ void this.requestLintUpdate();
+ }, remainingDelay);
return;
}
+ if (this.lintDelayTimer != null) {
+ window.clearTimeout(this.lintDelayTimer);
+ this.lintDelayTimer = null;
+ }
+
// Avoid duplicate requests in the queue
this.lintRequested = true;
@@ -339,7 +346,12 @@ export default class LintFramework {
}
return computeLintBoxes(target, currentLint, ruleName, {
- ignoreLint: this.actions.ignoreLint,
+ ignoreLint: this.actions.ignoreLint
+ ? async (hash: string) => {
+ await this.actions.ignoreLint?.(hash);
+ await this.requestLintUpdate(true);
+ }
+ : undefined,
});
}),
);