AddListener() публичный Метод

public AddListener ( Action callback ) : void
callback Action
Результат void
Пример #1
0
        public void TestMultipleCallbacks()
        {
            Signal <int> signal = new Signal <int>();

            signal.AddListener(OneArgSignalCallback);
            signal.AddListener(OneArgSignalCallbackTwo);

            signal.Dispatch(testInt);

            Assert.AreEqual(testInt + 1, testValue);
        }
Пример #2
0
        public void Signal_can_not_have_same_listener_twice()
        {
            var signal   = new Signal();
            var listener = new Mock <Action>();

            signal.AddListener(listener.Object);
            signal.AddListener(listener.Object);

            signal.Invoke();

            listener.is_called_once();
        }
Пример #3
0
        public void TestRemoveAllListenersOne()
        {
            Signal <int> signal = new Signal <int>();

            signal.AddListener(OneArgSignalCallback);
            signal.AddListener(OneArgSignalCallbackTwo);

            signal.RemoveAllListeners();
            signal.Dispatch(testInt);

            Assert.AreEqual(0, testValue);
        }
Пример #4
0
        public void TestRemoveAllListeners()
        {
            Signal signal = new Signal();

            signal.AddListener(NoArgSignalCallback);
            signal.AddListener(NoArgSignalCallbackTwo);

            signal.RemoveAllListeners();
            signal.Dispatch();

            Assert.AreEqual(0, testValue);
        }
Пример #5
0
        public void AddListener_SignalWithNoTypeGivenSameCallbackMultipleTimes_ExpectsDelegateCalledOnlyOnce()
        {
            Signal signal = new Signal();

            intToIncrement = 0;
            signal.AddListener(SimpleSignalCallback);
            signal.AddListener(SimpleSignalCallback);

            signal.Dispatch();

            Assert.AreEqual(1, intToIncrement);
        }
Пример #6
0
        public void AddListener_SignalWithOneTypeGivenSameCallbackMultipleTimes_ExpectsDelegateCalledOnlyOnce()
        {
            Signal <int> signal = new Signal <int>();

            testInt = 42;
            signal.AddListener(OneArgSignalCallback);
            signal.AddListener(OneArgSignalCallback);

            signal.Dispatch(testInt);

            Assert.AreEqual(testInt, testValue);
        }
Пример #7
0
        public void TestRemoveAllListenersFour()
        {
            Signal <int, int, int, int> signal = new Signal <int, int, int, int>();

            signal.AddListener(FourArgSignalCallback);
            signal.AddListener(FourArgSignalCallbackTwo);

            signal.RemoveAllListeners();
            signal.Dispatch(testInt, testIntTwo, testIntThree, testIntFour);

            Assert.AreEqual(0, testValue);
        }
Пример #8
0
        public void AllAddedListenersAreCalled_Params()
        {
            var wasCalled1 = false;
            var wasCalled2 = false;
            var signal     = new Signal <int, int>();

            signal.AddListener((ignore1, ignore2) => wasCalled1 = true);
            signal.AddListener((ignore1, ignore2) => wasCalled2 = true);
            signal.Call(0, 0);

            Assert.True(wasCalled1);
            Assert.True(wasCalled2);
        }
Пример #9
0
        public void AllAddedListenersAreCalled()
        {
            var wasCalled1 = false;
            var wasCalled2 = false;
            var signal     = new Signal();

            signal.AddListener(() => wasCalled1 = true);
            signal.AddListener(() => wasCalled2 = true);
            signal.Call();

            Assert.True(wasCalled1);
            Assert.True(wasCalled2);
        }
Пример #10
0
        public void AddListener_SignalWithFourTypesGivenSameCallbackMultipleTimes_ExpectsDelegateCalledOnlyOnce()
        {
            Signal <int, int, int, int> signal = new Signal <int, int, int, int>();

            this.CreateFourInts();
            signal.AddListener(FourArgCallback);
            signal.AddListener(FourArgCallback);

            signal.Dispatch(testInt, testIntTwo, testIntThree, testIntFour);

            int expected = this.GetFourIntExpected();

            Assert.AreEqual(expected, this.testValue);
        }
