feat(api): improve subscriptions management in Observables and RegionObservers#5101
Open
S-furi wants to merge 4 commits intoAlchemistSimulator:feat/reactive-enginefrom
Open
feat(api): improve subscriptions management in Observables and RegionObservers#5101S-furi wants to merge 4 commits intoAlchemistSimulator:feat/reactive-enginefrom
Observables and RegionObservers#5101S-furi wants to merge 4 commits intoAlchemistSimulator:feat/reactive-enginefrom
Conversation
This idea is borrowed by Androidx, where by means of Lifecycle state machines we are able to bound the lifecycle of the dependencies to the lifecycle of the owner (i.e. the registrant), properly disposing and releasing observers references withtout using weak references, hence not impacting too much negatively performance.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## feat/reactive-engine #5101 +/- ##
=======================================================
Coverage 61.53% 61.53%
Complexity 14 14
=======================================================
Files 2 2
Lines 78 78
Branches 4 4
=======================================================
Hits 48 48
Misses 24 24
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.



This PR aims at improving current
Observablesubscriptions management. Firstly, theDisposablepattern is enforced by makingNodeandActionableadhere to such contract. Moreover, taking inspiration fromLiveDatain androidx, the concept ofLifecycleand owners of such lifecycle are introduced.With this paradigm, automatic dependency cleanup on multiple observables is performed just by using the bridge
Observble<T>.bindTo(LifeCycleOwner, (T) -> Unit), which let us avoid zombie callbacks and automatic disposal of resources when observers lifecycle is over without making the observable holding strong references to them.This pattern also helps in avoiding the usage of weak references, which are (1) not very performant in the form of
WeakHashMap(2) hard to use them in a multi platform environment (still no support for native and Js WeakMap is not iterable).With this new method, we reduce the chances to leak subscribers while making developers avoid to explicitly track every subscription and making sure to dispose them when observers references are lost. Moreover, components lifecycle management could be further bound to the simulation lifecycle in future developments, making Lifecycle events even more meaningful (e.g. ready state on simulation initial configuration, no observers emissions; then started state when simulation starts, start observers emission; then destroyed state when simulation stops, remove every subscription to observers).
Finally this PR tries to better handle
RegionObservers in environment, by managing their lifecycle by means of reference counting. While they provide lazy mechanisms (a region observer starts emitting only when observers are registered), when the observers count reach zero, the region is removed from regions' caches avoiding regions leaking. Still regions could leak resources if developers poorly handle subscriptions by not properly disposing them, however the aboveLifecycleapproach could help.