utils: Reference mapping finishes when source finishes #1277
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.
"Shared References" is an RxPlayer structure which allows to pass a value around and making receivers able to register a callback to listen and react for when that value changes, with added niceties like being able through type only to enforce that some code block is only able to read that value, not update it.
It is used a lot to implement RxPlayer API which can change the RxPlayer's behavior during playback such as
wantedBufferAhead
,setPlaybackRate
but also for things like track switching. It was originally created when removing RxJS from the RxPlayer, and it is similar to RxJS'BehaviorSubject
in some ways.It turned out that we also needed some kind of mapping function for some edge cases where we want to communicate a value based on a Shared Reference. An example is the "buffer goal", the size of the buffer constructed by the RxPlayer, which is based on the
wantedBufferAhead
option but with a<=1
factor applied depending on past buffer issues.Such mapping function was simple enough to implement but there was still a potential for a (light) memory leak: when/if the source "Shared Reference" was "finished" (all its listeners are removed and it cannot emit anymore), the mapped reference wasn't (yet still coudn't emit anymore).
This is not a real isue right now but it could become one, so I chose to fix that problem. Sadly, this meant implementing a listener for when a shared reference is finished, which I do not find particularly elegant. As such, I made it clear that it is not supposed to be used frequently by calling it
_onFinished
(so, prepended with an underscore).