From 13dcc44ee8c89c8293f5ec6e323824b0e88b5761 Mon Sep 17 00:00:00 2001 From: Anton Veselev Date: Thu, 25 Jan 2024 02:09:24 +0000 Subject: [PATCH] improve typing --- src/utils/worker/constants.ts | 15 ++++++++++++ src/utils/worker/expose.ts | 20 ++++++++++++---- src/utils/worker/sendAndReceive.ts | 38 +++++++++++++++++------------- 3 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/utils/worker/constants.ts diff --git a/src/utils/worker/constants.ts b/src/utils/worker/constants.ts new file mode 100644 index 00000000..ae099d4b --- /dev/null +++ b/src/utils/worker/constants.ts @@ -0,0 +1,15 @@ +export const FOR_WORKER_CORRELATION_ID = 'corelationId' +export const FOR_WORKER_DATA_KEY = 'data' + +export interface MessageForWorker { + [FOR_WORKER_CORRELATION_ID]: string + [FOR_WORKER_DATA_KEY]: Response +} + +export const FROM_WORKER_CORRELATION_ID = 'corelationId111' +export const FROM_WORKER_DATA_KEY = 'data111' + +export interface MessageFromWorker { + [FROM_WORKER_CORRELATION_ID]: string + [FROM_WORKER_DATA_KEY]: Response +} diff --git a/src/utils/worker/expose.ts b/src/utils/worker/expose.ts index dc6f04a2..dbe2156a 100644 --- a/src/utils/worker/expose.ts +++ b/src/utils/worker/expose.ts @@ -1,14 +1,26 @@ +import { + FOR_WORKER_CORRELATION_ID, + FOR_WORKER_DATA_KEY, + FROM_WORKER_CORRELATION_ID, + FROM_WORKER_DATA_KEY, + type MessageForWorker, + type MessageFromWorker, +} from './constants' + export default any>(func: F) => { // eslint-disable-next-line no-restricted-globals addEventListener('message', (e: MessageEvent) => { - const { correlationId, data } = e.data + const { + [FOR_WORKER_CORRELATION_ID]: correlationId, + [FOR_WORKER_DATA_KEY]: data, + } = e.data as MessageForWorker[0]> const result = func(data) postMessage({ - correlationId, - data: result, - }) + [FROM_WORKER_CORRELATION_ID]: correlationId, + [FROM_WORKER_DATA_KEY]: result, + } satisfies MessageFromWorker) }) } diff --git a/src/utils/worker/sendAndReceive.ts b/src/utils/worker/sendAndReceive.ts index 86c7a0b1..da2efb45 100644 --- a/src/utils/worker/sendAndReceive.ts +++ b/src/utils/worker/sendAndReceive.ts @@ -1,30 +1,36 @@ -export default (worker: Worker) => { - interface ReceivedMessage { - correlationId: string - data: Response - } +import { + FOR_WORKER_CORRELATION_ID, + FOR_WORKER_DATA_KEY, + FROM_WORKER_CORRELATION_ID, + FROM_WORKER_DATA_KEY, + type MessageFromWorker, +} from './constants' +export default (worker: Worker) => { type Callback = (response: Response) => void const callbacks = new Map() - worker.addEventListener('message', (e: MessageEvent) => { - const id = e.data.correlationId - const cb = callbacks.get(e.data.correlationId) - if (!cb) { - return - } + worker.addEventListener( + 'message', + (e: MessageEvent>) => { + const id = e.data[FROM_WORKER_CORRELATION_ID] + const cb = callbacks.get(id) + if (!cb) { + return + } - callbacks.delete(id) - cb(e.data.data) - }) + callbacks.delete(id) + cb(e.data[FROM_WORKER_DATA_KEY]) + }, + ) return (message: Request) => new Promise(resolve => { const id = globalThis.crypto.randomUUID() callbacks.set(id, resolve) worker.postMessage({ - correlationId: id, - data: message, + [FOR_WORKER_CORRELATION_ID]: id, + [FOR_WORKER_DATA_KEY]: message, }) }) }