alewin/useWorker

How to cancel de previous execution?

LorhanSohaky opened this issue · 2 comments

I'm trying to use fuzzy search in a worker, but I'm having the following error:

[useWorker] You can only run one instance of the worker at a time, if you want to run more than one in parallel, create another instance with the hook useWorker()

Code:

import debounce from 'lodash.debounce'
import { useWorker, WORKER_STATUS } from "@koale/useworker";

const fuseSearch = ({ list, query, limit, options }) => {
  // my fuse search
}

const MyComponent = () => {
  const [search, controller] = useWorker(fuseSearch)
  
  const handleInputChange = debounce(async value => {
    controller.kill()
    
    setLoading(true)
    try {
      const data = await search({ list: fundos, query: value, limit: 10, options })
      setLoading(false)
      setResultados(data)
    } catch (err) {
      if (err?.message) {
        setLoading(false)
        console.error(err)
      }
    }
  }, 300)
}

There is an issue with the kill functionality of the library, I have created a fix for the same. Waiting for the maintainers to review and merge the same.
#122