Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created July 3, 2014 10:31
Show Gist options
  • Save pvdz/f33a51dc7189d98efe23 to your computer and use it in GitHub Desktop.
Save pvdz/f33a51dc7189d98efe23 to your computer and use it in GitHub Desktop.
Right now the sub is never fired. If you uncomment the first line, both subs are fired.
// PubSub.subscribe('foo', function(name, value) {});
PubSub.publish('foo', 'bar');
PubSub.subscribe('foo', function(name, value) {});
@mroderick
Copy link

(i would expect the emitting to be queued and subscribers to be collected and evaluated at actual emit-time, rather than call)

Is your expectation that only the first subscriber be called?

@pvdz
Copy link
Author

pvdz commented Jul 3, 2014

My expectation is that .publish( only queues the event and nothing else. Then when the code/browser yields and PubSub gets a chance to fire events I expect it to look for subscribers and emit to all subscribers at that point.

Right now it seems to collect all subscribers at call time. It won't find any so the event fizzles.

That is correct, right? Or is my test setup wrong?

@mroderick
Copy link

Your test is probably correct, and I think I now understand your expectation. I think that it would be the most common expectation for this scenario and that we should update PubSubJS to match it.

@pvdz
Copy link
Author

pvdz commented Jul 3, 2014

This is similar to resolving/rejecting a promise and registering another .then or .otherwise after doing so. The guaranteed async behavior made me think pubsub would follow the same line.

The main difference is that I don't expect pubsub to fire events retroactively for events registered after a yield. Don't get me wrong there :) But if an event hasn't fired yet, it should fire for all events that are registered up until the point where it fires, even if there weren't any events at the time of calling .publish.

So basically, I suspect a (imo) bug in where subscribers are collected. I think pubsub fizzles the event because there are no subscribers for it at call time. I'm sure you can confirm/reject this :)

@pvdz
Copy link
Author

pvdz commented Jul 3, 2014

Oh. Github didn't update. Great, should I file a ticket?

@mroderick
Copy link

Yeah, a ticket would be great!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment