-
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.
* change year. * update deps. * feat: worker-task * fix test code. * fix test code. * worker type. * remove unstable permission. * add jsdoc. * transfer typedarray. * revoke url each time. * dispose method. * feat: delay * wording. * feat: primitive type convert * change file name. * add jsdoc. * fix primitive. * feat primitive convert and remove env. * fix log deprecated.
- Loading branch information
Showing
16 changed files
with
295 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export {assertEquals} from "https://deno.land/std@0.210.0/assert/mod.ts"; | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.210.0/path/mod.ts"; | ||
export {exists} from "https://deno.land/std@0.210.0/fs/mod.ts"; | ||
export {assertEquals} from "https://deno.land/std@0.212.0/assert/mod.ts"; | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.212.0/path/mod.ts"; | ||
export {exists} from "https://deno.land/std@0.212.0/fs/mod.ts"; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.210.0/path/mod.ts"; | ||
export {Logger, handlers} from "https://deno.land/std@0.210.0/log/mod.ts"; | ||
export {format} from "https://deno.land/std@0.210.0/datetime/mod.ts"; | ||
export {dirname, fromFileUrl} from "https://deno.land/std@0.212.0/path/mod.ts"; | ||
export {Logger, ConsoleHandler, FileHandler} from "https://deno.land/std@0.212.0/log/mod.ts"; | ||
export {format} from "https://deno.land/std@0.212.0/datetime/mod.ts"; |
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 was deleted.
Oops, something went wrong.
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,116 @@ | ||
/** | ||
* Infer data type from literal type. | ||
*/ | ||
export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T; | ||
|
||
/** | ||
* Something that might be string. | ||
*/ | ||
export type MaybeString = string | null | undefined; | ||
|
||
/** | ||
* Whether to allow `undefined`. | ||
*/ | ||
export type TypeStrict<T extends unknown, U extends boolean> = U extends true ? T : T | undefined; | ||
|
||
/** | ||
* Map of primitive types and string that specify them. | ||
*/ | ||
export interface PrimitiveMap{ | ||
"string": string; | ||
"number": number; | ||
"boolean": boolean; | ||
} | ||
|
||
function undef(strict?:boolean){ | ||
if(strict){ | ||
throw new Error(); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Convert from dirty text to specified type. | ||
* Enabling `strict` flag will throw exception if parsing is not possible. | ||
* @example | ||
* ```ts | ||
* const value = primitiveParse("123", "number", true); | ||
* ``` | ||
*/ | ||
export function primitiveParse<T extends keyof PrimitiveMap, U extends boolean>(text:MaybeString, type:T, strict?:U):TypeStrict<PrimitiveMap[T], U>{ | ||
switch(type){ | ||
case "string": { | ||
const v = String(text); | ||
|
||
if(text === undefined || text === null){ | ||
return <TypeStrict<PrimitiveMap[T], U>>undef(strict); | ||
} | ||
|
||
return <TypeStrict<PrimitiveMap[T], U>>v; | ||
} | ||
|
||
case "number": { | ||
const v = Number(text); | ||
|
||
if(text === undefined || text === null || isNaN(v)){ | ||
return <TypeStrict<PrimitiveMap[T], U>>undef(strict); | ||
} | ||
|
||
return <TypeStrict<PrimitiveMap[T], U>>v; | ||
} | ||
|
||
case "boolean": { | ||
switch(text){ | ||
case "true": return <TypeStrict<PrimitiveMap[T], U>>true; | ||
case "false": return <TypeStrict<PrimitiveMap[T], U>>false; | ||
default: return <TypeStrict<PrimitiveMap[T], U>>undef(strict); | ||
} | ||
} | ||
|
||
default: throw new Error(); | ||
} | ||
} | ||
|
||
/** | ||
* Convert from dirty text to specified type. | ||
* If cannot be parsed, use default (`def`) value. | ||
* Convert to same type as default value. | ||
* @example | ||
* ```ts | ||
* const value = primitiveParseX("123", 0); | ||
* ``` | ||
*/ | ||
export function primitiveParseX<T extends string | number | boolean>(text:MaybeString, def:T):WidenLiteral<T>{ | ||
switch(typeof def){ | ||
case "string": { | ||
const v = String(text); | ||
|
||
if(text === undefined || text === null){ | ||
return <WidenLiteral<T>>def; | ||
} | ||
|
||
return <WidenLiteral<T>>v; | ||
} | ||
|
||
case "number": { | ||
const v = Number(text); | ||
|
||
if(text === undefined || text === null || isNaN(v)){ | ||
return <WidenLiteral<T>>def; | ||
} | ||
|
||
return <WidenLiteral<T>>v; | ||
} | ||
|
||
case "boolean": { | ||
switch(text){ | ||
case "true": return <WidenLiteral<T>>true; | ||
case "false": return <WidenLiteral<T>>false; | ||
default: return <WidenLiteral<T>>def; | ||
} | ||
} | ||
|
||
default: throw new Error(); | ||
} | ||
} |
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,84 @@ | ||
/** | ||
* TypedArray also automatically unwrap to `ArrayBuffer`. | ||
*/ | ||
export type TaskTransfer = Transferable | ArrayBufferView; | ||
|
||
/** | ||
* Content of processing run by worker thread. | ||
*/ | ||
export type TaskAction<T extends unknown, K extends unknown> = (message:T) => TaskMessage<K> | Promise<TaskMessage<K>>; | ||
|
||
/** | ||
* Run registered `TaskAction` in worker thread. | ||
*/ | ||
export type TaskContext<T extends unknown, K extends unknown> = (message:T, transfers?:TaskTransfer[]) => Promise<K>; | ||
|
||
/** | ||
* Communication content between main thread and worker thread. | ||
*/ | ||
export interface TaskMessage<T extends unknown>{ | ||
message: T; | ||
transfers?: TaskTransfer[]; | ||
} | ||
|
||
/** | ||
* Register `TaskAction` and return reusable task execution context. | ||
* `Worker` instance is created and destroyed each time they run `TaskContext`. | ||
* `import` can only use "syntax", not "declaration". | ||
* @example | ||
* ```ts | ||
* const task = createTask<number, number>(async(data)=>{ | ||
* const {delay} = await import("https://deno.land/std/async/mod.ts"); | ||
* await delay(1000); | ||
* return { | ||
* message: data * 2 | ||
* }; | ||
* }); | ||
* const result1 = await task(1); | ||
* const result2 = await task(2); | ||
* ``` | ||
*/ | ||
export function createTask<T extends unknown, K extends unknown>(task:TaskAction<T, K>):TaskContext<T, K>{ | ||
const script = task.toString(); | ||
const regist = /*js*/` | ||
globalThis.onmessage = async({data})=>{ | ||
const {message, transfers} = await(${script})(data); | ||
globalThis.postMessage(message, { | ||
transfer: transfers?.map(v => "buffer" in v ? v.buffer : v) | ||
}); | ||
}; | ||
`; | ||
|
||
return (message, transfers)=>{ | ||
return new Promise<K>((res, rej)=>{ | ||
const url = URL.createObjectURL(new Blob([regist])); | ||
const worker = new Worker(url, { | ||
type: "module" | ||
}); | ||
|
||
function dispose(){ | ||
worker.terminate(); | ||
URL.revokeObjectURL(url); | ||
} | ||
|
||
worker.onmessage = ({data})=>{ | ||
res(data); | ||
dispose(); | ||
}; | ||
|
||
worker.onerror = (e)=>{ | ||
rej(e); | ||
dispose(); | ||
}; | ||
|
||
worker.onmessageerror = (e)=>{ | ||
rej(e); | ||
dispose(); | ||
}; | ||
|
||
worker.postMessage(message, { | ||
transfer: transfers?.map(v => "buffer" in v ? v.buffer : v) | ||
}); | ||
}); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.