Exemplo n.º 1
0
        public IDisposable Subscribe <TMessage>(Func <TMessage, Task> callback) where TMessage : IDomainMessage <TDocument>
        {
            var key          = new NanoSubsciptionKey(typeof(TMessage), new NanoDocumentId(null));
            var subscription = (NanoSubscription <TDocument, TMessage>)_subscriptions.GetOrAdd(key, CreateSubscription <TMessage>);

            return(subscription.AddHandler(callback));
        }
Exemplo n.º 2
0
        public Task DistributeAsync(NanoPublishMessage publishMessage)
        {
            Type messageType;

            if (!_messageTypeMap.TryGetValue(publishMessage.MessageTypeName, out messageType))
            {
                return(Task.FromResult(0));
            }

            var message = publishMessage.DeseralizePayload(messageType);

            INanoSubscription nanoSubscription;
            var tasks = new List <Task>(2);

            var key = new NanoSubsciptionKey(messageType, publishMessage.NanoDocumentId);

            if (_subscriptions.TryGetValue(key, out nanoSubscription))
            {
                var task = nanoSubscription.InvokeHandlers(message);
                tasks.Add(task);
            }

            key = new NanoSubsciptionKey(messageType, NanoDocumentId.Empty);
            if (_subscriptions.TryGetValue(key, out nanoSubscription))
            {
                var task = nanoSubscription.InvokeHandlers(message);
                tasks.Add(task);
            }

            return(Task.WhenAll(tasks));
        }
Exemplo n.º 3
0
        private INanoSubscription CreateSubscription <TMessage>(NanoSubsciptionKey subscriptionKey) where TMessage : IDomainMessage <TDocument>
        {
            if (_messageTypeMap.TryAdd(typeof(TMessage).FullName, typeof(TMessage)))
            {
                var subscription = new NanoSubscription <TDocument, TMessage>();
                var nanoMessage  = NanoMessage.CreateSubscribeMessage <TDocument, TMessage>(NanoBusClient.ClientId, subscriptionKey.NanoDocumentId);
                EnqueueMessage(nanoMessage);

                return(subscription);
            }
            ;

            throw new InvalidOperationException();
        }