Пример #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;
            });
        }
Пример #2
0
 /// <summary> This should only be used in the datastore dispatchers, marks that
 /// the object was modified by the dispatched mutation </summary>
 public static void MarkMutated(this IsMutable self)
 {
     if (!StateCompare.IsCurrentlyDispatching())
     {
         throw new InvalidOperationException("Model was modified outside of the dispatchers");
     }
     self.LastMutation = Stopwatch.GetTimestamp();
 }
Пример #3
0
 [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");
     }
 }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
 public static Middleware <T> NewMutableDataSupport <T>()
 {
     return((IDataStore <T> store) => {
         return (Dispatcher innerDispatcher) => {
             Dispatcher dispatcher = (action) => {
                 StateCompare.SetStoreDispatchingStarted();
                 var a = innerDispatcher(action);
                 StateCompare.SetStoreDispatchingEnded();
                 return a;
             };
             return dispatcher;
         };
     });
 }
Пример #7
0
        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);
        }
Пример #8
0
 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);
 }
Пример #9
0
 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);
 }
Пример #10
0
        public static Action AddStateChangeListener <T, S>(this IDataStore <T> self, S mutableObj, Action <S> onChanged, bool triggerInstantToInit = true) where S : IsMutable
        {
            Action newListener = () => {
                if (StateCompare.WasModifiedInLastDispatch(mutableObj))
                {
                    onChanged(mutableObj);
                }
            };

            self.onStateChanged += newListener;
            if (triggerInstantToInit)
            {
                onChanged(mutableObj);
            }
            return(newListener);
        }
Пример #11
0
        public Action AddStateChangeListener <T, S>(S mutableObj, Action <S> onChanged, bool triggerInstantToInit = true) where S : IsMutable
        {
            Action newListener = () => {
                if (StateCompare.WasModifiedInLastDispatch(mutableObj))
                {
                    onChanged(mutableObj);
                }
            };

            innerListeners += newListener;
            if (triggerInstantToInit)
            {
                onChanged(mutableObj);
            }
            return(newListener);
        }
Пример #12
0
 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);
 }
Пример #13
0
        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);
        }
Пример #14
0
 /// <summary> Will be true if the object was modified when the last mutation was
 /// dispatched via the data store the object is managed in </summary>
 public static bool WasModifiedInLastDispatch(this IsMutable self)
 {
     return(StateCompare.WasModifiedInLastDispatch(self));
 }