Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
neozhu opened this issue May 8, 2024 · 2 comments

Comments

@neozhu
Copy link

neozhu commented May 8, 2024

Could you please provide me with an example?

@extential
Copy link

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

@alexyakunin
Copy link
Collaborator

alexyakunin commented Aug 18, 2024

@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).

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

No branches or pull requests

3 participants