Skip to content

Commit

Permalink
Merge pull request #122 from ashish-r/fix/kill-worker
Browse files Browse the repository at this point in the history
Fix kill functionality
  • Loading branch information
alewin committed Dec 11, 2022
2 parents 1ceb211 + 638859f commit 19d2f24
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/useWorker/src/lib/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum WORKER_STATUS {
RUNNING = 'RUNNING',
ERROR = 'ERROR',
TIMEOUT_EXPIRED = 'TIMEOUT_EXPIRED',
KILLED = 'KILLED',
}

export default WORKER_STATUS
18 changes: 11 additions & 7 deletions packages/useWorker/src/useWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const DEFAULT_OPTIONS: Options = {
export const useWorker = <T extends (...fnArgs: any[]) => any>(
fn: T, options: Options = DEFAULT_OPTIONS,
) => {
const [workerStatus, _setWorkerStatus] = React.useState<WORKER_STATUS>(WORKER_STATUS.PENDING)
const [workerStatus, setWorkerStatus] = React.useState<WORKER_STATUS>(WORKER_STATUS.PENDING)
const worker = React.useRef<Worker & { _url?: string }>()
const isRunning = React.useRef(false)
const promise = React.useRef<{
Expand All @@ -49,11 +49,6 @@ export const useWorker = <T extends (...fnArgs: any[]) => any>(
}>({})
const timeoutId = React.useRef<number>()

const setWorkerStatus = React.useCallback((status: WORKER_STATUS) => {
isRunning.current = status === WORKER_STATUS.RUNNING
_setWorkerStatus(status)
}, [])

const killWorker = React.useCallback(() => {
if (worker.current?._url) {
worker.current.terminate()
Expand Down Expand Up @@ -156,11 +151,20 @@ export const useWorker = <T extends (...fnArgs: any[]) => any>(
return callWorker(...fnArgs)
}, [options.autoTerminate, generateWorker, callWorker])

const killWorkerController = React.useCallback(() => {
killWorker()
setWorkerStatus(WORKER_STATUS.KILLED)
}, [killWorker, setWorkerStatus])

const workerController = {
status: workerStatus,
kill: killWorker,
kill: killWorkerController,
}

React.useEffect(() => {
isRunning.current = workerStatus === WORKER_STATUS.RUNNING
}, [workerStatus])

React.useEffect(() => () => {
killWorker()
}, [killWorker])
Expand Down
15 changes: 8 additions & 7 deletions packages/website/docs/workerstatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { WORKER_STATUS } from "@koale/useworker";

## API

| WORKER_STATUS | Type | Description |
| ----------------- | ------ | ------------------------------------------------------------------ |
| `PENDING` | string | the web worker has been initialized, but has not yet been executed |
| `SUCCESS` | string | the web worker, has been executed correctly |
| `RUNNING` | string | the web worker, is running |
| `ERROR` | string | the web worker, ended with an error |
| `TIMEOUT_EXPIRED` | string | The web worker was killed because the defined timeout expired. |
| WORKER_STATUS | Type | Description |
| ----------------- | ------ | ---------------------------------------------------------------------------------|
| `PENDING` | string | the web worker has been initialized, but has not yet been executed |
| `SUCCESS` | string | the web worker, has been executed correctly |
| `RUNNING` | string | the web worker, is running |
| `ERROR` | string | the web worker, ended with an error |
| `TIMEOUT_EXPIRED` | string | The web worker was killed because the defined timeout expired |
| `KILLED` | string | The web worker was killed by the [kill](./useworker.md#controller-api) function |

0 comments on commit 19d2f24

Please sign in to comment.