Skip to content

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

feat(harper.js): reduce the potential for unneeded cache busts

Elijah Potter026751c52a85
diff --git a/harper-core/src/spell/dictionary.rs b/harper-core/src/spell/dictionary.rs
index 403f29b86..3fc7693c8 100644
--- a/harper-core/src/spell/dictionary.rs
+++ b/harper-core/src/spell/dictionary.rs
@@ -38,6 +38,9 @@ pub trait Dictionary: Send + Sync {
/// Iterate over the words in the dictionary.
fn words_iter(&self) -> Box<dyn Iterator<Item = &'_ [char]> + Send + '_>;
+ /// The number of words in the dictionary.
+ fn word_count(&self) -> usize;
+
/// Iterate over all the words in the dictionary of a given length
fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + Send + '_>;
}
diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rs
index 816048043..edc12fa93 100644
--- a/harper-core/src/spell/fst_dictionary.rs
+++ b/harper-core/src/spell/fst_dictionary.rs
@@ -212,6 +212,10 @@ impl Dictionary for FstDictionary {
fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + Send + '_> {
self.full_dict.words_with_len_iter(len)
}
+
+ fn word_count(&self) -> usize {
+ self.full_dict.word_count()
+ }
}
#[cfg(test)]
diff --git a/harper-core/src/spell/merged_dictionary.rs b/harper-core/src/spell/merged_dictionary.rs
index c455a2298..34c050e8f 100644
--- a/harper-core/src/spell/merged_dictionary.rs
+++ b/harper-core/src/spell/merged_dictionary.rs
@@ -104,4 +104,8 @@ impl Dictionary for MergedDictionary {
.take(max_results)
.collect()
}
+
+ fn word_count(&self) -> usize {
+ self.children.iter().map(|d| d.word_count()).sum()
+ }
}
diff --git a/harper-core/src/spell/mutable_dictionary.rs b/harper-core/src/spell/mutable_dictionary.rs
index 5c58b77cd..d8696c95d 100644
--- a/harper-core/src/spell/mutable_dictionary.rs
+++ b/harper-core/src/spell/mutable_dictionary.rs
@@ -260,6 +260,10 @@ impl Dictionary for MutableDictionary {
Box::new(self.words[start..end].iter().map(|v| v.as_slice()))
}
+
+ fn word_count(&self) -> usize {
+ self.words.len()
+ }
}
#[cfg(test)]
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 8a49a0b2c..ddc917986 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -209,6 +209,8 @@ impl Linter {
/// Import words into the dictionary.
pub fn import_words(&mut self, additional_words: Vec<String>) {
+ let init_len = self.user_dictionary.word_count();
+
self.user_dictionary
.extend_words(additional_words.iter().map(|word| {
(
@@ -216,7 +218,11 @@ impl Linter {
WordMetadata::default(),
)
}));
- self.synchronize_lint_dict();
+
+ // Only synchronize if we added words that were not there before.
+ if self.user_dictionary.word_count() > init_len {
+ self.synchronize_lint_dict();
+ }
}
/// Export words from the dictionary.