-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
a53d46e
commit 998d8dd
Showing
10 changed files
with
186 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { baseUrl as pageBaseUrl, esModuleShimsSrc, createBlob } from './common.js'; | ||
|
||
export class WorkerShim { | ||
constructor(aURL, options = {type: 'classic'}) { | ||
if (options.type !== 'module') | ||
return new Worker(aURL, options); | ||
|
||
if (!esModuleShimsSrc) | ||
throw new Error('es-module-shims.js must be loaded with a script tag for WorkerShim support.'); | ||
|
||
const workerScriptUrl = createBlob( | ||
`importScripts('${esModuleShimsSrc}'); self.importMap = ${JSON.stringify(options.importMap || {})}; importShim('${new URL(aURL, pageBaseUrl).href}')`); | ||
|
||
const workerOptions = {...options}; | ||
workerOptions.type = 'classic'; | ||
|
||
return new Worker(workerScriptUrl, workerOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
self.postMessage('classic'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import test from "test"; | ||
|
||
self.postMessage(test); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!doctype html> | ||
<link rel="stylesheet" type="text/css" href="../node_modules/mocha/mocha.css"/> | ||
<script src="../node_modules/mocha/mocha.js"></script> | ||
<script type="importmap-shim"> | ||
{ | ||
"imports": { | ||
"test": "/test/fixtures/es-modules/es6-file.js", | ||
"test/": "/test/fixtures/", | ||
"example": "/test/fixtures/wasm/example.js" | ||
}, | ||
"scopes": { | ||
"/": { | ||
"test-dep": "/test/fixtures/test-dep.js" | ||
} | ||
} | ||
} | ||
</script> | ||
<script type="text/javascript" src="../dist/es-module-shims.js"></script> | ||
<script type="module"> | ||
mocha.setup('tdd'); | ||
mocha.allowUncaught(); | ||
|
||
self.baseURL = location.href.substr(0, location.href.lastIndexOf('/') + 1); | ||
self.rootURL = location.href.substr(0, location.href.length - 'test/test-worker.html'.length); | ||
self.assert = function (val) { | ||
equal(!!val, true); | ||
}; | ||
assert.equal = equal; | ||
assert.ok = assert; | ||
function equal (a, b) { | ||
if (a !== b) | ||
throw new Error('Expected "' + a + '" to be "' + b + '"'); | ||
} | ||
self.fail = function (msg) { | ||
throw new Error(msg); | ||
}; | ||
|
||
const suites = ['worker']; | ||
function runNextSuite () { | ||
mocha.suite.suites.shift(); | ||
const suite = suites.shift(); | ||
if (suite) | ||
importShim('./' + suite + '.js') | ||
.then(function () { | ||
mocha.run(runNextSuite); | ||
}); | ||
} | ||
|
||
runNextSuite(); | ||
</script> | ||
|
||
<div id="mocha"></div> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
suite('Worker', () => { | ||
test(`should create a worker type=classic and then receive a message containing the string 'classic'`, async () => { | ||
const worker = new WorkerShim("./fixtures/worker/clasic-worker.js", { | ||
name: 'classic-worker' | ||
}); | ||
|
||
const result = await new Promise((resolve, reject) => { | ||
// set a timeout to resolve the promise if the worker doesn't 'respond' | ||
const timeoutId = setTimeout(() => { | ||
resolve(null); | ||
}, 2000); | ||
|
||
worker.onmessage = (e) => { | ||
clearTimeout(timeoutId); | ||
|
||
resolve(e.data); | ||
}; | ||
|
||
worker.onerror = (e) => { | ||
clearTimeout(timeoutId); | ||
resolve(null); | ||
} | ||
}); | ||
|
||
assert.equal(result, 'classic'); | ||
}); | ||
|
||
test('should create worker type=module and then receive a message containing the result of a bare import', async () => { | ||
const worker = new WorkerShim("./fixtures/worker/module-worker.js", { | ||
type: 'module', | ||
name: 'test_import_map', | ||
importMap: self.importMap | ||
}); | ||
|
||
const result = await new Promise((resolve, reject) => { | ||
// set a timeout to resolve the promise if the worker doesn't 'respond' | ||
const timeoutId = setTimeout(() => { | ||
resolve(null); | ||
}, 2000); | ||
|
||
worker.onmessage = (e) => { | ||
clearTimeout(timeoutId); | ||
resolve(e.data); | ||
}; | ||
|
||
worker.onerror = (e) => { | ||
clearTimeout(timeoutId); | ||
resolve(null); | ||
} | ||
}); | ||
|
||
assert.equal(result, 4); | ||
}); | ||
}); |