Created
August 1, 2024 15:11
-
-
Save VehpuS/c62e95e7d0420a9d30440c1eafab0f92 to your computer and use it in GitHub Desktop.
Web pack enabled web workers
This file contains hidden or 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
export const testFunction = () => { | |
return Array(1000).fill(2).map((n, i) => n * i).map((n, i) => n * i ** 2) | |
} |
This file contains hidden or 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
import { testFunction } from './testFunction'; | |
onmessage = function (e: MessageEvent<{ args: Parameters<typeof testFunction> }>) { | |
const result = formatFeatures(...e.data.args); | |
postMessage(result); | |
}; |
This file contains hidden or 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
import { testFunction } from './testFunction'; | |
export async function testFunctionWebWorker( | |
...args: Parameters<typeof testFunction> | |
): Promise<ReturnType<typeof testFunction>> { | |
const worker = new Worker(new URL('./worker.ts', import.meta.url)); | |
return new Promise<ReturnType<typeof testFunction>>((resolve, reject) => { | |
worker.onmessage = function (e: MessageEvent<ReturnType<typeof testFunction>>) { | |
resolve(e.data); | |
worker.terminate(); | |
}; | |
worker.onerror = function (e: ErrorEvent) { | |
reject(e); | |
worker.terminate(); | |
}; | |
worker.postMessage({ args }); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment