public void TestNestedRaiseSimple() { int nestedCalls = 0; int totalInvocations = 0; EventBetter.Listen(this, (TestMessage o) => { ++totalInvocations; InstanceHandle(null, null); try { ++nestedCalls; if (nestedCalls < 10) { EventBetter.Raise(new TestMessage()); } } finally { --nestedCalls; } }); EventBetter.Raise(new TestMessage()); Assert.AreEqual(10, totalInvocations); }
public void TestNestedRegisterSimple() { int totalInvocations = 0; EventBetter.Listen(this, (TestMessage o) => { InstanceHandle(null, null); ++totalInvocations; EventBetter.Listen(this, (TestMessage oo) => { InstanceHandle(null, null); ++totalInvocations; }); }); EventBetter.Raise(new TestMessage()); Assert.AreEqual(2, totalInvocations); EventBetter.Raise(new TestMessage()); Assert.AreEqual(5, totalInvocations); EventBetter.Raise(new TestMessage()); Assert.AreEqual(9, totalInvocations); }
public IEnumerator TestCoroutineCapture() { { // if nothing is captured - works yield return(null); EventBetter.Listen(this, (TestMessage msg) => { }); Assert.IsTrue(EventBetter.UnlistenAll(this)); } { // if nothing is captured - works int count = 0; yield return(null); using (var listener = EventBetter.ListenManual((TestMessage msg) => InstanceHandle(++count, 1))) { Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(1, count); } Assert.IsFalse(EventBetter.Raise(new TestMessage())); Assert.AreEqual(1, count); } { // if local coroutine variables captured - won't int count = 0; yield return(null); EventBetter.Listen(this, (TestMessage msg) => InstanceHandle(++count, 1), once: true); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(1, count); } }
public void TestWorker() { var worker = GameObject.Find("EventBetterWorker"); Assert.IsFalse(worker); EventBetter.Listen(this, (TestMessage o) => { }); worker = GameObject.Find("EventBetterWorker"); Assert.IsTrue(worker); Assert.AreEqual(worker, EventBetter.Test_Worker?.gameObject); worker.SetActive(false); Assert.Throws <InvalidOperationException>(() => EventBetter.Listen(this, (TestMessage o) => { })); worker.SetActive(true); EventBetter.Listen(this, (TestMessage o) => { }); worker.GetComponent <MonoBehaviour>().enabled = false; Assert.Throws <InvalidOperationException>(() => EventBetter.Listen(this, (TestMessage o) => { })); worker.GetComponent <MonoBehaviour>().enabled = true; EventBetter.Listen(this, (TestMessage o) => { }); DestroyImmediate(worker); EventBetter.Listen(this, (TestMessage o) => { }); worker = GameObject.Find("EventBetterWorker"); Assert.IsTrue(worker); Assert.AreEqual(worker, EventBetter.Test_Worker?.gameObject); }
public void TestOnce() { { int count = 0; EventBetter.Listen(this, (TestMessage o) => ++ count); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(2, count); Assert.IsTrue(EventBetter.UnlistenAll(this)); } { int count = 0; EventBetter.Listen(this, (TestMessage o) => ++ count, once: true); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.IsFalse(EventBetter.Raise(new TestMessage())); Assert.AreEqual(1, count); Assert.IsFalse(EventBetter.UnlistenAll(this)); } { int count = 0; EventBetter.Listen(this, (TestMessage o) => ++ count, once: true); EventBetter.Listen(this, (TestMessage o) => ++ count, once: true); EventBetter.Listen(this, (TestMessage o) => ++ count, once: true); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(3, count); Assert.IsFalse(EventBetter.Raise(new TestMessage())); Assert.IsFalse(EventBetter.UnlistenAll(this)); } }
public void TestUnregister() { Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Raise(new TestMessage())); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); Assert.AreEqual(true, EventBetter.Raise(new TestMessage())); Assert.AreEqual(true, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Raise(new TestMessage())); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); Assert.AreEqual(true, EventBetter.Raise(new TestMessage())); Assert.AreEqual(true, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Raise(new TestMessage())); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); Assert.AreEqual(true, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Unlisten <TestMessage>(this)); Assert.AreEqual(false, EventBetter.Raise(new TestMessage())); }
public void NestedDifferentContext(int baseValue) { EventBetter.Listen(this, (TestMessage o) => { InstanceHandle(null, null); ++nestedInvocations; }); }
public void TestSelfAdv() { var date = System.DateTime.Now.Date; var str = "TestString"; EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(date, System.DateTime.Now.Date)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(str, "TestString")); }
public void TestNestedMessedUp() { int nestedCalls = 0; int totalInvocations = 0; int dummyMessage2Invocations = 0; EventBetter.Listen(this, (TestMessage2 o) => { InstanceHandle(null, null); ++dummyMessage2Invocations; EventBetter.Unlisten <TestMessage>(this); }); int dummyMessage3Invocations = 0; EventBetter.Listen(this, (TestMessage3 o) => { InstanceHandle(null, null); ++dummyMessage3Invocations; EventBetter.Unlisten <TestMessage3>(this); EventBetter.Raise(new TestMessage()); }); EventBetter.Listen(this, (TestMessage o) => { InstanceHandle(null, null); ++totalInvocations; ++nestedCalls; if (nestedCalls >= 10) { EventBetter.Raise(new TestMessage2()); EventBetter.Raise(new TestMessage3()); EventBetter.Raise(new TestMessage2()); EventBetter.Raise(new TestMessage3()); EventBetter.Raise(new TestMessage2()); EventBetter.Raise(new TestMessage3()); } EventBetter.Raise(new TestMessage()); }); EventBetter.Raise(new TestMessage()); Assert.AreEqual(10, totalInvocations); Assert.AreEqual(3, dummyMessage2Invocations); Assert.AreEqual(1, dummyMessage3Invocations); }
public void TestStructStatic() { var someStruct = new SomeStruct() { Value = 123 }; EventBetter.Listen(this, (TestMessage o) => StaticHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => StaticHandle(someStruct, new SomeStruct() { Value = 123 })); EventBetter.Listen(this, (TestMessage o) => StaticHandle(someStruct.Value, 123)); }
public void TestClassStatic() { var someClass = new SomeClass() { Value = 456 }; EventBetter.Listen(this, (TestMessage o) => StaticHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => StaticHandle(someClass, new SomeClass() { Value = 456 })); EventBetter.Listen(this, (TestMessage o) => StaticHandle(someClass.Value, 456)); }
public void TestSomeOtherHost() { { var go = new GameObject("blah"); go.transform.SetParent(transform); EventBetter.Listen(go, (TestMessage o) => StaticHandle(go.name, "blah")); } { var go = new GameObject("blah2"); go.transform.SetParent(transform); EventBetter.Listen(go, (TestMessage o) => InstanceHandle(go.name, "blah2")); } }
public void TestClass() { var someClass = new SomeClass() { Value = 456 }; // this captured implicitly EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(someClass, new SomeClass() { Value = 456 })); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(someClass.Value, 456)); }
public void TestIfActiveAndEnabled() { int count = 0; EventBetter.Listen(this, (TestMessage o) => ++ count); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(1, count); enabled = false; Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(2, count); enabled = true; gameObject.SetActive(false); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(3, count); gameObject.SetActive(true); Assert.IsTrue(EventBetter.Unlisten <TestMessage>(this)); Assert.IsFalse(EventBetter.Raise(new TestMessage())); Assert.AreEqual(3, count); EventBetter.Listen(this, (TestMessage o) => ++ count, exculdeInactive: true); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(4, count); enabled = false; Assert.IsFalse(EventBetter.Raise(new TestMessage())); Assert.AreEqual(4, count); enabled = true; Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(5, count); gameObject.SetActive(false); Assert.IsFalse(EventBetter.Raise(new TestMessage())); Assert.AreEqual(5, count); gameObject.SetActive(true); Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.AreEqual(6, count); }
public void TestMutableLambda() { { int startValue = 100; EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 100)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 101)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 102)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 103)); EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue++, 104)); } { int startValue = 200; EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue, 210)); startValue += 10; EventBetter.Listen(this, (TestMessage o) => InstanceHandle(startValue, 210)); } Assert.IsTrue(EventBetter.Raise(new TestMessage())); Assert.IsTrue(EventBetter.UnlistenAll(this)); }
public void TestNestedRaiseContexts() { int totalInvocations = 0; nestedInvocations = 0; EventBetter.Listen(this, (TestMessage o) => { InstanceHandle(null, null); ++totalInvocations; NestedDifferentContext(totalInvocations); }); EventBetter.Raise(new TestMessage()); Assert.AreEqual(2, totalInvocations + nestedInvocations); EventBetter.Raise(new TestMessage()); Assert.AreEqual(5, totalInvocations + nestedInvocations); EventBetter.Raise(new TestMessage()); Assert.AreEqual(9, totalInvocations + nestedInvocations); }
private void Awake() { EventBetter.Listen(this, (CollisionMessage msg) => LogCollision(msg.reporter, msg.collision)); }
private void Awake() { EventBetter.Listen(this, (TextMessage msg) => ++ count); }
public void TestSelf() { EventBetter.Listen(this, (TestMessage o) => InstanceHandle(null, null)); }
public void TestSelfStatic() { EventBetter.Listen(this, (TestMessage o) => StaticHandle(null, null)); }
private void Update() { EventBetter.Listen(this, handler); EventBetter.UnlistenAll(this); }
void Awake() { EventBetter.Listen <SimpleConsumerNoLambda, TextMessage>(this, HandlePrintMessage, once: true); }
void Awake() { EventBetter.Listen(this, (TextMessage msg) => Debug.Log(msg.text, this)); }