Exemplo n.º 1
0
        public bool Register(string keyword, Action <T> callback)
        {
            if (keyword.IsEmpty())
            {
                return(false);
            }

            var result = false;

            if (m_Subscriptions != null)
            {
                RedisActionBag <T> bag;
                if (!m_Subscriptions.TryGetValue(keyword, out bag))
                {
                    lock (m_SyncObj)
                    {
                        if (!m_Subscriptions.TryGetValue(keyword, out bag))
                        {
                            bag = new RedisActionBag <T>();
                            bag.Add(callback);
                            m_Subscriptions[keyword] = bag;
                            result = true;
                        }
                    }
                }

                if (!result)
                {
                    lock (m_SyncObj)
                    {
                        var minfo = callback.Method;

                        var index = bag.FindIndex(c => c.Method == minfo);
                        if (index == -1)
                        {
                            bag.Add(callback);
                            result = true;
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public bool Register(string keyword, RedisActionBag <T> callbacks)
        {
            if (keyword.IsEmpty() || callbacks == null ||
                callbacks.Count == 0)
            {
                return(false);
            }

            var result = false;

            if (m_Subscriptions != null)
            {
                RedisActionBag <T> bag;
                var processed = false;
                if (!m_Subscriptions.TryGetValue(keyword, out bag))
                {
                    lock (m_SyncObj)
                    {
                        if (!m_Subscriptions.TryGetValue(keyword, out bag))
                        {
                            processed = true;
                            bag       = new RedisActionBag <T>();

                            foreach (var callback in callbacks)
                            {
                                if (callback != null)
                                {
                                    var minfo = callback.Method;

                                    var index = bag.FindIndex(c => c.Method == minfo);
                                    if (index == -1)
                                    {
                                        bag.Add(callback);
                                        result = true;
                                    }
                                }
                            }

                            if (result)
                            {
                                m_Subscriptions[keyword] = bag;
                            }
                        }
                    }
                }

                if (!processed)
                {
                    lock (m_SyncObj)
                    {
                        foreach (var callback in callbacks)
                        {
                            if (callback != null)
                            {
                                var minfo = callback.Method;

                                var index = bag.FindIndex(c => c.Method == minfo);
                                if (index == -1)
                                {
                                    bag.Add(callback);
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }