-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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 | ||
}); | ||
}); | ||
}; | ||
} |
This file contains 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
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); | ||
} | ||
}); |