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

Is overriding protection for .off appropriate? #17844

Closed
Ulmanb opened this issue Dec 23, 2017 · 10 comments
Closed

Is overriding protection for .off appropriate? #17844

Ulmanb opened this issue Dec 23, 2017 · 10 comments
Labels
events Issues and PRs related to the events subsystem / EventEmitter. question Issues that look for answers.

Comments

@Ulmanb
Copy link
Contributor

Ulmanb commented Dec 23, 2017

Hey, Follow up on #17156 ,
Do you think a PR on the matter of overriding protection for .off is appropriate, and if so, what scenarios should be regarded?

@addaleax addaleax added events Issues and PRs related to the events subsystem / EventEmitter. question Issues that look for answers. labels Dec 23, 2017
@addaleax
Copy link
Member

What would the motivation for forbidding overriding be?

@benjamingr
Copy link
Member

Copying comment by @ChALkeR in this comment:

Everyone, do you think if an attempt to override it should warn users? Such overriding would mean one of two things:

  1. It's being overriden with an exact same alias. This case is fixeable in userland with a simple check in the lib that tries to set the alias.
  2. It's being overriden with something else — e.g. a variable or a method which does other things. This case means that something is either broken or is probable to be broken — e.g. (in variable case) if userland code later checks for the .off variable and treats it as a flag, or (in different method case) if some userland code (e.g. different libs) expect different behaviour from .off — Node.js one and overriden one.

See the rest of the discussion there. Pinging people who weighed in @ChALkeR @ljharb @BridgeAR

@ljharb
Copy link
Member

ljharb commented Dec 24, 2017

I think a simple warning when .off is overridden with anything besides removeEventListener (the current value) should suffice. When it’s overridden with the correct value, i think it’s fine and no warning is needed.

@addaleax
Copy link
Member

@benjamingr It isn’t quite clear from that comment, but I think that means what you are talking about is .off being set as a property on an EventEmitter instance, not overriding EventEmitter.prototype.off itself (which is what I thought when I read “overriding”)… is that correct?

@ljharb
Copy link
Member

ljharb commented Dec 24, 2017

My interpretation was about monkeypatching the prototype; it’s perfectly fine for someone to do anything they like to the instance.

@addaleax
Copy link
Member

It’s weird, but my thinking would be reversed I guess? Maybe there’s something special about off that I’m not seeing, but generally, monkeypatching seems like something that should be allowed?

Since the API here is well-defined, it should be safe for userland to override it as long as they know they are overriding it, right?

I can see how people might be surprised to find out that this.off = false; would start to override an EventEmitter method, so a warning in that case seems fine?

@ljharb
Copy link
Member

ljharb commented Dec 24, 2017

Shadowing a prototype method on an instance only risks surprising/breaking the tiny segment of code that interacts with it, which will likely be located nearby. Monkeypatching a prototype affects all code in the entire process.

@TimothyGu
Copy link
Member

I’m okay with making off a setter that’ll warn if the prototype’s method gets overridden.

lev-kazakov pushed a commit to lev-kazakov/node that referenced this issue Dec 26, 2017
This includes three npm releases, one of which is pretty big (5.4.0).

Changes of Note

* Significant performance boost for installations from cache (~10%+)
* A number of bugfixes for important and severe bugs affecting
  installation on all supported platforms, but particularly noticeable
  on Windows.
* More Windows-related fixes for `npx`.
* [nodejs#17844](npm/npm#17844)
  Make package-lock.json sorting locale-agnostic. This will cause
  some users to see seemingly spurious diffs on their pkglocks if
  they were using previous versions of npm5, because, for example,
  `JSONStream` is sorted into a different location. This is fine and
  will go away as people upgrade.

Changelogs

* [`v5.4.0`](https://github.com/npm/npm/releases/tag/v5.4.0)
* [`v5.4.1`](https://github.com/npm/npm/releases/tag/v5.4.1)
* [`v5.4.2`](https://github.com/npm/npm/releases/tag/v5.4.2)
@mcollina
Copy link
Member

mcollina commented Jan 3, 2018

I'm -1 in adding override protection or emitting a warning. I think it's good as it is.

@bnoordhuis
Copy link
Member

What is the conclusion here? Can this issue be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

7 participants