postMessage used in README before it's explained
jakearchibald opened this issue · 2 comments
postMessage
ing a module appears in an example in the "HTML Integration" section, before the concept of postMessage
ing modules is introduced.
Anything specifically you’d like to see here? It’s mentioned in the HTML Integration section, so it’s referring to postMessage as defined in HTML.
Sorry I should have provided more detail:
Let's say I knew nothing about this proposal, and read the README from top to bottom. I get to this section:
HTML Integration
Module blocks can be turned into an Object URL using URL.createObjectURL(moduleBlock)
for backwards-compatibility and polyfillability. Maybe it even makes sense to allow stringification via toString()
. Importing a module block’s object URL or the module block directly returns a reference to the same module from the module cache:
const module = module { export default 42; }
const moduleURL = URL.createObjectURL(module);
assert(await import(module) === await import(moduleURL));
import.meta
is inherited from the module the module block is syntactically located in. This is especially useful (if not essential) to make module blocks and the relative paths contained within behave as expected once they are shared across realms (e.g. sent to a worker):
// main.js
const module = module {
export async function main(url) {
return import.meta.url;
}
}
const worker = new Worker("./module-executor.js");
worker.postMessage(module);
worker.onmessage = ({data}) => assert(data == import.meta.url);
// module-executor.js
addEventListener("message", async ({data}) => {
const {main} = await import(data);
postMessage(await main());
});
worker.postMessage(module);
is used as an example of how import.meta.url
should behave, but this is the first time the idea of post-messaging modules is introduced. The "Use with workers" section that explains this is a feature comes later.
I guess "Use with workers" should appear before examples that use post-messaging incidentally.
Or, make the point about import.meta.url
without using postMessage
somehow.
Additionally, the stuff about import.meta.url
should probably be outside of "HTML integration", since it (and workers) are also available in Node.