uno.extensions
uno.extensions copied to clipboard
[MVUX] Support of I[Async]Disposable
Current behavior
To ensure to propagate the disposable from the bindable to the model and its IState
(as those instance are more prone to have flowed to other application layers), MVUX's generator will generate implementation of IAsyncDisposable
in the model.
This prevent users to have there own logic for the dispose.
Expected behavior
User can add their own disposables.
How to reproduce it (as minimally and precisely as possible)
Implement IAsyncDIspsoable
in a class named MyModel
Environment
Nuget Package (s): Package Version(s): 4.1 Affected platform(s):
- [x] iOS
- [x] macOS (AppKit)
- [x] Mac Catalyst
- [x] Android
- [x] WebAssembly
- [x] Windows
- [x] Skia (WPF)
- [x] Skia (GTK on Linux/macOS/Windows)
- [x] Skia (Linux Framebuffer)
- [ ] Build tasks
Visual Studio: irrelevant Relevant plugins: none
Anything else we need to know?
MVUX actually implements IAsyncDisposable
only for safety, but it also relies on weakly attached object to detect that a model is being GC'ed and then dispose its IState
.
WORKAROUND
We can rely on that weakly attached object to also dispose user's code by using:
Uno.Extensions.Reactive.Core.SourceContext.GetOrCreate(this).Token;
WARNING: Callbacks registered in that CancellationToken
must not prevent collection of the model by the GC as it will prevent that token to be cancelled.
Note: In a previous life, I solved this issue by creating attributes [Constructor]
, [Initialize]
(right after the constructor, once all members are initialized), [Dispose]
and [Finalizer]
and then generating methods that would "compose" all methods flags with that attributes.