Skip to content

Commit 29f0995

Browse files
committed
Updating coincident to its latest
1 parent 036b48c commit 29f0995

22 files changed

+250
-292
lines changed

cjs/package.json

-1
This file was deleted.

docs/index.js

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-CGWtiqjJ.js

-2
This file was deleted.

docs/zip-gl8b5xR3.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/zip-CGWtiqjJ.js.map renamed to docs/zip-gl8b5xR3.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

esm/worker/_template.js

+7-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// bigger than it used to be before any changes is applied to this file.
66

77
import * as JSON from '@ungap/structured-clone/json';
8-
import coincident from 'coincident/window';
8+
import coincident from 'coincident/window/worker';
99

1010
import { assign, create, createFunction, createOverload, createResolved, dispatch, registerJSModules } from '../utils.js';
1111
import createJSModules from './js_modules.js';
@@ -32,19 +32,23 @@ const add = (type, fn) => {
3232

3333
const { parse, stringify } = JSON;
3434

35-
const { proxy: sync, window, isWindowProxy } = coincident(self, {
35+
const { proxy: sync, sync: syncMainAndWorker, polyfill, window, isWindowProxy } = await coincident({
3636
parse,
3737
stringify,
3838
transform: value => transform ? transform(value) : value
3939
});
4040

4141
const xworker = {
42+
// propagate the fact SharedArrayBuffer is polyfilled
43+
polyfill,
4244
// allows synchronous utilities between this worker and the main thread
4345
sync,
4446
// allow access to the main thread world
4547
window,
4648
// allow introspection for foreign (main thread) refrences
4749
isWindowProxy,
50+
// propagate the fact `window` can be used
51+
hasWindow: syncMainAndWorker,
4852
// standard worker related events / features
4953
onmessage: console.info,
5054
onerror: console.error,
@@ -61,38 +65,10 @@ add('message', ({ data: { options, config: baseURL, configURL, code, hooks } })
6165

6266
const interpreter = await getRuntime(runtimeID, baseURL, configURL, config);
6367

64-
const { js_modules, sync_main_only } = configs.get(runtimeID);
68+
const { js_modules } = configs.get(runtimeID);
6569

6670
const mainModules = js_modules?.main;
6771

68-
// this flag allows interacting with the xworker.sync exposed
69-
// *only in the worker* and eventually invoked *only from main*.
70-
// If that flag is `false` or not present, then SharedArrayBuffer
71-
// must be available or not much can work in here.
72-
let syncMainAndWorker = !sync_main_only;
73-
74-
// bails out out of the box with a native/meaningful error
75-
// in case the SharedArrayBuffer is not available
76-
try {
77-
new SharedArrayBuffer(4);
78-
// if this does not throw there's no reason to
79-
// branch out of all the features ... but ...
80-
syncMainAndWorker = true;
81-
}
82-
// eslint-disable-next-line no-unused-vars
83-
catch (_) {
84-
// if it does throw and `sync_main_only` was not `true`
85-
// then there's no way to go further
86-
if (syncMainAndWorker) {
87-
throw new Error(
88-
[
89-
'Unable to use SharedArrayBuffer due insecure environment.',
90-
'Please read requirements in MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements',
91-
].join('\n'),
92-
);
93-
}
94-
}
95-
9672
const details = create(registry.get(type));
9773

9874
const resolved = createResolved(

esm/worker/class.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as JSON from '@ungap/structured-clone/json';
21
import fetch from '@webreflection/fetch';
3-
import coincident from 'coincident/window';
42
import xworker from './xworker.js';
53
import { getConfigURLAndType } from '../loader.js';
64
import { assign, create, defineProperties, importCSS, importJS } from '../utils.js';
@@ -14,6 +12,8 @@ import Hook from './hook.js';
1412
* @prop {string} [configURL] the optional configURL used to resolve config entries
1513
*/
1614

15+
// REQUIRES INTEGRATION TEST
16+
/* c8 ignore start */
1717
export default (...args) =>
1818
/**
1919
* A XWorker is a Worker facade able to bootstrap a channel with any desired interpreter.
@@ -22,7 +22,8 @@ export default (...args) =>
2222
* @returns {Worker}
2323
*/
2424
function XWorker(url, options) {
25-
const worker = xworker();
25+
const serviceWorker = options?.serviceWorker;
26+
const worker = xworker({ serviceWorker });
2627
const { postMessage } = worker;
2728
const isHook = this instanceof Hook;
2829

@@ -37,15 +38,19 @@ export default (...args) =>
3738
// fallback to a generic, ignored, config.txt file to still provide a URL.
3839
const [ config ] = getConfigURLAndType(options.config, options.configURL);
3940

40-
const bootstrap = fetch(url)
41+
let bootstrap = fetch(url)
4142
.text()
4243
.then(code => {
4344
const hooks = isHook ? this.toJSON() : void 0;
4445
postMessage.call(worker, { options, config, code, hooks });
46+
})
47+
.then(() => {
48+
// boost postMessage performance
49+
bootstrap = { then: fn => fn() };
4550
});
4651

4752
const sync = assign(
48-
coincident(worker, JSON).proxy,
53+
worker.proxy,
4954
{ importJS, importCSS },
5055
);
5156

@@ -55,10 +60,9 @@ export default (...args) =>
5560
sync: { value: sync },
5661
ready: { value: resolver.promise },
5762
postMessage: {
58-
value: (data, ...rest) =>
59-
bootstrap.then(() =>
60-
postMessage.call(worker, data, ...rest),
61-
),
63+
value: (data, ...rest) => bootstrap.then(
64+
() => postMessage.call(worker, data, ...rest),
65+
),
6266
},
6367
onerror: {
6468
writable: true,
@@ -87,3 +91,5 @@ export default (...args) =>
8791

8892
return worker;
8993
};
94+
95+
/* c8 ignore stop */

0 commit comments

Comments
 (0)