예제 #1
0
        public static SubListeners <S> NewSubStateListenerForUnity <T, S>(this SubListeners <T> self, UnityEngine.Object context, Func <T, S> getSubState, bool eventsAlwaysInMainThread = true)
        {
            var subListener         = new SubListeners <S>(getSubState(self.latestSubState));
            var ownListenerInParent = self.AddStateChangeListener(getSubState, newSubState => {
                if (eventsAlwaysInMainThread)
                {
                    MainThread.Invoke(() => { OnSubstateChangedForUnity(subListener, newSubState, context); });
                }
                else
                {
                    OnSubstateChangedForUnity(subListener, newSubState, context);
                }
            });

            subListener.SetUnregisterInParentAction(() => { self.innerListeners -= ownListenerInParent; });
            return(subListener);
        }
예제 #2
0
        public static void SubscribeToStateChanges <T, V>(this UnityEngine.Object self, IDataStore <T> store, Func <T, V> getSubState, Action <V> updateUi, bool triggerOnSubscribe = true, bool eventsAlwaysInMainThread = true)
        {
            updateUi(getSubState(store.GetState()));
            Wrapper w = new Wrapper();

            w.stateChangeListener = store.AddStateChangeListener(getSubState, newVal => {
                if (self.IsDestroyed())
                {
                    store.onStateChanged -= w.stateChangeListener;
                    return;
                }
                if (eventsAlwaysInMainThread)
                {
                    MainThread.Invoke(() => { OnStateChangedForUnity(updateUi, newVal, self); });
                }
                else
                {
                    OnStateChangedForUnity(updateUi, newVal, self);
                }
            }, triggerOnSubscribe);
        }