Stl.Fusion icon indicating copy to clipboard operation
Stl.Fusion copied to clipboard

how to correctly subscribe to an ComputedState<T> state value without inheriting from ComputedStateComponent<T>?

Open neozhu opened this issue 9 months ago • 2 comments

Could you please provide me with an example?

neozhu avatar May 08 '24 09:05 neozhu

hey mate. this has been moved to https://github.com/ActualLab/Fusion

extential avatar May 09 '24 00:05 extential

@extential thanks, your're right.

@neozhu I'll post a quick answer: see how IState<T>.When and IState<T>.Changes methods are implemented ( https://github.com/ActualLab/Fusion/blob/master/src/ActualLab.Fusion/State/StateExt.cs#L81 ). Overall, they call identical methods on State.Computed, which can be found here: https://github.com/ActualLab/Fusion/blob/master/src/ActualLab.Fusion/ComputedExt.cs#L175

And that's nearly the logic you need to observe IState<T> changes. Or simply use await foreach (var computed in state.Changes(...)) or await foreach (value, error) in state.Changes(...)). Computed<T> implements IResult<T>, which means it can be deconstructed to (T value, Exception? error).

alexyakunin avatar Aug 18 '24 00:08 alexyakunin