-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}) | ||
} |