Skip to content

Commit df872d6

Browse files
committed
Test BlobWorker in browser
1 parent 79f0e7b commit df872d6

7 files changed

+61
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.cache/
22
.vscode/
3+
bundle/
34
docs/_site
45
docs/vendor
56
dist/*

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
"build": "run-p build:cjs build:es",
1616
"build:cjs": "tsc -p tsconfig.json",
1717
"build:es": "tsc -p tsconfig-esm.json",
18+
"postbuild": "run-s bundle",
19+
"bundle": "rollup -c -f umd --file=bundle/worker.js --name=threads --silent -- dist-esm/worker/bundle-entry.js",
1820
"test": "run-s test:ava test:puppeteer:basic test:puppeteer:webpack",
1921
"test:ava": "cross-env TS_NODE_FILES=true ava",
20-
"test:puppeteer:basic": "puppet-run --plugin=mocha --bundle=./test/workers/:/workers/ ./test/*.chromium*.ts",
22+
"test:puppeteer:basic": "puppet-run --plugin=mocha --bundle=./test/workers/:/workers/ --serve=./bundle/worker.js:/worker.js ./test/*.chromium*.ts",
2123
"test:puppeteer:webpack": "puppet-run --serve ./test/webpack/dist.web/0.worker.js --serve ./test/webpack/dist.web/1.worker.js --plugin=mocha ./test/webpack/webpack.chromium.mocha.ts",
2224
"posttest": "tslint --project .",
2325
"prepare": "run-s build"
@@ -79,7 +81,7 @@
7981
"ts-node": "^8.1.0",
8082
"tslint": "^5.16.0",
8183
"tslint-config-prettier": "^1.18.0",
82-
"typescript": "^3.4.5",
84+
"typescript": "^3.9.3",
8385
"wavy": "^1.0.4",
8486
"webpack": "^4.32.2",
8587
"worker-plugin": "^3.1.0"

rollup.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const commonjs = require("rollup-plugin-commonjs")
2+
const nodeResolve = require("rollup-plugin-node-resolve")
3+
4+
module.exports = {
5+
plugins: [
6+
nodeResolve({
7+
browser: true,
8+
mainFields: ["module", "main"],
9+
preferBuiltins: true
10+
}),
11+
12+
commonjs()
13+
]
14+
};

src/worker/bundle-entry.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expose } from "./index"
2+
export * from "./index"
3+
4+
if (typeof global !== "undefined") {
5+
(global as any).expose = expose
6+
}
7+
if (typeof self !== "undefined") {
8+
(self as any).expose = expose
9+
}

test/spawn.chromium.mocha.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/*
2+
* This code here will be run in a headless Chromium browser using `puppet-run`.
3+
* Check the package.json scripts `test:puppeteer:*`.
4+
*/
5+
16
import { expect } from "chai"
2-
import { spawn, Thread } from "../"
7+
import { spawn, BlobWorker, Thread } from "../"
38

49
// We need this as a work-around to make our threads Worker global, since
5-
// the Parcel bundler would otherwise not recognize `new Worker()` as a web worker
10+
// the bundler would otherwise not recognize `new Worker()` as a web worker
611
import "../src/master/register"
712

813
describe("threads in browser", function() {
@@ -20,4 +25,22 @@ describe("threads in browser", function() {
2025
await Thread.terminate(increment)
2126
})
2227

28+
it("can spawn and use a blob worker", async function() {
29+
const baseUrl = new URL(window.location.href).origin
30+
const workerSource = `
31+
// Makes expose() available on global scope
32+
importScripts(${JSON.stringify(baseUrl + "/worker.js")})
33+
34+
let counter = 0
35+
36+
expose(function() {
37+
return ++counter
38+
})
39+
`
40+
const increment = await spawn<() => number>(BlobWorker.fromText(workerSource))
41+
expect(await increment()).to.equal(1)
42+
expect(await increment()).to.equal(2)
43+
expect(await increment()).to.equal(3)
44+
await Thread.terminate(increment)
45+
})
2346
})

test/transferables.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ function replaceArrayBufferWithPlaceholder<In extends any>(obj: In, arrayBuffer:
2121
} else if (Array.isArray(obj)) {
2222
return (obj as any[]).map(element => replaceArrayBufferWithPlaceholder(element, arrayBuffer)) as any
2323
} else if (obj && typeof obj === "object") {
24-
const result: typeof obj = Object.create(Object.getPrototypeOf(obj))
24+
const result: In = Object.create(Object.getPrototypeOf(obj))
2525

2626
for (const key of Object.getOwnPropertyNames(obj)) {
27-
result[key] = replaceArrayBufferWithPlaceholder(obj[key], arrayBuffer)
27+
(result as any)[key] = replaceArrayBufferWithPlaceholder((obj as any)[key], arrayBuffer)
2828
}
2929
return result as any
3030
} else {

0 commit comments

Comments
 (0)