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

Change the Store to have a typed Payload explaining what the change has been in StateHasChangedEventArgs when mutating the State of the Store. #18

Merged
merged 3 commits into from
Jul 10, 2023

Conversation

le-nn
Copy link
Owner

@le-nn le-nn commented Jul 9, 2023

Change the Store to have a typed Payload explaining what the change has been in StateHasChangedEventArgs when mutating the State of the Store.

Changes

TPayload is added to the second argument of Store<TState, TPayload> and MementoStore<TState, TPayload> .
Store<TState>, MementoStore<TState> and FluxStore<TState, TCommand> are unchanged

Usage

Define the payload type.

public enum StateChangedType {
    CountUp,
    Loading,
    CountUpAsync,
    SetCount,
    CountUpWithAmount
}

The Payload type is specified in Store Type params in the following way.

Store<AsyncCounterState, StateChangedType>

If you set Payload as the second argument of Mutate(..., StateChangedType.CountUp), you can have Payload in the StateHasChangedEventArgs.

store.Subscribe(e => {
    Console.WriteLine(e.Command.Payload); // Specified Paylaod
});

Example AsyncCounterStore Overview

public record AsyncCounterState {
    public int Count { get; init; } = 0;

    public bool IsLoading { get; init; } = false;

    public ImmutableArray<int> Histories { get; init; } = ImmutableArray.Create<int>();
}

public enum StateChangedType {
    CountUp,
    Loading,
    CountUpAsync,
    SetCount,
    CountUpWithAmount
}

public class AsyncCounterStore : Store<AsyncCounterState, StateChangedType> {
    public AsyncCounterStore() : base(() => new()) { }

    public void CountUp() {
        Mutate(state => state with {
            Count = state.Count + 1,
            IsLoading = false,
            Histories = state.Histories.Add(state.Count + 1),
        }, StateChangedType.CountUp);
    }

    public async Task CountUpAsync() {
        Mutate(state => state with { IsLoading = true, }, StateChangedType.Loading);
        await Task.Delay(800);
        Mutate(state => state with {
            Count = state.Count + 1,
            IsLoading = false,
            Histories = state.Histories.Add(state.Count + 1),
        }, StateChangedType.CountUp);
    }

    public void CountUpManyTimes(int count) {
        for (var i = 0; i < count; i++) {
            Mutate(state => state with {
                Count = state.Count + 1,
            }, StateChangedType.CountUpAsync);
        }
    }

    public void SetCount(int count) {
        Mutate(state => state with {
            Count = count,
        }, StateChangedType.SetCount);
    }

    public void CountUpWithAmount(int amount) {
        Mutate(state => state with {
            Count = state.Count + amount,
        }, StateChangedType.CountUpWithAmount);
    }
}

Sample Source

https://github.com/le-nn/memento/blob/main/samples/Memento.Sample.Blazor/Stores/AsyncCounterStore.cs

@le-nn
Copy link
Owner Author

le-nn commented Jul 9, 2023

And add docs.

@le-nn le-nn changed the title Change the EventArgs to have a typed Payload when changing the State of the Store. Change the Store to have a typed Payload explaining what the change has been in StateHasChangedEventArgs when mutating the State of the Store. Jul 9, 2023
@le-nn le-nn merged commit 68dc868 into main Jul 10, 2023
@le-nn le-nn deleted the feture/store_notification branch July 10, 2023 06:01
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

Successfully merging this pull request may close these issues.

2 participants