示例#1
0
        /// <summary>
        ///     Subscribe a message.
        /// </summary>
        /// <typeparam name="T">The type of message that the recipient subscribes for.</typeparam>
        /// <param name="channel">
        ///     A name for a messaging channel.If a recipient subscribes
        ///     using a channel, and a sender sends a message using the same channel, then this
        ///     message will be delivered to the recipient. Other recipients who did not
        ///     use a channel when subscribing (or who used a different channel) will not
        ///     get the message.
        /// </param>
        /// <param name="action">The action that will be executed when a message of type T is sent.</param>
        /// <returns>
        ///     Disposable object that can be used to unsubscribe the message from the messenger.
        ///     if the disposable object is disposed,the message is automatically unsubscribed.
        /// </returns>
        public virtual ISubscription <T> Subscribe <T>(string channel, Action <T> action)
        {
            SubjectBase notifier = null;
            ConcurrentDictionary <Type, SubjectBase> dict = null;

            if (!channelNotifiers.TryGetValue(channel, out dict))
            {
                dict = new ConcurrentDictionary <Type, SubjectBase>();
                if (!channelNotifiers.TryAdd(channel, dict))
                {
                    channelNotifiers.TryGetValue(channel, out dict);
                }
            }

            if (!dict.TryGetValue(typeof(T), out notifier))
            {
                notifier = new Subject <T>();
                if (!dict.TryAdd(typeof(T), notifier))
                {
                    dict.TryGetValue(typeof(T), out notifier);
                }
            }

            return((notifier as Subject <T>).Subscribe(action));
        }
示例#2
0
        /// <summary>
        /// Subscribe a message.
        /// </summary>
        /// <param name="channel">A name for a messaging channel.If a recipient subscribes
        /// using a channel, and a sender sends a message using the same channel, then this
        /// message will be delivered to the recipient. Other recipients who did not
        /// use a channel when subscribing (or who used a different channel) will not
        /// get the message. </param>
        /// <param name="type">The type of message that the recipient subscribes for.</param>
        /// <param name="action">The action that will be executed when a message of type T is sent.</param>
        /// <returns>Disposable object that can be used to unsubscribe the message from the messenger.
        /// if the disposable object is disposed,the message is automatically unsubscribed.</returns>
        public virtual IDisposable Subscribe(string channel, Type type, Action <object> action)
        {
            Dictionary <Type, SubjectBase> dict = null;
            SubjectBase notifier = null;

            lock (channelNotifiers)
            {
                if (!channelNotifiers.TryGetValue(channel, out dict))
                {
                    dict = new Dictionary <Type, SubjectBase>();
                    channelNotifiers.Add(channel, dict);
                }

                if (!dict.TryGetValue(type, out notifier))
                {
                    notifier = new Subject <object>();
                    dict.Add(type, notifier);
                }
            }
            return((notifier as Subject <object>).Subscribe(action));
        }