Пример #11
0
        public void ClearRemovesAllListeners_Params()
        {
            var wasCalled = false;
            var signal    = new Signal <int, int>();

            signal.AddListener((ignore1, ignore2) => wasCalled = true);
            signal.AddListener((ignore1, ignore2) => wasCalled = true);
            signal.AddListener((ignore1, ignore2) => wasCalled = true);
            signal.AddListener((ignore1, ignore2) => wasCalled = true);
            signal.Clear();
            signal.Call(0, 0);

            Assert.False(wasCalled);
        }
Пример #12
0
        public void ClearRemovesAllListeners()
        {
            var wasCalled = false;
            var signal    = new Signal();

            signal.AddListener(() => wasCalled = true);
            signal.AddListener(() => wasCalled = true);
            signal.AddListener(() => wasCalled = true);
            signal.AddListener(() => wasCalled = true);
            signal.Clear();
            signal.Call();

            Assert.False(wasCalled);
        }
Пример #13
0
        public void ListenersShouldBeRemovedByContext_Params()
        {
            var wasCalled = false;
            var wasFailed = false;
            var signal    = new Signal <int, int>();

            signal.AddListener((ignore1, ignore2) => wasCalled = true, "test1");
            signal.AddListener((ignore1, ignore2) => wasFailed = true, "test2");
            signal.AddListener((ignore1, ignore2) => wasCalled = true, "test1");
            signal.AddListener((ignore1, ignore2) => wasFailed = true, "test2");
            signal.RemoveListenerByContext("test2");
            signal.Call(0, 0);

            Assert.True(wasCalled);
            Assert.False(wasFailed);
        }
Пример #14
0
        public void TestFourArgSignal()
        {
            Signal <int, int, int, int> signal = new Signal <int, int, int, int>();

            int intendedResult = ((testInt + testIntTwo) * testIntThree) - testIntFour;

            signal.AddListener(FourArgSignalCallback);
            signal.Dispatch(testInt, testIntTwo, testIntThree, testIntFour);
            Assert.AreEqual(intendedResult, testValue);

            signal.RemoveListener(FourArgSignalCallback);
            signal.Dispatch(testInt, testIntTwo, testIntThree, testIntFour);
            Assert.AreEqual(intendedResult, testValue);             //no-op due to remove

            intendedResult += testInt;
            intendedResult += testIntTwo;
            intendedResult *= testIntThree;
            intendedResult -= testIntFour;

            signal.AddOnce(FourArgSignalCallback);
            signal.Dispatch(testInt, testIntTwo, testIntThree, testIntFour);
            Assert.AreEqual(intendedResult, testValue);

            signal.Dispatch(testInt, testIntTwo, testIntThree, testIntFour);
            Assert.AreEqual(intendedResult, testValue);             //Add once should result in no-op
        }
Пример #15
0
        public void ListenersShouldBeRemovedByContext()
        {
            var wasCalled = false;
            var wasFailed = false;
            var signal    = new Signal();

            signal.AddListener(() => wasCalled = true, "test1");
            signal.AddListener(() => wasFailed = true, "test2");
            signal.AddListener(() => wasCalled = true, "test1");
            signal.AddListener(() => wasFailed = true, "test2");
            signal.RemoveListenerByContext("test2");
            signal.Call();

            Assert.True(wasCalled);
            Assert.False(wasFailed);
        }
Пример #16
0
        public void TestNoArgSignal()
        {
            Signal signal = new Signal();

            signal.AddListener(NoArgSignalCallback);
            signal.Dispatch();
            Assert.AreEqual(1, testValue);
        }
Пример #17
0
        internal override void Initialize()
        {
            base.Initialize();

            _transform = transform;

            _dig.AddListener(AnimateDig);
        }
Пример #18
0
        public void Initialize(string title, Signal <float> signal)
        {
            Disconnect();

            _title.text = title;

            _signal = signal;
            _signal.AddListener(Restart);
        }
Пример #19
0
        public void ParamsAreSentToListener()
        {
            int[] passedParams = null;
            var   signal       = new Signal <int, int>();

            signal.AddListener((p1, p2) => passedParams = new [] { p1, p2 });
            signal.Call(13, 27);

            CollectionAssert.AreEqual(new [] { 13, 27 }, passedParams);
        }
