Description
The current SpellcheckerWasm
implementation is a bit confusing to use from JS, given that the constructor takes a callback function and there's no obvious way to get at the results outside of the callback or even to match up each result to its corresponding input. I get that it's due to how JS interacts with WASM, but it feels like an implementation detail that should be hidden from library consumers.
Also, given that callbacks are often used for async functionality, a reasonable (but incorrect) assumption would be that the callback is called asynchronously.
A more intuitive API could be achieved something like this:
class SpellChecker {
#results: SuggestedItem[] = []
#scw = new SpellcheckerWasm((x) => this.#results = x)
async init(wasm: string | Response, dictionary: string | Response, bigram?: string | Response) {
await this.#scw.prepareSpellchecker(wasm, dictionary, bigram)
}
suggest(word: string, options? CheckSpellingOptions): SuggestedItem[] {
this.#scw.checkSpelling(word, options)
return this.#results
}
suggestCompound(sentence: string, options?: Pick<CheckSpellingOptions, 'maxEditDistance'>): SuggestedItem[] {
this.#scw.checkSpellingCompound(word, options)
return this.#results
}
}
Perhaps this could be incorporated into SpellcheckerWasm
's public API, with direct calling of checkSpelling
etc either made private (breaking change) or deprecated?