public static TodoListState ListReduce(TodoListState prevState, object action) { var currentList = prevState.todoList; var currentState = prevState.showState; switch (action) { case Actions.AddAction _: var addItem = (action as Actions.AddAction).item; currentList.Add(addItem); SaveData(currentList); return(new TodoListState(currentList, currentState)); break; case Actions.UpdateFinishAction _: var updateItem = (action as Actions.UpdateFinishAction).item; updateItem.isFinish = !(updateItem.isFinish); SaveData(currentList); return(prevState); case Actions.DeleteAction _: var removeItem = (action as Actions.DeleteAction).item; currentList.Remove(removeItem); SaveData(currentList); return(new TodoListState(currentList, currentState)); default: return(prevState); } }
public void CanSelectEntireState() { // Arrange var initialState = CreateInitialTodoListState(); var store = new TodoListStore( Setup.TodoListStore.Reducers.CreateReducers(), initialState ); // Act int observeCount = 0; TodoListState lastState = null; store.Select() .Subscribe(state => { observeCount++; lastState = state; }); DispatchAddTodoItemAction(store, 1, "Create unit tests"); DispatchSwitchUserAction(store, "Emily"); // Assert Assert.Equal(3, observeCount); Assert.Single(lastState.TodoList); Assert.Equal("Emily", lastState.CurrentUser); }
public void CanResetStore() { // Arrange var initialState = CreateInitialTodoListState(); var store = new TodoListStore( Setup.TodoListStore.Reducers.CreateReducers(), initialState, true ); // Act int observeCount = 0; TodoListState lastState = null; store.ObserveReset() .Subscribe(state => { observeCount++; lastState = state; }); DispatchAllActions(store); store.Reset(); // Assert Assert.Equal(1, observeCount); Assert.Empty(lastState.TodoList); Assert.Equal("David", lastState.CurrentUser); }
public async Task <int> Save(TodoListState aggregate) { var toAppend = aggregate.UncommittedEvents.Select(Serialize).ToArray(); var writeResult = await _store.AppendToStream(aggregate.Id, aggregate.LoadedRevision, toAppend); return(writeResult.CurrentVersion); }
public async Task <TodoListState> Load(string aggregateId) { var stream = await _store.ReadStreamForwards(new StreamId(aggregateId), 0, int.MaxValue, true); var aggregate = new TodoListState(aggregateId, stream.LastStreamVersion); foreach (var message in stream.Messages) { var @event = await Deserialize(message); aggregate.Apply(@event); } return(aggregate); }