useWorker()

Import

import { useWorker } from "@koale/useworker";

Usage

const [workerFn, controller] = useWorker(fn, options);

Hook API

ValueTypeDescription
fnFunctionThe pure function to run with web workers
workerFnPromiseThe function that allows you to run fn with web worker
controllerObjectHook controller ( see Controller API)
optionsObjectThe object containing the options of the worker
note

to view the values of WORKER_STATUS click here: Status API

Controller API

ValueTypeDescription
status@WORKER_STATUSThe status of workerFn
killFunctionThe function that allows killing the worker

Options

Options API

ValueTypeDefaultDescription
timeoutNumberundefinedThe number of milliseconds before killing the worker
remoteDependenciesArray of String[]An array that contains the remote dependencies needed to run the worker
autoTerminateBooleantrueKill the worker once it's done (success or error)
transferableString'auto'Enable Transferable Objects, to disable it set transferable: 'none'

Options Example

import { useWorker } from "@koale/useworker";
const fn = dates => dates.sort(dateFns.compareAsc)
const [workerFn, {status: workerStatus, kill: workerTerminate }] = useWorker(fn, {
timeout: 50000 // 5 seconds
remoteDependencies: [
"https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.30.1/date_fns.js" // dateFns
],
});
tip

To see an example click here: ExternalScripts