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

docs,events: ee.once() event ordering differs from documentation #5566

Closed
omsmith opened this issue Mar 4, 2016 · 8 comments
Closed

docs,events: ee.once() event ordering differs from documentation #5566

omsmith opened this issue Mar 4, 2016 · 8 comments
Labels
doc Issues and PRs related to the documentations. events Issues and PRs related to the events subsystem / EventEmitter.

Comments

@omsmith
Copy link
Contributor

omsmith commented Mar 4, 2016

  • Version: v4.3.2
  • Platform: Linux osmith-ubuntu-t1700 4.2.0-30-generic docs: re-word project messaging #36-Ubuntu SMP Fri Feb 26 00:58:07 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: events, docs

Documentation describes ee.once() behaviour as (emphasis mine)

Adds a one time listener function for the event. This listener is invoked only the next time event is triggered, after which it is removed.

The actual current behaviour is for the listener to removed, and then invoked.

'use strict';

const assert = require('assert');
const EventEmitter = require('events');

const order = [];

new EventEmitter()
  .on('removeListener', () => order.push('remove'))
  .once('foo', () => order.push('invoke'))
  .emit('foo');

process.on('exit', () => assert.deepEqual(order, ['invoke', 'remove']));
assert.js:89
  throw new assert.AssertionError({
  ^
AssertionError: [ 'remove', 'invoke' ] deepEqual [ 'invoke', 'remove' ]
    at process.<anonymous> (/home/omsmith/ee-once-remove-order-test/index.js:13:33)
    at emitOne (events.js:77:13)
    at process.emit (events.js:169:7)
@omsmith omsmith changed the title docs,events: docs,events: ee.once() event ordering differs from documentation Mar 4, 2016
@mscdex mscdex added doc Issues and PRs related to the documentations. events Issues and PRs related to the events subsystem / EventEmitter. labels Mar 4, 2016
@zeusdeux
Copy link
Contributor

zeusdeux commented Mar 4, 2016

Confirmed on v5.7.0. Either we move this to after this or update the docs. Imho, the code change makes more sense as opposed to saying that listener is removed *then* called since that might cause confusion.

@omsmith
Copy link
Contributor Author

omsmith commented Mar 4, 2016

It's also worth noting this has been the behaviour since it was introduced (but I would also prefer fixing the ordering).

@MylesBorins
Copy link
Contributor

It looks like the documentation was added in 179a7f6 and has always mis documented the function.

Modifying the behavior would be a semver major change. Modifying the documentation would be a semver patch change which could be backported to v4 / v5. In reality the doc change should potentially happen even if the behavior is changed on master.

Is there an advantage to the behavior change aside from accurately following what was documented?

@zeusdeux
Copy link
Contributor

zeusdeux commented Mar 4, 2016

For one, it enforces logically correct ordering. You might want to, for example, log stuff after a handler is run for the last time just before being removed.

On 04-Mar-2016, at 19:31, Myles Borins notifications@github.com wrote:

It looks like the documentation was added in 179a7f6 and has always mis documented the function.

Modifying the behavior would be a semver major change. Modifying the documentation would be a semver patch change which could be backported to v4 / v5. In reality the doc change should potentially happen even if the behavior is changed on master.

Is there an advantage to the behavior change aside from accurately following what was documented?


Reply to this email directly or view it on GitHub.

@lance
Copy link
Member

lance commented Mar 8, 2016

I was going to submit a PR with @zeusdeux suggested change, but this ends up breaking one of the removeListener tests. Specifically, here we get 'removeListener' instead of 'hello'. This is because the function executing from the original call to once() right here has not completed and so hasn't been removed yet.

I can fix this test, but I wonder if this doesn't illustrate some potential brittleness to this change.

Update:
With the code as it is now, this.removeListener() triggers a removeListener event before the function provided to once. This ensures user-land code can call once('removeListener', ...); from within the body of the original function supplied to once(), and still not receive the 'removeListener' event that is generated by once(). By moving the call to removeListener() to here, user land code ends up receiving the removeListener event that is generated.

I tried changing the removeListener function to not issue a removeListener event here and here if type === 'removeListener', but that ends up breaking expectations here where the removeListener event is expected.

It seems to me, that unless you are willing to accept a possibly breaking change, the best course is to just change the documentation. Two node.js tests expect behavior as it is today. You can be sure user code does as well.

@omsmith
Copy link
Contributor Author

omsmith commented Apr 11, 2016

@lance @thealphanerd Just a doc update makes sense to me

@jasnell
Copy link
Member

jasnell commented Apr 19, 2016

+1 to doc update.

@jasnell
Copy link
Member

jasnell commented Apr 22, 2016

@nodejs/documentation

Fishrock123 pushed a commit that referenced this issue May 4, 2016
Addresses #5566. The `ee.once()` function is currently documented as
invoking the listener, and then removing it when the event is
triggered. However, this is not really the case. The listener is removed
and _then_ invoked. This only matters in a narrow set of use cases, but
when it matters, it matters that the docs are correct.

See the issue (#5566) for a discussion on why the code has not been
modified to match the documentation, but instead the documentation has
been modified to match the code.

Fixes: #5566
PR-URL: #6371
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
MylesBorins pushed a commit that referenced this issue Jun 2, 2016
Addresses #5566. The `ee.once()` function is currently documented as
invoking the listener, and then removing it when the event is
triggered. However, this is not really the case. The listener is removed
and _then_ invoked. This only matters in a narrow set of use cases, but
when it matters, it matters that the docs are correct.

See the issue (#5566) for a discussion on why the code has not been
modified to match the documentation, but instead the documentation has
been modified to match the code.

Fixes: #5566
Ref: #6371
PR-URL: #7103
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
MylesBorins pushed a commit that referenced this issue Jun 2, 2016
Addresses #5566. The `ee.once()` function is currently documented as
invoking the listener, and then removing it when the event is
triggered. However, this is not really the case. The listener is removed
and _then_ invoked. This only matters in a narrow set of use cases, but
when it matters, it matters that the docs are correct.

See the issue (#5566) for a discussion on why the code has not been
modified to match the documentation, but instead the documentation has
been modified to match the code.

Fixes: #5566
Ref: #6371
PR-URL: #7103
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
MylesBorins pushed a commit that referenced this issue Jun 24, 2016
Addresses #5566. The `ee.once()` function is currently documented as
invoking the listener, and then removing it when the event is
triggered. However, this is not really the case. The listener is removed
and _then_ invoked. This only matters in a narrow set of use cases, but
when it matters, it matters that the docs are correct.

See the issue (#5566) for a discussion on why the code has not been
modified to match the documentation, but instead the documentation has
been modified to match the code.

Fixes: #5566
Ref: #6371
PR-URL: #7103
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
MylesBorins pushed a commit that referenced this issue Jun 24, 2016
Addresses #5566. The `ee.once()` function is currently documented as
invoking the listener, and then removing it when the event is
triggered. However, this is not really the case. The listener is removed
and _then_ invoked. This only matters in a narrow set of use cases, but
when it matters, it matters that the docs are correct.

See the issue (#5566) for a discussion on why the code has not been
modified to match the documentation, but instead the documentation has
been modified to match the code.

Fixes: #5566
Ref: #6371
PR-URL: #7103
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
@sam-github sam-github added doc Issues and PRs related to the documentations. and removed doc Issues and PRs related to the documentations. labels Dec 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. events Issues and PRs related to the events subsystem / EventEmitter.
Projects
None yet
Development

No branches or pull requests

7 participants