From 4ae0f7880ac08208144500ed3c0f0dd4c58a11a3 Mon Sep 17 00:00:00 2001 From: sheverniskiy Date: Sun, 26 Mar 2023 20:19:07 +0300 Subject: [PATCH 1/2] test fails (ts), but works with example --- mod.ts | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/mod.ts b/mod.ts index 2469af3..7511280 100644 --- a/mod.ts +++ b/mod.ts @@ -85,10 +85,7 @@ export const parry: Parry = ( ]; } = {}; - const worker = new Worker( - join(dirname(import.meta.url), "worker.js"), // Allows the Worker to be run both locally and imported from an URL - { type: "module", deno }, - ); + const worker = new Worker(URL.createObjectURL(new Blob([`(${expose.toString()})()`])), { type: "module" }); worker.onmessage = (event) => { const { type, id, data } = event.data; // All parry Worker messages use this object structure @@ -227,4 +224,68 @@ parry.close = (): void => { } }; +function expose() { + let __func = () => {}; + self.onmessage = (event) => { + const { type, id, data } = event.data; + switch (type) { + case "set": + __func = new Function(`return ${data};`)(); + break; + case "call": { + Promise.resolve(data) + .then((v) => __func.apply(null, v)) + .then( + (resolved) => + self.postMessage({ + type: "resolve", + id, + data: resolved, + }), + (rejected) => + self.postMessage({ + type: "reject", + id, + data: rejected, + }), + ); + break; + } + case "run": { + const r = new Function(`return ${data.func};`)(); + Promise.resolve(data.params) + .then((v) => r.apply(null, v)) + .then( + (resolved) => { + self.postMessage({ + type: "resolve", + id, + data: resolved, + }); + }, + (rejected) => + self.postMessage({ + type: "reject", + id, + data: rejected, + }), + ); + break; + } + case "declare": + self[data.ident] = data.value; + break; + case "define": + self[data.ident] = new Function(`return ${data.func};`)(); + break; + default: + self.postMessage({ + type: "error", + data: "Unknown message type", + }); + break; + } + }; +} + export default parry; From c81a4ea32d431a03ef9448e40a7a7b13001e8c6f Mon Sep 17 00:00:00 2001 From: sheverniskiy Date: Sun, 26 Mar 2023 20:28:04 +0300 Subject: [PATCH 2/2] don't need things anymore --- mod.ts | 2 -- worker.js | 61 ------------------------------------------------------- 2 files changed, 63 deletions(-) delete mode 100644 worker.js diff --git a/mod.ts b/mod.ts index 7511280..f0f29a5 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,3 @@ -import { dirname, join } from "https://deno.land/std@0.84.0/path/mod.ts"; - type AsyncFunction = (...params: S) => Promise; type MaybeAsyncFunction = ( ...params: S diff --git a/worker.js b/worker.js deleted file mode 100644 index ccee873..0000000 --- a/worker.js +++ /dev/null @@ -1,61 +0,0 @@ -let __func = () => {}; - -onmessage = function (event) { - const { type, id, data } = event.data; - switch (type) { - case "set": - __func = new Function(`return ${data};`)(); - break; - case "call": - Promise.resolve(data) - .then((v) => __func.apply(null, v)) - .then( - (resolved) => - self.postMessage({ - type: "resolve", - id, - data: resolved, - }), - (rejected) => - self.postMessage({ - type: "reject", - id, - data: rejected, - }), - ); - break; - case "run": - const r = new Function(`return ${data.func};`)(); - - Promise.resolve(data.params) - .then((v) => r.apply(null, v)) - .then( - (resolved) => { - self.postMessage({ - type: "resolve", - id, - data: resolved, - }); - }, - (rejected) => - self.postMessage({ - type: "reject", - id, - data: rejected, - }), - ); - break; - case "declare": - self[data.ident] = data.value; - break; - case "define": - self[data.ident] = new Function(`return ${data.func};`)(); - break; - default: - self.postMessage({ - type: "error", - data: "Unknown message type", - }); - break; - } -};