fabulous-dev / Fabulous

Declarative UI framework for cross-platform mobile & desktop apps, using MVU and F# functional programming
https://fabulous.dev
Apache License 2.0
1.13k stars 122 forks source link

Dispose properly ViewNode and Component when Widget is removed from tree #1066

Closed TimLariviere closed 5 months ago

TimLariviere commented 5 months ago

New dependency tree:

graph TD;
    Control-->ViewNode
    ViewNode-->Events[Event handlers]
    ViewNode-->Component
    Component-->ComponentContext
    ComponentContext-->Runner
    Runner-->Subscriptions

When Fabulous detects a widget is removed from the view tree, it will dispose the associated ViewNode, which in turn will dispose the whole dependency tree.

We need to rely on ViewNode.Dispose() because frameworks like Maui doesn't provide any event telling us when a control is removed from the UI tree. The Unloaded event of Maui for example is called as soon as the control is no longer visible (eg we navigated to another page, but the previous page is still supposed to be alive)