8000 fix(useWebWorker): add web worker transferable option (#3123) · vueuse/vueuse@5988f73 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 5988f73

Browse files
authored
fix(useWebWorker): add web worker transferable option (#3123)
1 parent 04d32d8 commit 5988f73

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

packages/core/useWebWorker/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const { data, post, terminate, worker } = useWebWorker('/path/to/worker.js')
2020
| data | `Ref<any>` | Reference to the latest data received via the worker, can be watched to respond to incoming messages |
2121
| worker | `ShallowRef<Worker \| undefined>` | Reference to the instance of the WebWorker |
2222

23-
| Method | Signature | Description |
24-
| --------- | --------------------- | -------------------------------- |
25-
| post | `(data: any) => void` | Sends data to the worker thread. |
26-
| terminate | `() => void` | Stops and terminates the worker. |
23+
| Method | Signature | Description |
24+
| --------- | ------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
25+
| post | `(message: any, transfer: Transferable[]): void`<br>`(message: any, options?: StructuredSerializeOptions | undefined): void` | Sends data to the worker thread. |
26+
| terminate | `() => void` | Stops and terminates the worker. |

packages/core/useWebWorker/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { tryOnScopeDispose } from '@vueuse/shared'
66
import type { ConfigurableWindow } from '../_configurable'
77
import { defaultWindow } from '../_configurable'
88

9+
type PostMessage = typeof Worker.prototype['postMessage']
10+
911
export interface UseWebWorkerReturn<Data = any> {
1012
data: Ref<Data>
11-
post: typeof Worker.prototype['postMessage']
13+
post: PostMessage
1214
terminate: () => void
1315
worker: ShallowRef<Worker | undefined>
1416
}
@@ -51,11 +53,11 @@ export function useWebWorker<Data = any>(
5153
const data: Ref<any> = ref(null)
5254
const worker = shallowRef<Worker>()
5355

54-
const post: typeof Worker.prototype['postMessage'] = function post(val: any) {
56+
const post: PostMessage = (...args) => {
5557
if (!worker.value)
5658
return
5759

58-
worker.value.postMessage(val)
60+
worker.value.postMessage(...args as Parameters<PostMessage>)
5961
}
6062

6163
const terminate: typeof Worker.prototype['terminate'] = function terminate() {

0 commit comments

Comments
 (0)
0