Skip to content

Is overriding protection for .off appropriate?  #17844

Description

@Ulmanb

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?

Activity

  1. added
    eventsIssues and PRs related to EventEmitter and the events module.
    questionIssues asking questions about Node.js.
    on Dec 23, 2017
  2. addaleax commented on Dec 23, 2017

    @addaleax
    Member

    What would the motivation for forbidding overriding be?

  3. benjamingr commented on Dec 24, 2017

    @benjamingr
    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

  4. ljharb commented on Dec 24, 2017

    @ljharb
    SponsorMember

    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.

  5. addaleax commented on Dec 24, 2017

    @addaleax
    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?

  6. ljharb commented on Dec 24, 2017

    @ljharb
    SponsorMember

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

  7. addaleax commented on Dec 24, 2017

    @addaleax
    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?

  8. ljharb commented on Dec 24, 2017

    @ljharb
    SponsorMember

    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.

  9. TimothyGu commented on Dec 24, 2017

    @TimothyGu
    Member

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

  10. added a commit that references this issue on Dec 26, 2017
  11. mcollina commented on Jan 3, 2018

    @mcollina
    SponsorMember

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

  12. bnoordhuis commented on Mar 13, 2018

    @bnoordhuis
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to EventEmitter and the events module.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions