From dd2767fe8f1a6d73ab5f3983ebeebbac155741ff Mon Sep 17 00:00:00 2001 From: dojyorin Date: Sun, 21 Jan 2024 03:29:23 +0900 Subject: [PATCH] feat: worker-task --- mod.test.ts | 3 ++- mod.ts | 1 + mod.universal.ts | 3 ++- src/worker.ts | 59 +++++++++++++++++++++++++++++++++++++++++++++ test/worker.test.ts | 23 ++++++++++++++++++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/worker.ts create mode 100644 test/worker.test.ts diff --git a/mod.test.ts b/mod.test.ts index 44d2d4b..fec756e 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -14,4 +14,5 @@ import "./test/platform.deno.test.ts"; import "./test/process.deno.test.ts"; import "./test/stream.test.ts"; import "./test/text.test.ts"; -import "./test/time.test.ts"; \ No newline at end of file +import "./test/time.test.ts"; +import "./test/worker.test.ts"; \ No newline at end of file diff --git a/mod.ts b/mod.ts index b1fd6ed..81ad9cc 100644 --- a/mod.ts +++ b/mod.ts @@ -9,6 +9,7 @@ export * from "./src/minipack.ts"; export * from "./src/stream.ts"; export * from "./src/text.ts"; export * from "./src/time.ts"; +export * from "./src/worker.ts"; export * from "./src/env.deno.ts"; export * from "./src/json.deno.ts"; diff --git a/mod.universal.ts b/mod.universal.ts index 51d84c2..5a28eaa 100644 --- a/mod.universal.ts +++ b/mod.universal.ts @@ -8,4 +8,5 @@ export * from "./src/import.ts"; export * from "./src/minipack.ts"; export * from "./src/stream.ts"; export * from "./src/text.ts"; -export * from "./src/time.ts"; \ No newline at end of file +export * from "./src/time.ts"; +export * from "./src/worker.ts"; \ No newline at end of file diff --git a/src/worker.ts b/src/worker.ts new file mode 100644 index 0000000..c8025e7 --- /dev/null +++ b/src/worker.ts @@ -0,0 +1,59 @@ +/** +* WIP. +*/ +export interface WorkerMessage{ + message: T; + transfer?: Transferable[]; +} + +/** +* WIP. +*/ +export type WorkerTask = (message:T) => WorkerMessage | Promise>; + +/** +* WIP. +* @example +* ```ts +* const task = createTask(()=>{}); +* ``` +*/ +export function createTask(task:WorkerTask){ + const script = task.toString(); + const regist = /*js*/` + globalThis.onmessage = async({data})=>{ + const {message, transfer} = await(${script})(data); + globalThis.postMessage(message, { + transfer: transfer + }); + }; + `; + const url = URL.createObjectURL(new Blob([regist])); + + return (message:T, transfer?:Transferable[])=>{ + return new Promise((res, rej)=>{ + const worker = new Worker(url, { + type: "module" + }); + + worker.onmessage = ({data})=>{ + res(data); + worker.terminate(); + }; + + worker.onerror = (e)=>{ + rej(e); + worker.terminate(); + }; + + worker.onmessageerror = (e)=>{ + rej(e); + worker.terminate(); + }; + + worker.postMessage(message, { + transfer: transfer + }); + }); + }; +} \ No newline at end of file diff --git a/test/worker.test.ts b/test/worker.test.ts new file mode 100644 index 0000000..178cb72 --- /dev/null +++ b/test/worker.test.ts @@ -0,0 +1,23 @@ +import {assertEquals} from "../deps.test.ts"; +import {createTask} from "../src/worker.ts"; + +const sample1 = new Uint8Array([1, 2, 3, 4]); +const sample2 = new Uint8Array([1, 4, 6, 8]); + +Deno.test({ + name: "Worker: Create Task.", + async fn(){ + const task = createTask((v)=>{ + const result = v.map(n => n * 2); + + return { + message: result, + transfer: [result] + }; + }); + + const result = await task(sample1, [sample1]); + + assertEquals(result, sample2); + } +}); \ No newline at end of file