public static IDisposable TimerSubscribe(this IPubSubBus messageBus, string topic, int multiple, Action <IActionSubscriptionBuilder <MessageTimerEvent> > build)
        {
            Assert.ArgumentNotNull(messageBus, nameof(messageBus));
            Assert.IsInRange(multiple, nameof(multiple), 1, int.MaxValue);
            Assert.ArgumentNotNull(build, nameof(build));

            topic = topic ?? string.Empty;

            var module = messageBus.Modules.Get <MessageTimerModule>();

            if (module == null)
            {
                messageBus.Logger.Warn($"Subscribed to Timer message {topic} but the message timer module has not been initialized. Did you forget to call .{nameof(InitializeMessageTimer)}() first?");
            }
            else if (!module.TimerExists(topic))
            {
                messageBus.Logger.Warn($"Subscribed to Timer message {topic} but that timer does not exist. You must call .{nameof(StartTimer)}({topic}) to activate that timer");
            }

            return(messageBus.Subscribe <MessageTimerEvent>(builder =>
            {
                var b2 = builder.WithTopic(topic);
                build?.Invoke(b2);
                var b3 = b2 as IDetailsSubscriptionBuilder <MessageTimerEvent>;
                b3?.WithFilter(t => t.Id % multiple == 0);
            }));
        }
示例#2
0
        private static IDisposable Subscribe(IPubSubBus messageBus, Action <TPayload> action, Action <IThreadSubscriptionBuilder <TPayload> > build, string channelName)
        {
            var token = messageBus.Subscribe <TPayload>(b =>
            {
                var c = b.WithTopic(channelName).Invoke(action);
                build?.Invoke(c);
            });

            return(token);
        }
示例#3
0
        /// <summary>
        /// Build a subscription using common options
        /// </summary>
        /// <typeparam name="TPayload">The type of object to listen for</typeparam>
        /// <param name="messageBus">The message bus</param>
        /// <param name="build">Lambda function to setup the subscription builder.</param>
        /// <returns>The subscription token which, when disposed, cancels the subscription.</returns>
        public static IDisposable Subscribe <TPayload>(this IPubSubBus messageBus, Action <ITopicSubscriptionBuilder <TPayload> > build)
        {
            Assert.ArgumentNotNull(messageBus, nameof(messageBus));
            Assert.ArgumentNotNull(build, nameof(build));

            var builder = new SubscriptionBuilder <TPayload>(messageBus, messageBus.WorkerPool);

            build(builder);
            var subscription = builder.BuildSubscription();

            var token = messageBus.Subscribe(builder.Topics, subscription);

            return(builder.WrapToken(token));
        }
示例#4
0
        private IDisposable SubscribeUntypedActionInternal <TPayload>(string[] topics, Action act, bool useWeakReference)
        {
            if (topics == null)
            {
                return(_messageBus.Subscribe <TPayload>(b => b
                                                        .ForAllTopics()
                                                        .Invoke(p => act(), useWeakReference)));
            }

            if (topics.Length == 0)
            {
                topics = new[] { string.Empty }
            }
            ;

            return(_messageBus.Subscribe <TPayload>(b => b
                                                    .WithTopic(topics)
                                                    .Invoke(p => act(), useWeakReference)));
        }
示例#5
0
 /// <summary>
 /// Convenience method to subscribe to a single topic
 /// </summary>
 /// <typeparam name="TPayload"></typeparam>
 /// <param name="messageBus"></param>
 /// <param name="topic"></param>
 /// <param name="subscription"></param>
 /// <returns>The subscription token which, when disposed, cancels the subscription</returns>
 public static IDisposable Subscribe <TPayload>(this IPubSubBus messageBus, string topic, ISubscription <TPayload> subscription)
 {
     return(messageBus.Subscribe(new[] { topic ?? string.Empty }, subscription));
 }