Example #1
0
        public async Task PublishAsync_Runs_All_Interested_Subscribers()
        {
            const string message            = nameof(message);
            var          subscriber1Handled = false;
            var          subscriber2Handled = false;
            var          subscriber3Handled = false;
            var          subscriber4Handled = false;

            var messageChannel = new MessageChannel();

            messageChannel.Subscribe <string>(m =>
            {
                subscriber1Handled = true;
                return(Task.CompletedTask);
            });

            var subscriber2 = messageChannel.Subscribe <string>(m =>
            {
                subscriber2Handled = true;
                return(Task.CompletedTask);
            });

            subscriber2.Filter(m => m == message);

            var subscriber3 = messageChannel.Subscribe <string>(m =>
            {
                subscriber3Handled = true;
                return(Task.CompletedTask);
            });

            subscriber3.Filter(m => m != message);

            messageChannel.Subscribe <int>(m =>
            {
                subscriber4Handled = true;
                return(Task.CompletedTask);
            });

            await messageChannel.PublishAsync(message).ConfigureAwait(false);

            Assert.True(subscriber1Handled);
            Assert.True(subscriber2Handled);
            Assert.False(subscriber3Handled);
            Assert.False(subscriber4Handled);
        }