- const waitAddedWorkerElement = new Promise<R>((resolve, reject) => {
- const messageHandler = (message: WorkerMessage<R>): void => {
- if (message.event === WorkerMessageEvents.addedWorkerElement) {
- ++workerSetElement.numberOfWorkerElements
- resolve(message.data)
- workerSetElement.worker.off('message', messageHandler)
- } else if (message.event === WorkerMessageEvents.workerElementError) {
- // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
- reject(message.data)
- workerSetElement.worker.off('message', messageHandler)
- }
- }
- workerSetElement.worker.on('message', messageHandler)
- })
- workerSetElement.worker.postMessage({
- event: WorkerMessageEvents.addWorkerElement,
- data: elementData
+ const sendMessageToWorker = new Promise<R>((resolve, reject) => {
+ const message = {
+ uuid: randomUUID(),
+ event: WorkerMessageEvents.addWorkerElement,
+ data: elementData
+ } satisfies WorkerMessage<D>
+ workerSetElement.worker.postMessage(message)
+ this.promiseResponseMap.set(message.uuid, { resolve, reject, workerSetElement })