A minimal Redux implementation
$ dotnet add package RG.ReduxA minimal Redux implementation
// Events
public interface IFooEvent : IEvent { }
public record FooIncremented() : IFooEvent;
public record FooDecremented() : IFooEvent;
public record FooIncrementedBy(int Value) : IFooEvent;
public record FooReset() : IFooEvent;
// Store and Reducer
public record FooStore() : Store<int, IFooEvent>(
reducer: (state, action) =>
action switch {
FooIncremented => state + 1,
FooDecremented => state - 1,
FooIncrementedBy { Value: var val } => state + val,
FooReset => 0,
_ => state
}.
initialState: 0
);
// Creating a Store instance
var fooStore = new FooStore();
var state = fooStore.State;
fooStore.Dispatch(new FooDecremented());
var subscription = fooStore.Subscribe(value => {
Console.WriteLine(value);
});
subscription.Dispose();
var query = from value in fooStore
where value % 2 == 0
select value / 2;
var subscription = query.Subscribe(value => {
Console.WriteLine(value);
});
Just mutate the State property
public record FooStore() : Store<int>(initialState: 0) {
public void Increment() => State++;
public void Decrement() => State--;
public void IncrementBy(int x) => State += x;
public void Reset() => State = 0;
}