Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Reference mapping finishes when source finishes #1277

Merged
merged 1 commit into from
Sep 26, 2023

Conversation

peaBerberian
Copy link
Collaborator

"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).

"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).
@peaBerberian peaBerberian force-pushed the misc/finish-mapped-shared-references branch from e258016 to f6095fd Compare September 26, 2023 12:09
@peaBerberian peaBerberian added this to the 3.32.0 milestone Sep 26, 2023
@peaBerberian peaBerberian merged commit 26258d7 into master Sep 26, 2023
3 checks passed
@peaBerberian peaBerberian deleted the misc/finish-mapped-shared-references branch February 7, 2024 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant