Adds onFirstListener and onNoListener callbacks #4040
Closed
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.
These callbacks allow anything extending
Backbones.Events
to benotified when something starts listening to it, and when nothing
is left listening to it.
Because of
internalOn
this is hard to implement in user land.Use Case
The use case for this is if you had a Model presenting the output of
an expensive continuous calculation (say real time Stock Prices or
a timer).
The
onFirstListener
function can be used to start your expensivecalculation, as you only care about it while the something is listening
to changes in it.
When that item stops listening to changes in it,
onNoListeners
willtrigger, allowing you to clean up your code (unsubscribe the stock price,
or stop the timer or even stop listening to another Event).