From 4ee6a9eb7d81724e264b3a2ae59b3d1376286774 Mon Sep 17 00:00:00 2001 From: duart38 Date: Wed, 14 Apr 2021 20:17:30 +0200 Subject: [PATCH 01/21] removed console.log --- Thread.bundle.js | 2 -- Thread.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/Thread.bundle.js b/Thread.bundle.js index 4bab676..5da8139 100644 --- a/Thread.bundle.js +++ b/Thread.bundle.js @@ -12,8 +12,6 @@ class Thread { }); this.imports = imports || []; this.blob = this.populateFile(operation); - this.blob.then(async (b)=>console.log(await b.text()) - ); this.worker = this.makeWorker(type1); } async makeWorker(type) { diff --git a/Thread.ts b/Thread.ts index 216a3d1..b5de034 100644 --- a/Thread.ts +++ b/Thread.ts @@ -24,7 +24,6 @@ export default class Thread { }); this.imports = imports || []; this.blob = this.populateFile(operation); - this.blob.then(async (b)=>console.log(await b.text())); this.worker = this.makeWorker(type); } From 240fd20d6e1a853885f270b95ee455911fb2ac5b Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:27:05 +0200 Subject: [PATCH 02/21] Cleaned up code Signed-off-by: duart38 --- Thread.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Thread.ts b/Thread.ts index b5de034..242b726 100644 --- a/Thread.ts +++ b/Thread.ts @@ -38,7 +38,7 @@ export default class Thread { } private async populateFile(code: Function) { - let imported = this.imports?.flatMap(async (val) => (await this.copyDep(val)).join("\n")); + const imported = this.imports?.flatMap(async (val) => (await this.copyDep(val)).join("\n")); return new Blob([` ${(await Promise.all(imported)).join("\n")} @@ -57,11 +57,11 @@ export default class Thread { * @param str the import line (eg: import {som} from "lorem/ipsum.js";) */ private async copyDep(str: string) { - var importPathRegex = /('|"|`)(.+\.js)(\1)/ig; // for the path string ("lorem/ipsum.js") - var importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; // for the instruction before the path (import {som} from) - var matchedPath = importPathRegex.exec(str) || ""; - var file = false; - var fqfn = ""; + const importPathRegex = /('|"|`)(.+\.js)(\1)/ig; // for the path string ("lorem/ipsum.js") + const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; // for the instruction before the path (import {som} from) + const matchedPath = importPathRegex.exec(str) || ""; + let file = false; + let fqfn = ""; if ( !matchedPath[0].includes("http://") && @@ -70,7 +70,7 @@ export default class Thread { file = true; fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); } - var matchedIns = importInsRegex.exec(str) || ""; // matchedIns[0] > import {sss} from + const matchedIns = importInsRegex.exec(str) || ""; // matchedIns[0] > import {sss} from if (!matchedIns) { throw new Error( @@ -81,10 +81,10 @@ export default class Thread { if (file) { - let x = await import(fqfn); //Deno.realPathSync(fqfn) + const x = await import(fqfn); //Deno.realPathSync(fqfn) return Object.keys(x).map((v)=>x[v].toString()) } else { - let x = await import(matchedPath[0].replaceAll(/'|"/g,"")); + const x = await import(matchedPath[0].replaceAll(/'|"/g,"")); return Object.keys(x).map((v)=>x[v].toString()) } } From 5aa3bedddabae140e4c74ee09507c9b63f8457fd Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:33:46 +0200 Subject: [PATCH 03/21] fixed typo Signed-off-by: duart38 --- test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.ts b/test.ts index e5d05e0..fe110d6 100644 --- a/test.ts +++ b/test.ts @@ -68,7 +68,7 @@ Deno.test("Local file imports work", async () => { assertEquals(await run, 1); }); -Deno.test("Over the new file imports work", async () => { +Deno.test("Over the network file imports work", async () => { let run = new Promise((resolve) => { let t = new Thread((e) => returnNumber(), "module", [ 'import { returnNumber } from "https://raw.githubusercontent.com/duart38/Thread/master/test_import.js"', From 307905633be5b11ab8fb39b2f0ca9e122221c6e4 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:33:57 +0200 Subject: [PATCH 04/21] let -> const Signed-off-by: duart38 --- examples/example_simple.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example_simple.ts b/examples/example_simple.ts index c28476f..6043b1c 100644 --- a/examples/example_simple.ts +++ b/examples/example_simple.ts @@ -1,6 +1,6 @@ import Thread from "../Thread.ts"; -let thread = new Thread((e: MessageEvent) => { +const thread = new Thread((e: MessageEvent) => { console.log("Worker: Message received from main script"); const result = e.data[0] * e.data[1]; if (isNaN(result)) { From 311deb6f26c31a4b334cbe627e48a34bba7ddbe3 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:38:01 +0200 Subject: [PATCH 05/21] added tests for new async support Signed-off-by: duart38 --- test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test.ts b/test.ts index fe110d6..e6f2c37 100644 --- a/test.ts +++ b/test.ts @@ -32,6 +32,20 @@ Deno.test("Worker takes in external function", async () => { assertEquals(await run, 1); }); +Deno.test("Worker async function supported", async () => { + let run = new Promise((resolve) => { + let t = new Thread(async ()=>{ + await new Promise((ir) => setTimeout(ir, 1000)) + return 1; + }, "module"); + t.onMessage((n) => { + t.remove()?.then(() => resolve(n)); + }); + t.postMessage(2); + }); + assertEquals(await run, 1); +}); + Deno.test("Command/Method chaining works", async () => { let run = new Promise((resolve) => { let t = new Thread((e) => 0, "module"); From 814b3a0c6a687728f4ee8bf88d3292bdcb42a02e Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:40:15 +0200 Subject: [PATCH 06/21] add async function support Signed-off-by: duart38 --- Thread.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Thread.ts b/Thread.ts index 242b726..e6705ae 100644 --- a/Thread.ts +++ b/Thread.ts @@ -2,7 +2,7 @@ export default class Thread { public worker: Promise; private imports: Array; private blob: Promise; - private blobURL: string = ""; + private blobURL= ""; /** * Tells if the worker has been stopped */ @@ -13,7 +13,7 @@ export default class Thread { * @param imports Modules to import in the worker. only JS files allowed (over the net import allowed) */ constructor( - operation: (e: MessageEvent, globalObject?:{}) => T, + operation: (e: MessageEvent, globalObject?:{}) => T | Promise, type?: "classic" | "module", imports?: Array, ) { @@ -45,8 +45,8 @@ export default class Thread { var global = {}; var userCode = ${code.toString()} - onmessage = function(e) { - postMessage(userCode(e, global)); + onmessage = async function(e) { + postMessage(await userCode(e, global)); } `]); From 91e00114334c9c799bbb09e5160e502b2cb9505c Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:45:57 +0200 Subject: [PATCH 07/21] Added typings for threaded function Signed-off-by: duart38 --- Thread.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Thread.ts b/Thread.ts index e6705ae..94b7a9e 100644 --- a/Thread.ts +++ b/Thread.ts @@ -1,4 +1,4 @@ -export default class Thread { +export default class Thread { public worker: Promise; private imports: Array; private blob: Promise; @@ -13,7 +13,7 @@ export default class Thread { * @param imports Modules to import in the worker. only JS files allowed (over the net import allowed) */ constructor( - operation: (e: MessageEvent, globalObject?:{}) => T | Promise, + operation: (e: MessageEvent, globalObject?:{}) => T | Promise, type?: "classic" | "module", imports?: Array, ) { From aefacbf4401f007396095e3d65247495f768dada Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:46:10 +0200 Subject: [PATCH 08/21] silenced vscode Signed-off-by: duart38 --- test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.ts b/test.ts index e6f2c37..478c377 100644 --- a/test.ts +++ b/test.ts @@ -97,7 +97,7 @@ Deno.test("Over the network file imports work", async () => { Deno.test("Worker has global object", async () => { let run = new Promise<{} | undefined>((resolve) => { - let t = new Thread((e, glob) => glob, "module"); + let t = new Thread((_, glob) => glob, "module"); t.onMessage((n) => { t.remove()?.then(() => resolve(n)); }); From fd17fa2e4845124cc167582123f64508cc897c21 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:46:26 +0200 Subject: [PATCH 09/21] updated example to include new type Signed-off-by: duart38 --- examples/example_simple.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example_simple.ts b/examples/example_simple.ts index 6043b1c..6b7cca0 100644 --- a/examples/example_simple.ts +++ b/examples/example_simple.ts @@ -1,6 +1,6 @@ import Thread from "../Thread.ts"; -const thread = new Thread((e: MessageEvent) => { +const thread = new Thread((e) => { console.log("Worker: Message received from main script"); const result = e.data[0] * e.data[1]; if (isNaN(result)) { From 91541e7edc0c080811976f1a17c1d5a338c062b3 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:47:50 +0200 Subject: [PATCH 10/21] updated example to include new type Signed-off-by: duart38 --- examples/example_deno_worker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example_deno_worker.ts b/examples/example_deno_worker.ts index 489ef40..1fc9978 100644 --- a/examples/example_deno_worker.ts +++ b/examples/example_deno_worker.ts @@ -1,9 +1,9 @@ import Thread from "../Thread.ts"; import Observe from "https://raw.githubusercontent.com/duart38/Observe/master/Observe.ts"; -let tr = new Thread( +const tr = new Thread( (e) => { - let t = new Observe(e.data); // observable values + const t = new Observe(e.data); // observable values return t.getValue(); }, "module", From c01957a9a748d9142c6d0d5d4d8b8204561c2e4b Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:48:00 +0200 Subject: [PATCH 11/21] Enforcing type on postMessage Signed-off-by: duart38 --- Thread.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Thread.ts b/Thread.ts index 94b7a9e..fe12413 100644 --- a/Thread.ts +++ b/Thread.ts @@ -93,7 +93,7 @@ export default class Thread { * Sends data to the Thread * @param msg */ - public postMessage(msg: any): this { + public postMessage(msg: K): this { this.worker.then(w=>w.postMessage(msg)); return this; } From 27c60f962dc450783c9f287dc183dfbd8534ec2b Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:49:53 +0200 Subject: [PATCH 12/21] silenced vscode Signed-off-by: duart38 --- examples/example_calculateprimes.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/example_calculateprimes.ts b/examples/example_calculateprimes.ts index f8f037f..8ac8dd6 100644 --- a/examples/example_calculateprimes.ts +++ b/examples/example_calculateprimes.ts @@ -1,6 +1,6 @@ import Thread from "../Thread.ts"; -let count = 2; // number of threads to spawn +const count = 2; // number of threads to spawn function postMessage(e: any) {} // stops the compiler from complaining that the method is not available.. this gets pasted in the worker @@ -8,11 +8,11 @@ function tester() { function calculatePrimes() { const iterations = 50; const multiplier = 100000000000; - var primes = []; - for (var i = 0; i < iterations; i++) { - var candidate = i * (multiplier * Math.random()); - var isPrime = true; - for (var c = 2; c <= Math.sqrt(candidate); ++c) { + const primes = []; + for (let i = 0; i < iterations; i++) { + const candidate = i * (multiplier * Math.random()); + let isPrime = true; + for (let c = 2; c <= Math.sqrt(candidate); ++c) { if (candidate % c === 0) { // not prime isPrime = false; From 1befe88222efbcdcee7bc9f4af00ed02a604383e Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:51:19 +0200 Subject: [PATCH 13/21] added jsdoc Signed-off-by: duart38 --- Thread.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Thread.ts b/Thread.ts index fe12413..1f20b56 100644 --- a/Thread.ts +++ b/Thread.ts @@ -1,3 +1,8 @@ +/** + * > Type T -> return type + * + * > Type K -> data type of MessageEvent + */ export default class Thread { public worker: Promise; private imports: Array; From 567f22e1b506ec13db0c2dae2374e46cef575eda Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:56:35 +0200 Subject: [PATCH 14/21] silenced vscode Signed-off-by: duart38 --- test.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test.ts b/test.ts index 478c377..a113115 100644 --- a/test.ts +++ b/test.ts @@ -1,15 +1,14 @@ import { assertEquals, - assertThrows, - fail, + assertThrows } from "https://deno.land/std@0.90.0/testing/asserts.ts"; import Thread from "./Thread.ts"; import { returnNumber } from "./test_import.js"; Deno.test("incorrect file extension throws error", (): void => { assertThrows(() => { - let tr = new Thread( - (e) => { + const _tr = new Thread( + (_) => { return 1; }, "module", @@ -19,11 +18,11 @@ Deno.test("incorrect file extension throws error", (): void => { }); Deno.test("Worker takes in external function", async () => { - let run = new Promise((resolve) => { + const run = new Promise((resolve) => { function testfunc() { return 1; } - let t = new Thread(testfunc, "module"); + const t = new Thread(testfunc, "module"); t.onMessage((n) => { t.remove()?.then(() => resolve(n)); }); @@ -33,8 +32,8 @@ Deno.test("Worker takes in external function", async () => { }); Deno.test("Worker async function supported", async () => { - let run = new Promise((resolve) => { - let t = new Thread(async ()=>{ + const run = new Promise((resolve) => { + const t = new Thread(async ()=>{ await new Promise((ir) => setTimeout(ir, 1000)) return 1; }, "module"); @@ -47,8 +46,8 @@ Deno.test("Worker async function supported", async () => { }); Deno.test("Command/Method chaining works", async () => { - let run = new Promise((resolve) => { - let t = new Thread((e) => 0, "module"); + const run = new Promise((resolve) => { + const t = new Thread((_) => 0, "module"); t.onMessage((n) => { t.remove()?.then(() => resolve(n)); }); @@ -58,8 +57,8 @@ Deno.test("Command/Method chaining works", async () => { }); Deno.test("Worker returns message", async () => { - let run = new Promise((resolve) => { - let t = new Thread((e) => e.data, "module"); + const run = new Promise((resolve) => { + const t = new Thread((e) => e.data, "module"); t.onMessage((n) => { t.remove()?.then(() => resolve(n)); }); @@ -70,8 +69,8 @@ Deno.test("Worker returns message", async () => { Deno.test("Local file imports work", async () => { - let run = new Promise((resolve) => { - let t = new Thread((e) => returnNumber(), "module", [ + const run = new Promise((resolve) => { + const t = new Thread((_) => returnNumber(), "module", [ 'import {returnNumber} from "./test_import.js"', ]); t.onMessage((n) => { @@ -83,8 +82,8 @@ Deno.test("Local file imports work", async () => { }); Deno.test("Over the network file imports work", async () => { - let run = new Promise((resolve) => { - let t = new Thread((e) => returnNumber(), "module", [ + const run = new Promise((resolve) => { + const t = new Thread((_) => returnNumber(), "module", [ 'import { returnNumber } from "https://raw.githubusercontent.com/duart38/Thread/master/test_import.js"', ]); t.onMessage((n) => { @@ -96,8 +95,9 @@ Deno.test("Over the network file imports work", async () => { }); Deno.test("Worker has global object", async () => { - let run = new Promise<{} | undefined>((resolve) => { - let t = new Thread((_, glob) => glob, "module"); + // deno-lint-ignore ban-types + const run = new Promise<{} | undefined>((resolve) => { + const t = new Thread((_, glob) => glob, "module"); t.onMessage((n) => { t.remove()?.then(() => resolve(n)); }); From 136dbbccf218f698cde2546e44994d479ceb8516 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 16:59:21 +0200 Subject: [PATCH 15/21] removed unused variable Signed-off-by: duart38 --- examples/example_allot_of_threads.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/example_allot_of_threads.ts b/examples/example_allot_of_threads.ts index 94721de..cf7a064 100644 --- a/examples/example_allot_of_threads.ts +++ b/examples/example_allot_of_threads.ts @@ -5,7 +5,6 @@ let count = 13; function postMessage(e: any) {} function tester() { - let i = 0; setInterval(() => { postMessage(0); }, 500); From 92c83181feecdabfbdb2beb3cf6f2ee79689ca04 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 17:01:21 +0200 Subject: [PATCH 16/21] add async example Signed-off-by: duart38 --- examples/example_async_support.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/example_async_support.ts diff --git a/examples/example_async_support.ts b/examples/example_async_support.ts new file mode 100644 index 0000000..26ebc54 --- /dev/null +++ b/examples/example_async_support.ts @@ -0,0 +1,26 @@ +import Thread from "../Thread.ts"; + +/** + * Thanks to @praswicaksono for the suggestion + * -> https://github.com/praswicaksono + * -> https://github.com/duart38/Thread/issues/3 + */ + +const thread = new Thread(async (e: MessageEvent) => { + console.log("Worker: Message received from main script"); + const result = e.data[0] * e.data[1]; + await new Promise((resolve) => setTimeout(resolve, 5 * 1000)) + if (isNaN(result)) { + return 0; + } else { + console.log("Worker: Posting message back to main script"); + return (result); + } +}, "module"); + +thread.onMessage((e) => { + console.log(`recived back from thread: ${e}`); +}); + +thread.postMessage([10, 12]); +thread.postMessage([10, 10]); From 63a4ff75323f2dbb0efd2a1c613d0abb06dd3649 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 17:05:52 +0200 Subject: [PATCH 17/21] Add async example to readme Signed-off-by: duart38 --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f835ad6..a78d930 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,9 @@ 3. Allows you to Thread already existing functions 5. Allows module imports inside the worker -## Example +## Examples +> See examples folder for more examples + ```typescript let thread = new Thread((e: MessageEvent)=>{ console.log('Worker: Message received from main script'); @@ -44,6 +46,22 @@ new Thread(someFunction, "module"); // thread an already existing function new Thread(someFunction, "module", ['import Something from "../some.bundle.js";']); // thread with custom importing ``` +**Async support** +```TypeScript +const thread = new Thread(async (_) => { + console.log("Worker: Message received from main script"); + // Some async logic... + await new Promise((ir) => setTimeout(ir, 2000)); + return "DONE"; +}, "module"); + +thread.onMessage((e) => { + console.log(`recived back from thread: ${e}`); +}); + +thread.postMessage(0); +``` + ## API ### Standard API From 85f3dad7e337b9f5191135589de45a54639fc695 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 17:06:25 +0200 Subject: [PATCH 18/21] Changed incorrect types usage Signed-off-by: duart38 --- examples/example_async_support.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example_async_support.ts b/examples/example_async_support.ts index 26ebc54..4f46df9 100644 --- a/examples/example_async_support.ts +++ b/examples/example_async_support.ts @@ -6,7 +6,7 @@ import Thread from "../Thread.ts"; * -> https://github.com/duart38/Thread/issues/3 */ -const thread = new Thread(async (e: MessageEvent) => { +const thread = new Thread(async (e) => { console.log("Worker: Message received from main script"); const result = e.data[0] * e.data[1]; await new Promise((resolve) => setTimeout(resolve, 5 * 1000)) From 6016519d48f2ddc868fd6d7ffc11485f972e5cda Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 17:07:43 +0200 Subject: [PATCH 19/21] silenced vscode Signed-off-by: duart38 --- Thread.ts | 3 ++- examples/example_allot_of_threads.ts | 6 +++--- examples/example_calculateprimes.ts | 2 +- examples/example_importing.ts | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Thread.ts b/Thread.ts index 1f20b56..7c86bcf 100644 --- a/Thread.ts +++ b/Thread.ts @@ -18,7 +18,7 @@ export default class Thread { * @param imports Modules to import in the worker. only JS files allowed (over the net import allowed) */ constructor( - operation: (e: MessageEvent, globalObject?:{}) => T | Promise, + operation: (e: MessageEvent, globalObject?: Record) => T | Promise, type?: "classic" | "module", imports?: Array, ) { @@ -42,6 +42,7 @@ export default class Thread { ); } + // deno-lint-ignore ban-types private async populateFile(code: Function) { const imported = this.imports?.flatMap(async (val) => (await this.copyDep(val)).join("\n")); return new Blob([` diff --git a/examples/example_allot_of_threads.ts b/examples/example_allot_of_threads.ts index cf7a064..dcef548 100644 --- a/examples/example_allot_of_threads.ts +++ b/examples/example_allot_of_threads.ts @@ -1,8 +1,8 @@ import Thread from "../Thread.ts"; -let count = 13; +const count = 13; -function postMessage(e: any) {} +function postMessage(_e: unknown) {} function tester() { setInterval(() => { @@ -13,6 +13,6 @@ function tester() { } for (let i = 0; i < count; i++) { - new Thread(tester, "module").onMessage((d) => console.log(`thread -> ${i}`)) + new Thread(tester, "module").onMessage((_) => console.log(`thread -> ${i}`)) .postMessage(0); } diff --git a/examples/example_calculateprimes.ts b/examples/example_calculateprimes.ts index 8ac8dd6..a2fdc46 100644 --- a/examples/example_calculateprimes.ts +++ b/examples/example_calculateprimes.ts @@ -2,7 +2,7 @@ import Thread from "../Thread.ts"; const count = 2; // number of threads to spawn -function postMessage(e: any) {} // stops the compiler from complaining that the method is not available.. this gets pasted in the worker +function postMessage(_e: unknown) {} // stops the compiler from complaining that the method is not available.. this gets pasted in the worker function tester() { function calculatePrimes() { diff --git a/examples/example_importing.ts b/examples/example_importing.ts index 896e5bc..a177d97 100644 --- a/examples/example_importing.ts +++ b/examples/example_importing.ts @@ -1,8 +1,8 @@ import Thread from "../Thread.ts"; import { CallMe } from "../test_import.js"; -let tr = new Thread( - (e) => { +const tr = new Thread( + (_e) => { CallMe(); return "pong"; }, From 1eb78d1d8dcd5e2e3dab27e0de8e6c02e46d0fbb Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 17:11:26 +0200 Subject: [PATCH 20/21] bundled Signed-off-by: duart38 --- Thread.bundle.js | 51 ++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/Thread.bundle.js b/Thread.bundle.js index 5da8139..152f5cf 100644 --- a/Thread.bundle.js +++ b/Thread.bundle.js @@ -1,10 +1,14 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + class Thread { worker; imports; blob; blobURL = ""; stopped = false; - constructor(operation, type1, imports){ + constructor(operation, type, imports){ imports?.forEach((v)=>{ if (v.endsWith(".ts'") || v.endsWith('.ts"')) { throw new Error("Threaded imports do no support typescript files"); @@ -12,7 +16,7 @@ class Thread { }); this.imports = imports || []; this.blob = this.populateFile(operation); - this.worker = this.makeWorker(type1); + this.worker = this.makeWorker(type); } async makeWorker(type) { this.blobURL = URL.createObjectURL(await this.blob); @@ -21,39 +25,45 @@ class Thread { }); } async populateFile(code) { - let imported = this.imports?.flatMap(async (val)=>(await this.copyDep(val)).join("\n") - ); + const imported = this.imports?.flatMap(async (val)=>(await this.copyDep(val)).join("\n")); return new Blob([ - `\n ${(await Promise.all(imported)).join("\n")}\n \n var global = {};\n var userCode = ${code.toString()}\n \n onmessage = function(e) {\n postMessage(userCode(e, global));\n }\n \n ` + ` + ${(await Promise.all(imported)).join("\n")} + + var global = {}; + var userCode = ${code.toString()} + + onmessage = async function(e) { + postMessage(await userCode(e, global)); + } + + ` ]); } async copyDep(str) { - var importPathRegex = /('|"|`)(.+\.js)(\1)/ig; - var importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; - var matchedPath = importPathRegex.exec(str) || ""; - var file = false; - var fqfn = ""; + const importPathRegex = /('|"|`)(.+\.js)(\1)/ig; + const importInsRegex = /(import( |))({.+}|.+)(from( |))/ig; + const matchedPath = importPathRegex.exec(str) || ""; + let file = false; + let fqfn = ""; if (!matchedPath[0].includes("http://") && !matchedPath[0].includes("https://")) { file = true; fqfn = matchedPath[0].replaceAll(/('|"|`)/ig, ""); } - var matchedIns = importInsRegex.exec(str) || ""; + const matchedIns = importInsRegex.exec(str) || ""; if (!matchedIns) { throw new Error("The import instruction seems to be unreadable try formatting it, for example: \n" + "import { something } from './somet.js' \n "); } if (file) { - let x = await import(fqfn); - return Object.keys(x).map((v)=>x[v].toString() - ); + const x = await import(fqfn); + return Object.keys(x).map((v)=>x[v].toString()); } else { - let x = await import(matchedPath[0].replaceAll(/'|"/g, "")); - return Object.keys(x).map((v)=>x[v].toString() - ); + const x1 = await import(matchedPath[0].replaceAll(/'|"/g, "")); + return Object.keys(x1).map((v)=>x1[v].toString()); } } postMessage(msg) { - this.worker.then((w)=>w.postMessage(msg) - ); + this.worker.then((w)=>w.postMessage(msg)); return this; } async stop() { @@ -65,8 +75,7 @@ class Thread { URL.revokeObjectURL(this.blobURL); } onMessage(callback) { - this.worker.then((w)=>w.onmessage = (e)=>callback(e.data) - ); + this.worker.then((w)=>w.onmessage = (e)=>callback(e.data)); return this; } } From 7b5cb65bc11418fef99a87da48b23a4363d242d5 Mon Sep 17 00:00:00 2001 From: duart38 Date: Sun, 11 Sep 2022 17:12:50 +0200 Subject: [PATCH 21/21] update Signed-off-by: duart38 --- egg.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/egg.json b/egg.json index 6994d88..f411f6e 100644 --- a/egg.json +++ b/egg.json @@ -4,8 +4,8 @@ "stable": true, "homepage": "https://github.com/duart38/Thread", "entry": "./Thread.ts", - "version": "3.0.0", - "releaseType": "patch", + "version": "4.0.0", + "releaseType": "major", "files": [ "./LICENSE", "./README.md",