Oakular.Stateful
0.14.4-rc0003
dotnet add package Oakular.Stateful --version 0.14.4-rc0003
NuGet\Install-Package Oakular.Stateful -Version 0.14.4-rc0003
<PackageReference Include="Oakular.Stateful" Version="0.14.4-rc0003" />
paket add Oakular.Stateful --version 0.14.4-rc0003
#r "nuget: Oakular.Stateful, 0.14.4-rc0003"
// Install Oakular.Stateful as a Cake Addin #addin nuget:?package=Oakular.Stateful&version=0.14.4-rc0003&prerelease // Install Oakular.Stateful as a Cake Tool #tool nuget:?package=Oakular.Stateful&version=0.14.4-rc0003&prerelease
Oakular.Stateful
A reduxesque implementation of state management.
How to use
Stateful is composed solely of the State<T>
type, which can be scoped at various levels of your environment: you can register it through DI, or set it as a property at the highest level you require if your app is using a component stack. For example, in a MAUI application you can register global state inside the App
class:
public partial class App : Application
{
public static State<int> CounterState = new(0);
public static State<ImageViewModel> ImageState = new(default);
}
Or, through DI:
services.AddSingleton(new State<int>(0));
Subscribing to changes
Consumers can subscribe to changes by implementing IObserver<T>
and passing themselves to State<T>.Subscribe
.
sealed class CounterPage : IObserver<IEnumerable<ImageViewModel>>
{
public CounterPage()
{
App.CounterState.Subscribe(this);
}
}
IObserver<T>
forces implementation of OnNext
and OnError
, which State<T>
will invoke for all subscribers when the underlying state changes:
public void OnNext(int value)
{
// do something with the value received.
}
public void OnError(Exception ex)
{
// handle the error received.
}
Mutations
Modifying state is done through Dispatch
only. It is important that Dispatch
is used because it will propagate changes to all subscribers. Select
can be used to modify the underlying state, but will not cause propagation. Unfortunately, because of the complexity of copying C# reference type, it is very difficult to constrain entirely the mutation of T
, yet still allow access to its value.
// Will propagate to subscribers
App.CounterState.Dispatch(_ => _ + 1);
// Will not propagate to subscribers
App.CounterState.Select(_ => _ + 1);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.14.4-rc0003 | 153 | 2/27/2023 |
0.14.3 | 258 | 2/27/2023 |
0.14.2 | 231 | 2/27/2023 |
0.14.1 | 230 | 2/27/2023 |
0.10.0 | 237 | 2/26/2023 |