AddOnce() public method

public AddOnce ( Action callback ) : void
callback Action
return void
コード例 #1
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        public void AddOnce_SignalWithNoTypeGivenSameCallbackMultipleTimes_ExpectsDelegateCalledOnlyOnce()
        {
            Signal signal = new Signal();

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

            signal.Dispatch();

            Assert.AreEqual(1, intToIncrement);
        }
コード例 #2
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        public void AddOnce_SignalWithOneTypeGivenSameCallbackMultipleTimes_ExpectsDelegateCalledOnlyOnce()
        {
            Signal <int> signal = new Signal <int>();

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

            signal.Dispatch(testInt);

            Assert.AreEqual(testInt, testValue);
        }
コード例 #3
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        public void TestRemoveAllRemovesOnce()
        {
            Signal signal = new Signal();

            signal.AddOnce(NoArgSignalCallback);
            signal.AddOnce(NoArgSignalCallbackTwo);

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

            Assert.AreEqual(0, testValue);
        }
コード例 #4
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        public void AddOnce_SignalWithFourTypesGivenSameCallbackMultipleTimes_ExpectsDelegateCalledOnlyOnce()
        {
            Signal <int, int, int, int> signal = new Signal <int, int, int, int>();

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

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

            int expected = this.GetFourIntExpected();

            Assert.AreEqual(expected, this.testValue);
        }
コード例 #5
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        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
        }
コード例 #6
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        public void TestOnce()
        {
            Signal <int> signal = new Signal <int>();

            signal.AddOnce(OneArgSignalCallback);
            signal.Dispatch(testInt);
            Assert.AreEqual(testInt, testValue);

            signal.Dispatch(testInt);
            Assert.AreEqual(testInt, testValue);             //should not fire a second time
        }
コード例 #7
0
        public void AddOnceTest()
        {
            var signal = new Signal();

            var counter = 0;

            Slot listener1 = ((args) => { counter++; });

            signal.AddOnce(listener1);

            // dispatch zwei
            signal.Dispatch();
            signal.Dispatch();

            Assert.AreEqual(1, counter, "The listener should only be called once");
        }
コード例 #8
0
ファイル: TestSignal.cs プロジェクト: Pikhota/CourseProject
        public void TestTwoArgSignal()
        {
            Signal <int, int> signal = new Signal <int, int>();

            signal.AddListener(TwoArgSignalCallback);
            signal.Dispatch(testInt, testIntTwo);
            Assert.AreEqual(testInt + testIntTwo, testValue);

            signal.RemoveListener(TwoArgSignalCallback);
            signal.Dispatch(testInt, testIntTwo);
            Assert.AreEqual(testInt + testIntTwo, testValue);             //Removed listener should have no-op

            signal.AddOnce(TwoArgSignalCallback);
            signal.Dispatch(testInt, testIntTwo);
            Assert.AreEqual((testInt + testIntTwo) * 2, testValue);

            signal.Dispatch(testInt, testIntTwo);
            Assert.AreEqual((testInt + testIntTwo) * 2, testValue);             //addonce should result in no-op
        }
 public static void DispatchAndListen <A, B, C, D>(this Signal dispatchingSignal, Signal <A, B, C, D> listenOn, Action <A, B, C, D> callback)
 {
     listenOn.AddOnce(callback);
     dispatchingSignal.Dispatch();
 }
 public static void ListenAndStartWith <A, B, C, D>(this Signal <A, B, C, D> listeningSignal, Action startProcess, Action <A, B, C, D> callback)
 {
     listeningSignal.AddOnce(callback);
     startProcess.Invoke();
 }