diff --git a/.gitignore b/.gitignore index 1a8d88c..dc8ea6e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ readme-draft.de.md webspree_session.json built/ example-node/sort_worka.js +sort_worka.js \ No newline at end of file diff --git a/changelog.md b/changelog.md index cbae8de..30089d8 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # Changelog +## 10.0.0 + + * Worker is a module by default. + * Works with Deno. + ## 9.1.0 diff --git a/example-deno/deno_raw_worker_test.js b/example-deno/deno_raw_worker_test.js new file mode 100644 index 0000000..95bdc81 --- /dev/null +++ b/example-deno/deno_raw_worker_test.js @@ -0,0 +1,12 @@ +import makeloc from 'https://cdn.deno.land/dirname/versions/1.1.2/raw/mod.ts'//https://x.nest.land/dirname@v1.1.2/mod.ts' + + +const { __dirname, __filename } = makeloc(import.meta); + + +console.log(`${__dirname}w.ts`.substr(1)); + +const tsWorker = new Worker(`file:///${`${__dirname}w.ts`.substr(1)}`, { + type: "module", +}); +tsWorker.postMessage("Hello World"); diff --git a/example-deno/file_usage.js b/example-deno/file_usage.js index f7785ab..dd0f646 100644 --- a/example-deno/file_usage.js +++ b/example-deno/file_usage.js @@ -2,19 +2,17 @@ import { registerWorker, FILE, NO_SUPPORT_ERROR, TIME_OUT_ERROR, work } from ".. import makeloc from 'https://cdn.deno.land/dirname/versions/1.1.2/raw/mod.ts'//https://x.nest.land/dirname@v1.1.2/mod.ts' -const { __dirname, __filename } = makeloc(import.meta) +const { __dirname, __filename } = makeloc(import.meta); -console.log(`${__dirname}/sort_worka.js`.substr(1)); -console.log(`${__dirname}/w.ts`.substr(1)); - -const tsWorker = new Worker(`${__dirname}/w.ts`.substr(1)); -tsWorker.postMessage("Hello World"); +// console.log(`${__dirname}sort_worka.js`.substr(1)); registerWorker({ name: `sort`, - resource: `${__dirname}/sort_worka.js`.substr(1), - loadMode: FILE + resource: `file:///${`${__dirname}..\\sort_worka.js`.substr(1)}`, + loadMode: FILE, + hope: 0, + timeout: 100, }); work({ name: `sort`, input: [1, 2, 3, -8, -5, 2, 3, 45, 5] }).then(function (result) { diff --git a/example-deno/readme.md b/example-deno/readme.md index 40f148f..9a9d2c0 100644 --- a/example-deno/readme.md +++ b/example-deno/readme.md @@ -1,7 +1,3 @@ # use worka in Deno -**Warning** Does not work - -- self is not defined - -https://github.com/denoland/deno/issues/2639 +see file_usage.js diff --git a/example-deno/sort_worka.js b/example-deno/sort_worka.js deleted file mode 100644 index dfe6d04..0000000 --- a/example-deno/sort_worka.js +++ /dev/null @@ -1,27 +0,0 @@ -"use strict"; -self.addEventListener(`error`, function (errorEvent) { - errorEvent.preventDefault(); - let asString; - if (errorEvent.message) { - asString = errorEvent.message; - } else { - asString = String(errorEvent); - } - self.postMessage({ - error: asString, - }); -}); -const doWork = function sort(array) { - array.sort(); // run time error will trigger the catch - return array; -}; -self.addEventListener(`message`, function (event) { - const message = event.data; - if (!Object.prototype.hasOwnProperty.call(message, `input`)) { - return; // only waking up - } - const {input} = message; - self.postMessage({ - result: doWork(input), - }); -}); diff --git a/example-deno/w.ts b/example-deno/w.ts index a5e4862..02642b1 100644 --- a/example-deno/w.ts +++ b/example-deno/w.ts @@ -1,7 +1,7 @@ onmessage = function (e): void { console.log(e.data); - postMessage(e.data); + // postMessage(e.data); - workerClose(); + // workerClose(); }; \ No newline at end of file diff --git a/example-node/sort.js b/example-node/sort.js index 18a7a0d..a133de3 100644 --- a/example-node/sort.js +++ b/example-node/sort.js @@ -1,4 +1,6 @@ -const sort = (array) => { - array.sort(); +(array) => { + array.sort(function (a, b) { + return a-b; + }); return array; }; diff --git a/package.json b/package.json index 92b77db..7fe0c29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "worka", - "version": "9.1.1", + "version": "10.0.0", "description": "web workers results in promises", "license": "BSL-1.0", "type": "module", @@ -14,8 +14,9 @@ "prepublishOnly": "npm run bundle", "try": "node --experimental-modules example/index.js 8080", "prepare-node-example": "node --experimental-modules source/decorateWorker.js example-node/sort.js example-node/sort_worka.js", + "prepare-deno-example": "node --experimental-modules source/decorateWorker.js example-node/sort.js sort_worka.js", "node-example": "node --experimental-modules example-node/file_usage.js", - "deno-example": "deno run example-deno/file_usage.js" + "deno-example": "deno run --allow-read example-deno/file_usage.js " }, "dependencies": {}, "devDependencies": { diff --git a/readme.md b/readme.md index a0255de..bd26552 100644 --- a/readme.md +++ b/readme.md @@ -101,6 +101,13 @@ import { TIME_OUT_ERROR, } from "worka"; +// deno +import { + registerWorker, + work, + // ... +} from "https://unpkg.com/worka/source/worka.js"; + // node11- const {registerWorker, work, ...} = require("worka/built/worka.cjs"); ``` @@ -137,7 +144,8 @@ Describes the worker. Example: max: navigator.hardwareConcurrency || 1, stateless: true, initialize: false, - timeOut: false + timeOut: false, + type: `module` || `classic`, } ``` diff --git a/source/worka.js b/source/worka.js index 90a61eb..14dd559 100644 --- a/source/worka.js +++ b/source/worka.js @@ -63,6 +63,7 @@ const WORKER_DEFAULT_OPTIONS = { stateless: true, initialize: false, timeOut: false, + type: `module`, }; // impossible to accidentally overwrite @@ -164,7 +165,9 @@ self.addEventListener(\`message\`, function(event) { const instanciateWorker = function (worker) { worker.instanciated = true; if (worker.loadMode === FILE) { - worker.instance = new Worker(worker.resource); + worker.instance = new Worker(worker.resource, { + type: worker.type, + }); return; } let { workerObjectURL } = worker; @@ -173,7 +176,9 @@ const instanciateWorker = function (worker) { const workerBlob = new Blob([decoratedAsString], JS_MIME); workerObjectURL = URL.createObjectURL(workerBlob); } - worker.instance = new Worker(workerObjectURL); + worker.instance = new Worker(workerObjectURL, { + type: worker.type, + }); if (worker.hope > 5 || worker.hope < 1) { // remove for debugging URL.revokeObjectURL(workerObjectURL);