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

on('attached') registration in an on('attached') block fires immediately #364

Closed
mattheworiordan opened this issue Dec 24, 2016 · 10 comments
Labels
bug Something isn't working. It's clear that this does need to be fixed.

Comments

@mattheworiordan
Copy link
Member

Easier to show in code

channel.on('attached', () => {
  console.log('First attach');
  channel.on('attached', () => {
     console.log('Second attach');
  }
});

/* or */

channel.attach((err) => {
  console.log('First attach');
  channel.on('attached', () => {
     console.log('Second attach');
  }
});

/* or */

channel.attach((err) => {
  console.log('First attach');
  channel.once('attached', () => {
     console.log('Second attach');
  }
});

Both of these result in the following, even though there is only one attach:

=> First attach
=> Second attach

I don't think it's valid that an event handler, registered in the listener for an event should fire immediately. I think it's pretty clear that the developer wants that to happen the next time that event happens, not this time.

However, this does work as expected:

channel.once('attached', () => {
  console.log('First attach');
  channel.on('attached', () => {
     console.log('Second attach');
  }
});

=> First attach
@mattheworiordan mattheworiordan added the bug Something isn't working. It's clear that this does need to be fixed. label Dec 24, 2016
@mattheworiordan
Copy link
Member Author

mattheworiordan commented Dec 24, 2016

Should the ordering of event registrations / emitting be part of the 0.10 spec perhaps?

@SimonWoolf
Copy link
Member

This is because it's appending to an array while iterating through it. Fix would be for emit() to copy the listeners arrays before it starts. I'd be fine with a spec item to enforce that if you think it's necessary.

@mattheworiordan
Copy link
Member Author

Yup, see https://github.com/ably/ably-ruby/blob/master/lib/ably/modules/event_emitter.rb#L82 and https://github.com/ably/ably-ruby/blob/master/spec/unit/modules/event_emitter_spec.rb#L73-L101, I clone it before iterating over and emitting.

I personally think this should be part of the spec, but open to feedback

@paddybyers
Copy link
Member

The spec should say that every listener that is registered at the time of the call should called exactly once. It shouldn't say anything about cloning arrays of listeners. Other languages have other ways of meeting that requirement.

SimonWoolf added a commit that referenced this issue Jan 2, 2017
So that listeners added in listener callbacks aren't called in that
emit() call

Fixes #364
@SimonWoolf
Copy link
Member

Fixed in 0.9 by a2fb1e2

(Not planning to fix in 0.8 as it is arguably a breaking api change; let me know if you disagree)

@paddybyers
Copy link
Member

it is arguably a breaking api change

Well, I think only in the same way that any bugfix is potentially a breaking change for somebody.

@SimonWoolf
Copy link
Member

SimonWoolf commented Jan 2, 2017

Well, I think only in the same way that any bugfix is potentially a breaking change for somebody.

This isn't like a crash or a race condition; the previous behaviour was stable and predictable, so isn't it plausible that someone have built an app that relies on it (albeit probably unintentionally)?

@paddybyers
Copy link
Member

I'm say that people can come to depend on all sorts of out-of-spec behaviours, but that shouldn't stop us fixing them, in general.

In this case I think the adverse impact of leaving it is small, and the likelihood we break someone's code by fixing it is smaller still.

SimonWoolf added a commit that referenced this issue Jan 2, 2017
So that listeners added in listener callbacks aren't called in that
emit() call

Fixes #364
@SimonWoolf
Copy link
Member

OK, fair enough. Backported to master as 6f5f54a and 3b315e6

@mattheworiordan
Copy link
Member Author

FYI, PR for this now at ably/docs#240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.
Development

No branches or pull requests

3 participants