Skip to content

Commit

Permalink
improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
inker committed Jan 25, 2024
1 parent 6c72ec7 commit 13dcc44
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/utils/worker/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const FOR_WORKER_CORRELATION_ID = 'corelationId'
export const FOR_WORKER_DATA_KEY = 'data'

export interface MessageForWorker<Response> {
[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<Response> {
[FROM_WORKER_CORRELATION_ID]: string
[FROM_WORKER_DATA_KEY]: Response
}
20 changes: 16 additions & 4 deletions src/utils/worker/expose.ts
Original file line number Diff line number Diff line change
@@ -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 <F extends (arg: any) => 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<Parameters<typeof func>[0]>

const result = func(data)

postMessage({
correlationId,
data: result,
})
[FROM_WORKER_CORRELATION_ID]: correlationId,
[FROM_WORKER_DATA_KEY]: result,
} satisfies MessageFromWorker<typeof result>)
})
}

Expand Down
38 changes: 22 additions & 16 deletions src/utils/worker/sendAndReceive.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
export default <Request, Response>(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 <Request, Response>(worker: Worker) => {
type Callback = (response: Response) => void
const callbacks = new Map<string, Callback>()

worker.addEventListener('message', (e: MessageEvent<ReceivedMessage>) => {
const id = e.data.correlationId
const cb = callbacks.get(e.data.correlationId)
if (!cb) {
return
}
worker.addEventListener(
'message',
(e: MessageEvent<MessageFromWorker<Response>>) => {
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<Response>(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,
})
})
}

0 comments on commit 13dcc44

Please sign in to comment.