Skip to content

feat(api): improve subscriptions management in Observables and RegionObservers#5101

Open
S-furi wants to merge 4 commits intoAlchemistSimulator:feat/reactive-enginefrom
S-furi:feat/reactive-engine
Open

feat(api): improve subscriptions management in Observables and RegionObservers#5101
S-furi wants to merge 4 commits intoAlchemistSimulator:feat/reactive-enginefrom
S-furi:feat/reactive-engine

Conversation

@S-furi
Copy link
Contributor

@S-furi S-furi commented Jan 31, 2026

This PR aims at improving current Observable subscriptions management. Firstly, the Disposable pattern is enforced by making Node and Actionable adhere to such contract. Moreover, taking inspiration from LiveData in androidx, the concept of Lifecycle and 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 above Lifecycle approach could help.

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.
@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Jan 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.53%. Comparing base (5ee3457) to head (ba4f2f3).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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