on now returns a deregistration function#215
Open
matthias-ccri wants to merge 1 commit intodevelopit:mainfrom
Open
on now returns a deregistration function#215matthias-ccri wants to merge 1 commit intodevelopit:mainfrom
on now returns a deregistration function#215matthias-ccri wants to merge 1 commit intodevelopit:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Co-authored-by: Jason Miller developit@users.noreply.github.com
What is the purpose of this pull request? (put an "X" next to an item)
What changes did you make? (Give an overview)
The
onfunction now returns a deregistration function that a consumer may call instead ofoff.This pattern is present in other event emitter libraries, and it has a particular benefit that I care about. It allows consumers to avoid creating a variable to store the handler function. In other words, the handler function can be inlined into the
onarguments. (in the current API, it is possible to inline the handler function, but then it is impossible to ever deregister it).Allowing inlining via a deregistration function is a key API feature in my experience. I have seen bad things happen in enterprise codebases that were caused by developers creating handler function variables, which then degrade over time. Once the variable exists, future developers may decide to use it for other purposes. This is a classic maintenance risk where code loses its purpose and gets corrupted by multiple concerns. Inlining would prevent that. If the variable never exists in the first place, then the code would be more resilient to degradation over time.
That's the idea at least.
Is there anything you'd like reviewers to focus on?
The deregistration function is a good pattern, but it involves creating a new object (arrow function) on each invocation of
on. I'm not well versed in JS engines but creating an object has implications for performance and GC.In typical usage I don't think this is an issue, by hypothetically there may be high performance consumers who call
onin high performance code loops, where allocating an object would degrade performance.If the creation of the object is not acceptable for this library, then we may consider implementing two different
onfunctions: one returning a deregistration function and one without (i.e. the current form). However, this would increase the library size a little.If neither of these are valid for the design decisions of this library, then please feel free to close this PR.
Does this PR introduce a breaking change? (What changes might other developers need to make in their application due to this PR?)
Yes. The
onfunction now returns a function instead ofundefined. All consumers who relied upon the function returningundefinedmust update their code to ignore the return value.