Skip to content

Commit

Permalink
feat: worker-task
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Jan 20, 2024
1 parent 7c6bb13 commit dd2767f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
import "./test/time.test.ts";
import "./test/worker.test.ts";
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion mod.universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
export * from "./src/time.ts";
export * from "./src/worker.ts";
59 changes: 59 additions & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* WIP.
*/
export interface WorkerMessage<T>{
message: T;
transfer?: Transferable[];
}

/**
* WIP.
*/
export type WorkerTask<T, K> = (message:T) => WorkerMessage<K> | Promise<WorkerMessage<K>>;

/**
* WIP.
* @example
* ```ts
* const task = createTask(()=>{});
* ```
*/
export function createTask<T, K>(task:WorkerTask<T, K>){
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<K>((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
});
});
};
}
23 changes: 23 additions & 0 deletions test/worker.test.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array, Uint8Array>((v)=>{
const result = v.map(n => n * 2);

return {
message: result,
transfer: [result]
};
});

const result = await task(sample1, [sample1]);

assertEquals(result, sample2);
}
});

0 comments on commit dd2767f

Please sign in to comment.