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 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 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)); }
private void Update() { EventBetter.Listen(this, handler); EventBetter.UnlistenAll(this); }