Closed NoamLoewenstern closed 1 year ago
You're correct, it's just to prevent race conditions between listeners that return responses.
While the messaging APIs technically support these sorts of listeners, it's not well documented and easy to mess up and cause these race conditions, especially when there are multiple people working on an extension.webextension-polyfill
makes it even harder to figure out, and is even less documented. For that reason, I wouldn't recommend doing this with @webext-core/messaging
, webextension-polyfill
, or the chrome
APIs directly.
It seems like you want an event-based messaging framework, rather than the request-response model this library provides. I do not plan on supporting these types of listeners in @webext-core/messaging
, so you have a couple options:
Write a DIY wrapper around this library using an event based framework, like nanoevents
import { onMessage } from './messaging';
import { createNanoEvents } from 'nanoevents';
const emitter = createNanoEvents();
onMessage("some-message", ({ data }) => emitter.emit("some-message", data));
emitter.on("some-message", (data) => {
console.log("listener 1", data);
});
emitter.on("some-message", (data) => {
console.log("listener 2", data);
});
webext-bridge
geared toward event-based extensionsI would consider adding an event based messaging framework to @webext-core
, but webext-bridge
already exists and works on all browsers, so I'd just recommend using it instead.
Thanks so much! I'll have a look.
Current code doesn't allow multiple listeners in a single context: here.
The reason is understandable: If there's multiple context listening and returning a response then the invoker/sender will receive just the first result. Plus, will confuse the listeres, not realizing that multiple responses is not an accurate way of data flow...
But, I would add that sometimes what is intended in the listeners' side is solely to listen to incoming event in multiple parts in the context, without returning response. In that sense, I wouldn't understand why it shouldn't be possible.
I'd like to ask if there's something else I'm missing for the reason?