Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: Reference mapping finishes when source finishes
"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 are 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).
- Loading branch information