Пример #20
0
        public void AddedListenerIsCalled_Params()
        {
            var wasCalled = false;
            var signal    = new Signal <int, int>();

            signal.AddListener((ignore1, ignore2) => wasCalled = true);
            signal.Call(0, 0);

            Assert.True(wasCalled);
        }
Пример #21
0
        public void AddedListenerIsCalled()
        {
            var wasCalled = false;
            var signal    = new Signal();

            signal.AddListener(() => wasCalled = true);
            signal.Call();

            Assert.True(wasCalled);
        }
Пример #22
0
        public void ParamsAreSentToListener()
        {
            int[] passedParams = null;
            var   signal       = new Signal <int>();

            signal.AddListener(param => passedParams = new [] { param });
            signal.Call(13);

            CollectionAssert.AreEqual(new [] { 13 }, passedParams);
        }
Пример #23
0
        Signal <IMediated> GetPostponeSignal()
        {
            if (onPostponeMediating == null)
            {
                waitingForMediation = new HashSet <IMediated>();
                onPostponeMediating = new Signal <IMediated>();
                onPostponeMediating.AddListener(AddToWaitingList);
            }

            return(onPostponeMediating);
        }
 public static void UpdateListener <A, B, C, D>(this Signal <A, B, C, D> signal, bool toAdd, Action <A, B, C, D> callback)
 {
     if (toAdd)
     {
         signal.AddListener(callback);
     }
     else
     {
         signal.RemoveListener(callback);
     }
 }
Пример #25
0
        public void Signal_can_add_delegates_as_listeners()
        {
            var signal = new Signal();
            var deleg  = new Mock <Action>();

            signal.AddListener(deleg.Object);

            signal.Invoke();

            deleg.is_called_once();
        }
Пример #26
0
        public void Signal_can_be_invoked()
        {
            var signal = new Signal();
            var deleg  = new Mock <Action>();

            signal.AddListener(deleg.Object);

            signal.Invoke();

            deleg.is_called_once();
        }
Пример #27
0
        public void Signal_can_remove_delegates_from_listening()
        {
            var signal = new Signal();
            var deleg  = new Mock <Action>();

            signal.AddListener(deleg.Object);
            signal.RemoveListener(deleg.Object);

            signal.Invoke();

            deleg.is_not_called();
        }
Пример #28
0
        public void Signal_with_parameters_can_add_parameterless_delegates_as_listeners()
        {
            var parametrizedSignal = new Signal <bool>();
            var parameter          = true;
            var deleg = new Mock <Action>();

            parametrizedSignal.AddListener(deleg.Object);

            parametrizedSignal.Invoke(parameter);

            deleg.is_called_once();
        }
Пример #29
0
        public void ListenersAreCalledInDescendingPriorityOrderInAscendingAgeOrder_Params()
        {
            var calls  = new List <int>();
            var signal = new Signal <int, int>();

            signal.AddListener((ignore1, ignore2) => calls.Add(1), null, 10);
            signal.AddListener((ignore1, ignore2) => calls.Add(2), null, 8);
            signal.AddListener((ignore1, ignore2) => calls.Add(3), null, 9);
            signal.AddListener((ignore1, ignore2) => calls.Add(4), null, 10);
            signal.AddListener((ignore1, ignore2) => calls.Add(5), null, 7);
            signal.AddListener((ignore1, ignore2) => calls.Add(6), null, 11);
            signal.AddListener((ignore1, ignore2) => calls.Add(7), null, 8);
            signal.AddListener((ignore1, ignore2) => calls.Add(8), null, 9);
            signal.AddListener((ignore1, ignore2) => calls.Add(9), null, 9);
            signal.Call(0, 0);

            CollectionAssert.AreEqual(new[] { 6, 1, 4, 3, 8, 9, 2, 7, 5 }, calls);
        }
Пример #30
0
        public void RemoveListener_NoType_ExpectsListenerRemoved()
        {
            Signal signal = new Signal();

            signal.AddListener(NoArgSignalCallback);

            signal.Dispatch();
            Assert.AreEqual(1, testValue);

            signal.RemoveListener(NoArgSignalCallback);
            signal.Dispatch();
            Assert.AreEqual(1, testValue);
        }