mirror of
https://github.com/morten-olsen/bob.git
synced 2026-02-08 01:46:29 +01:00
feat: init
This commit is contained in:
21
packages/playground/src/features/runner/worker.ts
Normal file
21
packages/playground/src/features/runner/worker.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
type WorkerFn = Record<string, (...args: any[]) => any>;
|
||||
|
||||
const createWorker = (fn: WorkerFn) => {
|
||||
self.addEventListener('message', (event) => {
|
||||
const { action, vars = {}, id } = event.data;
|
||||
const run = async () => {
|
||||
const startTime = performance.now();
|
||||
try {
|
||||
const result = await fn[action](vars);
|
||||
const endTime = performance.now();
|
||||
const duration = endTime - startTime;
|
||||
self.postMessage({ type: 'output', payload: result, id, duration });
|
||||
} catch (error) {
|
||||
self.postMessage({ type: 'error', payload: error, id });
|
||||
}
|
||||
};
|
||||
run();
|
||||
});
|
||||
};
|
||||
|
||||
export { createWorker };
|
||||
Reference in New Issue
Block a user