public WordTutorApplication Reduce(IReduxMessage message, WordTutorApplication currentState) { if (currentState is null) { throw new ArgumentNullException(nameof(currentState)); } if (!(currentState.CurrentScreen is VocabularyBrowserScreen)) { return(currentState); } return((message ?? throw new ArgumentNullException(nameof(message))) switch { ClearSelectedWordMessage _ => currentState.UpdateScreen( (VocabularyBrowserScreen s) => s.ClearSelection()), SelectWordMessage m => currentState.UpdateScreen( (VocabularyBrowserScreen s) => s.WithSelection(m.Word)), OpenScreenForNewWordMessage _ => currentState.OpenScreen( ModifyVocabularyWordScreen.ForNewWord()), OpenScreenForModifyingWordMessage m => currentState.OpenScreen( ModifyVocabularyWordScreen.ForExistingWord(m.Word)), _ => currentState, });
/// <summary> /// Dispatch an application message to modify application state /// </summary> /// <param name="message">Message to process.</param> public void Dispatch(IReduxMessage message) { bool currentlyDispatching; lock (_padlock) { _messagesToDispatch.Enqueue(message ?? throw new ArgumentNullException(nameof(message))); currentlyDispatching = _dispatching; _dispatching = true; } if (!currentlyDispatching) { while (_dispatching) { IReduxMessage messageToDispatch; lock (_padlock) { _dispatching = _messagesToDispatch.TryDequeue(out messageToDispatch !); } if (_dispatching) { var iterator = new ReduxMiddlewareIterator(_processingQueue.Value); iterator.Dispatch(messageToDispatch); } } } }
public void Dispatch(IReduxMessage message, IReduxDispatcher next) { if (next is null) { throw new ArgumentNullException(nameof(next)); } if (message is SpeakMessage speak) { Speak(speak.TextToSay); } next.Dispatch(message); }
public WordTutorApplication Reduce(IReduxMessage message, WordTutorApplication currentState) { if (currentState is null) { throw new ArgumentNullException(nameof(currentState)); } if (!(currentState.CurrentScreen is ModifyVocabularyWordScreen)) { return(currentState); } return((message ?? throw new ArgumentNullException(nameof(message))) switch { CloseScreenMessage m => currentState.CloseScreen(), ModifyPhraseMessage m => currentState.UpdateScreen( (ModifyVocabularyWordScreen s) => s.WithPhrase(m.Phrase)), ModifyPronunciationMessage m => currentState.UpdateScreen( (ModifyVocabularyWordScreen s) => s.WithPronunciation(m.Pronunciation)), ModifySpellingMessage m => currentState.UpdateScreen( (ModifyVocabularyWordScreen s) => s.WithSpelling(m.Spelling)), SaveNewVocabularyWordMessage m => currentState.UpdateVocabularySet( s => s.Add(m.Word)) .CloseScreen(), SaveModifiedVocabularyWordMessage m => currentState.UpdateVocabularySet( s => s.Replace(m.OriginalWord, m.ReplacementWord)) .CloseScreen(), _ => currentState, });
public void Dispatch(IReduxMessage message, IReduxDispatcher next) { LastCapturedMessage = message; next.Dispatch(message); }
public void Dispatch(IReduxMessage message, IReduxDispatcher next) => _dispatch(message, next);
public void Dispatch(IReduxMessage message) => _messages.Add(message);
private int Incrementer(IReduxMessage message, int state) => message is IncrementMessage inc ? state + inc.Increment : state;
public void Dispatch(IReduxMessage message, IReduxDispatcher next) { // do nothing }
private void HandleMessage(string json) { IReduxMessage <string> message = Deserialize <ReduxMessage <string> >(json); if (message.Type == "START") { Status = ReduxStatus.Connected; var onStart = OnStart; onStart?.Invoke(this, EVENT_ARGS); return; } if (Status != ReduxStatus.Connected) { // ignoring messages if not connected return; } var onPush = OnMessage; onPush?.Invoke(this, json); var type = message.Payload?.Type; switch (type) { case "RESET": { var onReset = OnReset; onReset?.Invoke(this, EVENT_ARGS); break; } case "COMMIT": { var onCommit = OnCommit; onCommit?.Invoke(this, EVENT_ARGS); break; } case "IMPORT_STATE": case "ROLLBACK": case "JUMP_TO_STATE": case "JUMP_TO_ACTION": { var computedStates = message.Payload.NextLiftedState?.ComputedStates.Select(s => new ComputedState <S> { State = Deserialize <S>(s.State) }).ToList(); var state = Deserialize <S>(message.State); var onState = OnState; onState?.Invoke(this, new ReduxMessage <S> { Payload = message.Payload != null ? new ReduxPayload <S> { ActionId = message.Payload.ActionId, Type = message.Payload.Type, Index = message.Payload.Index, NextLiftedState = new NextLiftedState <S> { ComputedStates = computedStates } } : null, Id = message.Id, Type = message.Type, Source = message.Source, State = state }); break; } case "TOGGLE_ACTION": { var state = Deserialize <ReduxToggleState <T, S> >(message.State); var onToggle = OnToggle; onToggle?.Invoke(this, new ReduxMessage <IReduxToggleState <T, S> > { Payload = message.Payload != null ? new ReduxPayload <IReduxToggleState <T, S> > { ActionId = message.Payload.ActionId, Type = message.Payload.Type, Index = message.Payload.Index } : null, Id = message.Id, Type = message.Type, Source = message.Source, State = new ReduxToggleState <T, S> { SkippedActionIds = state.SkippedActionIds, StaggedActionIds = state.StaggedActionIds, CurrentStateIndex = state.CurrentStateIndex, NextActionId = state.NextActionId, ActionsById = new Dictionary <int, T>(state.ActionsById), ComputedStates = new List <S>(state.ComputedStates) } }); break; } } }
private string IgnoreMessages(IReduxMessage message, string state) => state;
static string ReduceToNewState(IReduxMessage _, string __) { return(newState); }
public WordTutorApplication TheActionIs(WordTutorApplication application, IReduxMessage message) => Reducer.Reduce(message, application);
public T Reduce(IReduxMessage message, T currentState) { return(_reducers.Aggregate( currentState, (state, reducer) => reducer.Reduce(message, state))); }
public WordTutorApplication Reduce(IReduxMessage message, WordTutorApplication currentState) { _logger.Info($"Message: {message}"); return(currentState); }