ProSol.Messaging/docs/Subscriptions.md

1.5 KiB

ProSol.Messaging.Subscriptions

Enables a conditional subscribing for a publisher:

publisher.Subscribe(subscriber, msg => msg.Contains("ERROR"))

Demo

Let`s create a console logger for error messages.

Add the package:

dotnet add package ProSol.Messaging --version 4.0.0-rc.6.0

Include the package:

using ProSol.Messaging;
using ProSol.Messaging.Subscriptions;

Now, let`s build a system, which reacts on errors:

var publisher = new PipelineMessagePublisher<string>();
var subscriber = new DataSubscriber<string>();

publisher
    .Subscribe(new Logger(), msg => msg.Contains("ERROR"))
    .Subscribe(subscriber);

So, it's a two subscribers:

  • Logger: writes string to Console;
  • DataSubscriber: accumulates string in innter list.

Now, lets simulate some logs:

string[] source = [
    "quick brown fox",
    "introduced an ERROR",
    "so the lazy dog",
    "got a message"
];

foreach (var item in source)
{
    publisher.Publish(item);
}

Console output: introduced an ERROR subscriber.Messages:

"quick brown fox",
"introduced an ERROR",
"so the lazy dog",
"got a message"

The Logger is just a dummy console writer:

public class Logger : ISubscriber<string>
{
    public void OnCompleted()
    {
    }

    public void OnNext(string message)
    {
        Console.WriteLine(message);
    }
}

So, the Logger reacts on the IPublisher's messages if only they contain an "ERROR" string, without interfering the pipeline.

Happy coding!