Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Friendlier public API #63

Open
lionel-rowe opened this issue Oct 14, 2024 · 0 comments
Open

Friendlier public API #63

lionel-rowe opened this issue Oct 14, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@lionel-rowe
Copy link

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?

@justinwilaby justinwilaby added the enhancement New feature or request label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants