useWorker icon indicating copy to clipboard operation
useWorker copied to clipboard

How to cancel de previous execution?

Open LorhanSohaky opened this issue 2 years ago • 1 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)
}

LorhanSohaky avatar Mar 07 '22 12:03 LorhanSohaky

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. https://github.com/alewin/useWorker/pull/122

ashish-r avatar May 21 '22 07:05 ashish-r