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

missing transfer argument for postMessage #7021

Closed
nabezokodaikon opened this issue Aug 11, 2020 · 4 comments
Closed

missing transfer argument for postMessage #7021

nabezokodaikon opened this issue Aug 11, 2020 · 4 comments

Comments

@nabezokodaikon
Copy link

postMessage from worker fails.
How can I get it to work properly?

main.ts

const worker = new Worker(new URL('worker.ts', import.meta.url).href, {
  type: 'module',
  deno: true
});

worker.postMessage('foo');

worker.ts

self.onmessage = async (e: any) => {
  console.log(e.data);
  self.postMessage('bar', '*'); // <-- Error
};

Running command
bash $ deno run --unstable --allow-read main.ts

Error message

error: Uncaught Error: TS2554 [ERROR]: Expected 1 arguments, but got 2.
  self.postMessage(message, '*');
                            ~~~
@lucacasonato
Copy link
Member

lucacasonato commented Aug 11, 2020

That argument is used to zero-copy objects that implement the transferable interface (ArrayBuffer and MessagePort). Deno does not support structured cloning yet (which is a prerequisite for Transferrable), and thus does not support the optional second parameter on postMessage yet. Using a string as the second parameter to postMessage is not valid anyway because it is not Transferrable.

Ref https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage and https://developer.mozilla.org/en-US/docs/Web/API/Transferable

@nabezokodaikon
Copy link
Author

Thank you. understood.

@lucacasonato lucacasonato changed the title postMessage from worker fails. missing transfer argument for postMessage Aug 11, 2020
@kitsonk
Copy link
Contributor

kitsonk commented Aug 11, 2020

Ref #3557

@bartlomieju
Copy link
Member

Closing in favor of #3557

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants