Example #1
0
        /// <summary>
        /// Publishes the <paramref name="message"/> on the <see cref="MessageHub"/>.
        /// </summary>
        /// <param name="message">The message to published</param>
        public void Publish <T>(T message)
        {
            var localSubscriptions = Subscriptions.GetTheLatestRevisionOfSubscriptions();

            var msgType = typeof(T);

#if NET_STANDARD
            var msgTypeInfo = msgType.GetTypeInfo();
#endif
            _globalHandler?.Invoke(msgType, message);

            // ReSharper disable once ForCanBeConvertedToForeach | Performance Critical
            for (var idx = 0; idx < localSubscriptions.Length; idx++)
            {
                var subscription = localSubscriptions[idx];

#if NET_STANDARD
                if (!subscription.Type.GetTypeInfo().IsAssignableFrom(msgTypeInfo))
                {
                    continue;
                }
#else
                if (!subscription.Type.IsAssignableFrom(msgType))
                {
                    continue;
                }
#endif
                try
                {
                    subscription.Handle(message);
                }
                catch (Exception e)
                {
                    var copy = OnError;
                    copy?.Invoke(this, new MessageHubErrorEventArgs(e, subscription.Token));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Publishes the <paramref name="message"/> on the <see cref="MessageHub"/>.
        /// </summary>
        /// <param name="message">The message to published</param>
        public async Task PublishAsync <T>(T message)
        {
            var localSubscriptions = Subscriptions.GetTheLatestSubscriptions();

            var msgType = typeof(T);

#if NET_STANDARD
            var msgTypeInfo = msgType.GetTypeInfo();
#endif
            _globalHandler?.Invoke(msgType, message);

            // ReSharper disable once ForCanBeConvertedToForeach | Performance Critical
            for (var idx = 0; idx < localSubscriptions.Length; idx++)
            {
                var subscription = localSubscriptions[idx];

#if NET_STANDARD
                if (!subscription.Type.GetTypeInfo().IsAssignableFrom(msgTypeInfo))
                {
                    continue;
                }
#else
                if (!subscription.Type.IsAssignableFrom(msgType))
                {
                    continue;
                }
#endif
                try
                {
                    await subscription.HandleAsync(message);
                }
                catch (Exception e)
                {
                    _globalErrorHandler?.Invoke(subscription.Token, e);
                }
            }
        }
Example #3
0
 /// <summary>
 /// Clears all the subscriptions from the <see cref="MessageHub"/>.
 /// <remarks>The global handler and the global error handler are not affected</remarks>
 /// </summary>
 public void ClearSubscriptions() => Subscriptions.Clear();
Example #4
0
 /// <summary>
 /// Checks if a specific subscription is active on the <see cref="MessageHub"/>.
 /// </summary>
 /// <param name="token">The token representing the subscription</param>
 /// <returns><c>True</c> if the subscription is active otherwise <c>False</c></returns>
 public Boolean IsSubscribed(Guid token) => Subscriptions.IsRegistered(token);
Example #5
0
 /// <summary>
 /// Unsubscribes a subscription from the <see cref="MessageHub"/>.
 /// </summary>
 /// <param name="token">The token representing the subscription</param>
 public void Unsubscribe(Guid token) => Subscriptions.UnRegister(token);
Example #6
0
 /// <summary>
 /// Subscribes a callback against the <see cref="MessageHub"/> for a specific type of message.
 /// </summary>
 /// <typeparam name="T">The type of message to subscribe to</typeparam>
 /// <param name="action">The callback to be invoked once the message is published on the <see cref="MessageHub"/></param>
 /// <param name="throttleBy">The <see cref="TimeSpan"/> specifying the rate at which subscription is throttled</param>
 /// <returns>The token representing the subscription</returns>
 public Guid Subscribe <T>(Func <T, Task> action, TimeSpan throttleBy)
 {
     EnsureNotNull(action);
     return(Subscriptions.Register(throttleBy, action));
 }
Example #7
0
 /// <summary>
 /// Disposes the <see cref="MessageHub"/>.
 /// </summary>
 public void Dispose()
 {
     Interlocked.Increment(ref _disposed);
     Subscriptions.Dispose();
 }
Example #8
0
 /// <summary>
 /// Clears all the subscriptions from the <see cref="MessageHub"/>.
 /// <remarks>The global handler and the <see cref="OnError"/> are not affected</remarks>
 /// </summary>
 public void ClearSubscriptions()
 {
     EnsureNotDisposed();
     Subscriptions.Clear();
 }
Example #9
0
 /// <summary>
 /// Checks if a specific subscription is active on the <see cref="MessageHub"/>.
 /// </summary>
 /// <param name="token">The token representing the subscription</param>
 /// <returns><c>True</c> if the subscription is active otherwise <c>False</c></returns>
 public bool IsSubscribed(Guid token)
 {
     EnsureNotDisposed();
     return(Subscriptions.IsRegistered(token));
 }
Example #10
0
 /// <summary>
 /// Un-Subscribes a subscription from the <see cref="MessageHub"/>.
 /// </summary>
 /// <param name="token">The token representing the subscription</param>
 public void UnSubscribe(Guid token)
 {
     EnsureNotDisposed();
     Subscriptions.UnRegister(token);
 }
Example #11
0
 /// <summary>
 /// Checks if a specific subscription is active on the <see cref="MessageHub"/>.
 /// </summary>
 /// <param name="token">The token representing the subscription</param>
 /// <returns><c>True</c> if the subscription is active otherwise <c>False</c></returns>
 public bool IsSubscribed(Guid token)
 {
     return(Subscriptions.IsRegistered(token));
 }
Example #12
0
        /// <summary>
        /// Subscribes a callback against the <see cref="MessageHub"/> for a specific type of message.
        /// </summary>
        /// <typeparam name="T">The type of message to subscribe to</typeparam>
        /// <param name="action">The callback to be invoked once the message is published on the <see cref="MessageHub"/></param>
        /// <param name="throttleBy">The <see cref="TimeSpan"/> specifying the rate at which subscription is throttled</param>
        /// <returns>The token representing the subscription</returns>

        public Guid Subscribe <T>(Action <T> action, TimeSpan throttleBy)
        {
            EnsureNotNull(action);
            return(Subscriptions.Register(throttleBy, action, null));
        }