public Storage(SimpleEffectsMiddleware <GlobalState> effectsMiddleware) { this.Store = new Store <GlobalState>( (state, action) => { if (action is CleanupAction cleanupAction) { return(state.RemoveRange(state.Keys.Where(s => s.StartsWith(cleanupAction.Id)))); } if (action is SetValueAction setValueAction) { return(state.SetItem(setValueAction.PropertyName, setValueAction.Value)); } if (action is ModuleScopeAction moduleScopeAction && moduleScopeAction.Id != null) { var immutableDictionary = _reducers.Aggregate(state, (tempState, pair) => moduleScopeAction.Id.Equals(pair.Key) ? pair.Value.Reduce(tempState, action) : tempState); return(immutableDictionary); } if (action is SetupStateAction setupState) { return(setupState.State); } return(_reducers.Aggregate(state, (objects, pair) => pair.Value.Reduce(state, action))); }, new Dictionary <string, object> { }.ToImmutableDictionary(), effectsMiddleware.RegisterMiddleware()); }
public PageControllerEffects(PageControllerReducer reducer, SimpleEffectsMiddleware <GlobalState> effects, Storage storage) : base(effects, storage) { CreateEffect(actions => actions .OfType <RegisterControlsAction>() .WithLatestFrom(reducer.Select(state => state.Controls), LambdaHelper.ToTuple) .Select(tuple => { var(action, currentConfig) = tuple; var config = currentConfig; void Handle(object _, object __) { config.Remove(action.Token.Id); action.Token.Deactivated -= Handle; } action.Token.Deactivated += Handle; currentConfig = currentConfig.SetItem(action.Token.Id, action.Configs); return(reducer.SetActionId(new SetControlsAction(currentConfig))); }) ); }