Skip to content

Commit

Permalink
make it work with deno
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosSacASac committed Oct 19, 2020
1 parent 9769496 commit 9a3c88a
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ readme-draft.de.md
webspree_session.json
built/
example-node/sort_worka.js
sort_worka.js
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 10.0.0

* Worker is a module by default.
* Works with Deno.


## 9.1.0

Expand Down
12 changes: 12 additions & 0 deletions example-deno/deno_raw_worker_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import makeloc from 'https://cdn.deno.land/dirname/versions/1.1.2/raw/mod.ts'//https://x.nest.land/[email protected]/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");
14 changes: 6 additions & 8 deletions example-deno/file_usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/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) {
Expand Down
6 changes: 1 addition & 5 deletions example-deno/readme.md
Original file line number Diff line number Diff line change
@@ -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
27 changes: 0 additions & 27 deletions example-deno/sort_worka.js

This file was deleted.

4 changes: 2 additions & 2 deletions example-deno/w.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
onmessage = function (e): void {
console.log(e.data);

postMessage(e.data);
// postMessage(e.data);

workerClose();
// workerClose();
};
6 changes: 4 additions & 2 deletions example-node/sort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const sort = (array) => {
array.sort();
(array) => {
array.sort(function (a, b) {
return a-b;
});
return array;
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
```
Expand Down Expand Up @@ -137,7 +144,8 @@ Describes the worker. Example:
max: navigator.hardwareConcurrency || 1,
stateless: true,
initialize: false,
timeOut: false
timeOut: false,
type: `module` || `classic`,
}
```

Expand Down
9 changes: 7 additions & 2 deletions source/worka.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const WORKER_DEFAULT_OPTIONS = {
stateless: true,
initialize: false,
timeOut: false,
type: `module`,
};

// impossible to accidentally overwrite
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 9a3c88a

Please sign in to comment.