Example #1
0
        private static Dispatcher NewLoggingDispatcher <T>(IDataStore <T> store, Dispatcher innerDispatcher)
        {
            return((action) => {
                if (action is IsValid v && !v.IsValid())
                {
                    Log.e("Invalid action: " + asJson(action.GetType().Name, action));
                }

                bool copyOfActionSupported = false;
                object actionBeforeDispatch = null;
                MakeDebugCopyOfAction(action, ref copyOfActionSupported, ref actionBeforeDispatch);

                T previousState = store.GetState();
                var returnedAction = innerDispatcher(action);
                T newState = store.GetState();

                if (copyOfActionSupported)
                {
                    AssertActionDidNotChangeDuringDispatch(actionBeforeDispatch, action);
                }

                if (!StateCompare.WasModified(previousState, newState))
                {
                    Log.w("The action  " + action + " was not handled by any of the reducers! Store=" + store);
                }
                else
                {
                    ShowChanges(action, previousState, newState);
                }
                return returnedAction;
            });
        }
 [Conditional("DEBUG"), Conditional("ENFORCE_ASSERTIONS")] // Stripped from production code
 private static void AssertValid <T>(T oldVal, T newVal)
 {
     if (oldVal is IsValid v1)
     {
         AssertV2.IsTrue(v1.IsValid(), "Old value before mutation invalid");
     }
     if (StateCompare.WasModified(oldVal, newVal) && newVal is IsValid v2)
     {
         AssertV2.IsTrue(v2.IsValid(), "New value after mutation invalid");
     }
 }
        public static ImmutableDictionary <T, V> MutateEntry <T, V>(this ImmutableDictionary <T, V> dict, T key, object action, StateReducer <V> reducer)
        {
            var elem     = dict[key];
            var newValue = reducer(elem, action);

            if (StateCompare.WasModified(elem, newValue))
            {
                dict = dict.SetItem(key, newValue);
            }
            return(dict);
        }
        public static T Mutate <T>(this T self, bool applyReducer, object action, StateReducer <T> reducer, ref bool changed)
        {
            if (!applyReducer)
            {
                return(self);
            }
            var newVal = reducer(self, action);

            changed = changed || StateCompare.WasModified(self, newVal);
            AssertValid(self, newVal);
            return(newVal);
        }
        internal static Action NewSubstateChangeListener <S>(Func <S> getSubstate, Action <S> onChanged)
        {
            var    oldState    = getSubstate();
            Action newListener = () => {
                var newState = getSubstate();
                if (StateCompare.WasModified(oldState, newState))
                {
                    onChanged(newState);
                    oldState = newState;
                }
            };

            return(newListener);
        }
 public static ImmutableDictionary <T, V> MutateEntries <T, V>(this ImmutableDictionary <T, V> dict, object action, StateReducer <V> reducer)
 {
     if (dict != null)
     {
         foreach (var elem in dict)
         {
             var newValue = reducer(elem.Value, action);
             if (StateCompare.WasModified(elem.Value, newValue))
             {
                 dict = dict.SetItem(elem.Key, newValue);
             }
         }
     }
     return(dict);
 }
 public static ImmutableList <T> MutateEntries <T>(this ImmutableList <T> list, object action, StateReducer <T> reducer)
 {
     if (list != null)
     {
         foreach (var elem in list)
         {
             var newElem = reducer(elem, action);
             if (StateCompare.WasModified(elem, newElem))
             {
                 list = list.Replace(elem, newElem);
             }
         }
     }
     return(list);
 }
 public static IList <T> MutateEntries <T>(this IList <T> list, object action, StateReducer <T> reducer, ref bool changed)
 {
     if (list != null)
     {
         for (int i = 0; i < list.Count; i++)
         {
             var elem    = list[i];
             var newElem = reducer(elem, action);
             if (StateCompare.WasModified(elem, newElem))
             {
                 list[i] = newElem;
                 changed = true;
             }
         }
     }
     return(list);
 }
        internal static Action NewSubstateChangeListener <S>(Func <S> getSubstate, Action <S> onChanged)
        {
            var    oldState    = getSubstate();
            var    oldMonitor  = GetMonitorFor(oldState);
            Action newListener = () => {
                var  newState     = getSubstate();
                bool stateChanged = StateCompare.WasModified(oldState, newState);
                if (stateChanged || StateCompare.WasModified(oldMonitor, GetMonitorFor(newState)))
                {
                    onChanged(newState);
                    oldState   = newState;
                    oldMonitor = GetMonitorFor(newState);
                }
            };

            return(newListener);
        }