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

Closing the receiver tab will not throw an error in tabs.sendMessage() #365

Open
fregante opened this issue Mar 9, 2022 · 1 comment
Open

Comments

@fregante
Copy link
Contributor

fregante commented Mar 9, 2022

Related:

From my understanding, the code above was added to silence the error Chrome throws when a message is received by the listener but not "handled" (i.e. the listener did not return a promise).

For Chrome, this is a user error: The user must call sendResponse, even without arguments.

Unfortunately the polyfill’s workaround means that I can't distinguish between:

  • the tab never handled my request (if there's any listener registered)
  • the tab closed before my request was answered

… because in both cases, the promise will resolve with undefined.

Since the solution to this problem is simple, I suggest surfacing this error instead of ignoring it. The solution for users is:

// use an async listener
browser.runtime.onMessage.addListener(async (message) => {
	// Handled, done
});

// return anything (or better, a resolved promise)
browser.runtime.onMessage.addListener((message) => {
	return Promise.resolve();
});

I understand however that this would be a breaking change, but the current situation means I can't use this polyfill to send long-running messages (e.g. send message, wait for HTTP response(2))

@fregante
Copy link
Contributor Author

fregante commented Mar 9, 2022

While looking into this, I also attempted to document some Chrome responses, which lead me to open an issue on the polyfill:

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

1 participant