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

Cannot listen to same event twice #12

Closed
sorenlouv opened this issue Jan 19, 2017 · 3 comments
Closed

Cannot listen to same event twice #12

sorenlouv opened this issue Jan 19, 2017 · 3 comments

Comments

@sorenlouv
Copy link

I am currently doing something along the lines:

postRobot.on('myEventName', function() {
	// Do something
});

// In some other part of the codebase

postRobot.on('myEventName', function() {
	// Do something else
});

This throws Request listener already exists for <eventName> on domain *.

Looking in the source, I can see it's possible to override the previous listener.

What if I want two listeners for the same event? This is necessary keep separation of concerns.

Thanks.

@bluepnume
Copy link
Collaborator

bluepnume commented Feb 1, 2017

@sqren Sorry I didn't see your comment until now. For some reason didn't have notifications on for this repo.

I'm confused as to how having multiple listeners with the same name would work.

Let's say:

  • I'm on window A.
  • I send foo message to window B.
  • Window B has 2 listeners for foo

Which listener on window B gets called?

If you're suggesting that both should be called, that also wouldn't work too well with post-robot, since the listener can also return something to the other window:

postRobot.on('myEventName', function() {
	return 5;
});

postRobot.on('myEventName', function() {
	return 10;
});

If we were to call both listeners, how would we know whether to return 5 or 10 to the other window?

It's better to think of post-robot as letting you create global functions that you can call from other windows. Like any function, you call it once, and you get a single return value (or exception).


I think, if you want multiple event listeners, and you're not interested in the return value, you can always use something like EventEmitter to broadcast the event on the target window. So:

var emitter = new EventEmitter();

postRobot.on('myEventName', function() {
	emitter.emitEvent('myEventName');
});

emitter.addListener('myEventName', function() {
	// Do something
});

emitter.addListener('myEventName', function() {
	// Do something else
});

Does that work for your use-case?

@sorenlouv
Copy link
Author

@bluepnume Now it's my time to apologise for the slow reply. On a completely unrelated node I stumbled upon this article and was like: wait? wasn't that the post-robot guy - who wrote a reply last week...

Aaaanyways :)

I went exactly the route you suggested above. Didn't think about the return values being an issue - makes sense.
Thanks.

@bluepnume
Copy link
Collaborator

Cool! Glad it worked :)

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

2 participants