Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postMessage used in README before it's explained #36

Open
jakearchibald opened this issue Feb 4, 2021 · 2 comments
Open

postMessage used in README before it's explained #36

jakearchibald opened this issue Feb 4, 2021 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@jakearchibald
Copy link
Contributor

jakearchibald commented Feb 4, 2021

postMessageing a module appears in an example in the "HTML Integration" section, before the concept of postMessageing modules is introduced.

@surma
Copy link
Member

surma commented Feb 4, 2021

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.

@jakearchibald
Copy link
Contributor Author

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.

@littledan littledan added the documentation Improvements or additions to documentation label Feb 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants