Skip to content

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

fix: non-stop linting

Elijah Potter60adfbe28140
diff --git a/src/harper/useIgnoredLintState.ts b/src/harper/useIgnoredLintState.ts
index 825c5f503..dca5fdee7 100644
--- a/src/harper/useIgnoredLintState.ts
+++ b/src/harper/useIgnoredLintState.ts
@@ -1,6 +1,7 @@
import { Lint } from 'harper.js';
import { useDispatch, useSelect } from '@wordpress/data';
import { useLinter } from './LinterProvider';
+import { useCallback } from 'react';
const KEY = 'ignoredLints';
@@ -15,10 +16,12 @@ export default function useIgnoredLintState(): [
const { set } = useDispatch('core/preferences');
- return [
- ignoredLintState,
+ const updateState = useCallback(
(newValue: string) => set('harper-wp', KEY, newValue),
- ];
+ [set]
+ );
+
+ return [ignoredLintState, updateState];
}
/** Get a callback that adds a lint to the global ignored lint state. */
diff --git a/src/harper/useLintBoxes.ts b/src/harper/useLintBoxes.ts
index c2581930d..a9000d0bc 100644
--- a/src/harper/useLintBoxes.ts
+++ b/src/harper/useLintBoxes.ts
@@ -24,23 +24,24 @@ export default function useLintBoxes(
const [loading, setLoading] = useState(true);
const updateLints = useCallback(async () => {
+ await linter.clearIgnoredLints();
+
+ const tasks = [linter.setLintConfig(config)];
+
+ if (ignoreState) {
+ tasks.push(linter.importIgnoredLints(ignoreState));
+ }
+
+ await Promise.all(tasks);
+
// We assume that a given index always refers to the same rich text field.
- console.log('Start');
const newLints = await Promise.all(
richTexts.map(async (richText) => {
const contents = richText.getTextContent();
- await linter.clearIgnoredLints();
-
- if (ignoreState) {
- await linter.importIgnoredLints(ignoreState);
- }
-
- await linter.setLintConfig(config);
return await linter.lint(contents);
})
);
- console.log('End', newLints.flat().length);
setLoading(false);
setLints(newLints);
@@ -62,7 +63,7 @@ export default function useLintBoxes(
return () => {
observers.forEach((observer) => observer.disconnect());
};
- }, [richTexts, ignoreState, updateLints]);
+ }, [richTexts, updateLints]);
// Update the lint boxes each frame.
// Probably overkill.
diff --git a/src/harper/useLintConfig.ts b/src/harper/useLintConfig.ts
index a167fa126..adb43deba 100644
--- a/src/harper/useLintConfig.ts
+++ b/src/harper/useLintConfig.ts
@@ -1,8 +1,7 @@
import { useDispatch, useSelect } from '@wordpress/data';
import { LintConfig } from 'harper.js';
import { useLinter } from './LinterProvider';
-import useToggle from './useToggle';
-import { useCallback, useEffect, useState } from 'react';
+import { useCallback, useEffect, useMemo, useState } from 'react';
const KEY = 'lintConfig';
@@ -11,19 +10,16 @@ export default function useLintConfig(): [
(newState: LintConfig) => void,
] {
const defaultConfig = useDefaultLintConfig();
- let lintConfig = useSelect(
+ const lintConfig = useSelect(
(select) => select('core/preferences').get('harper-wp', KEY),
[]
);
const { set } = useDispatch('core/preferences');
- const setConfig = useCallback(
- (newValue) => {
- set('harper-wp', KEY, newValue);
- },
- [set]
- );
+ const setConfig = useCallback((newValue) => {
+ set('harper-wp', KEY, newValue);
+ }, []);
useEffect(() => {
if (lintConfig == null) {
@@ -31,11 +27,14 @@ export default function useLintConfig(): [
}
}, [defaultConfig, setConfig]);
- if (lintConfig == null) {
- lintConfig = {};
- }
+ const nonNull = useMemo(() => {
+ if (lintConfig == null) {
+ return {};
+ }
+ return lintConfig;
+ }, [lintConfig]);
- return [lintConfig, setConfig];
+ return [nonNull, setConfig];
}
export function useDefaultLintConfig(): LintConfig {