alewin / useWorker

⚛️ useWorker() - A React Hook for Blocking-Free Background Tasks

Home Page:https://useworker.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to cancel de previous execution?

LorhanSohaky opened this issue · 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