/// <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)); } } }
/// <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); } } }
/// <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();
/// <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);
/// <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);
/// <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)); }
/// <summary> /// Disposes the <see cref="MessageHub"/>. /// </summary> public void Dispose() { Interlocked.Increment(ref _disposed); Subscriptions.Dispose(); }
/// <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(); }
/// <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)); }
/// <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); }
/// <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)); }
/// <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)); }