Пример #1
0
        public void MulipleMessageBind()
        {
            var res = 0;
            MessageBindDelegate <string> bindAction  = s => { res |= 1; };
            MessageBindDelegate <string> bindAction2 = s => { res |= 2; };

            var binder = new MessageBinder();

            Is.False.ApplyTo(binder.AnyBind("message"));

            binder.Bind <string>("message", bindAction);
            binder.Bind <string>("message1", bindAction2);

            Is.True.ApplyTo(binder.AnyBind("message"));
            Is.Not.Null.ApplyTo(binder.GetBinds("message"));

            Is.True.ApplyTo(binder.AnyBind("message1"));
            Is.Not.Null.ApplyTo(binder.GetBinds("message1"));

            Assert.AreEqual(1, binder.GetBinds("message").Count);
            Assert.AreEqual(1, binder.GetBinds("message1").Count);

            binder.GetBinds("message").First().BindAction(null);
            binder.GetBinds("message1").First().BindAction(null);

            Assert.AreEqual(3, res);
        }
Пример #2
0
        public void Bind <TMessage>(string name, MessageBindDelegate <TMessage> onRecv)
        {
            if (!_binds.ContainsKey(name))
            {
                _binds[name] = new List <MessageBindInstance>();
            }

            _binds[name].Add(new MessageBindInstance(message => onRecv((TMessage)message), typeof(TMessage)));
        }
Пример #3
0
        public void Bind <TMessage>(MessageBindDelegate <TMessage> onRecv)
        {
            var messageType = typeof(TMessage);
            var msgName     = messageType.FullName;

            var nameAttributes = messageType.GetCustomAttributes(typeof(MessageAttribute), false);

            if (nameAttributes.Any())
            {
                msgName = ((MessageAttribute)nameAttributes[0]).MessageName;
            }

            Bind(msgName, onRecv);
        }
Пример #4
0
        public void ClassNamedAttributeBind()
        {
            var isCall = false;
            MessageBindDelegate <Foo> bindAction = s => isCall = true;;

            var binder = new MessageBinder();

            Is.False.ApplyTo(binder.AnyBind("foo"));

            binder.Bind(bindAction);

            Is.True.ApplyTo(binder.AnyBind("foo"));
            Is.Not.Null.ApplyTo(binder.GetBinds("foo"));

            Assert.AreEqual(1, binder.GetBinds("foo").Count);

            binder.GetBinds("foo")[0].BindAction(null);

            Assert.True(isCall);
        }
Пример #5
0
        public void ClassNamedBind()
        {
            var isCall = false;
            MessageBindDelegate <string> bindAction = s => isCall = true;;

            var binder = new MessageBinder();

            Is.False.ApplyTo(binder.AnyBind(typeof(string).FullName));

            binder.Bind <string>(bindAction);

            Is.True.ApplyTo(binder.AnyBind(typeof(string).FullName));
            Is.Not.Null.ApplyTo(binder.GetBinds(typeof(string).FullName));

            Assert.AreEqual(1, binder.GetBinds(typeof(string).FullName).Count);

            binder.GetBinds(typeof(string).FullName)[0].BindAction(null);

            Assert.True(isCall);
        }
Пример #6
0
        public void MulipleHandlerBind()
        {
            var res = 0;
            MessageBindDelegate <string> bindAction  = s => { res |= 1; };
            MessageBindDelegate <string> bindAction2 = s => { res |= 2; };
            var binder = new MessageBinder();

            Is.False.ApplyTo(binder.AnyBind("message"));

            binder.Bind <string>("message", bindAction);
            binder.Bind <string>("message", bindAction2);

            Is.True.ApplyTo(binder.AnyBind("message"));
            Is.Not.Null.ApplyTo(binder.GetBinds("message"));

            Assert.AreEqual(2, binder.GetBinds("message").Count);

            foreach (var messageBindDelegate in binder.GetBinds("message"))
            {
                messageBindDelegate.BindAction(null);
            }

            Assert.AreEqual(3, res);
        }
Пример #7
0
 public MessageBindInstance(MessageBindDelegate <object> bindAction, Type messageType)
 {
     BindAction  = bindAction;
     MessageType = messageType;
 }