-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Brent Bulla edited this page Oct 7, 2018
·
1 revision
See the Broadcaster.Tests project for more information.
public class SimpleClass
{
public SimpleClass()
{
Listener = new Listener<Simple>(Broadcaster);
}
public enum Simple
{
One,
Two,
Three
}
// keep the broadcaster private so no one else can publish your messages
Broadcaster<Simple> Broadcaster { get; } = new Broadcaster<Simple>();
// make your Listener public so that other objects can subscribe
public Listener<Simple> Listener { get; }
}
public class SomeOtherClass : IDisposable
{
IDisposable _token = null;
public SomeOtherClass(SimpleClass loudMouth)
{
_token = loudMouth.Listener.Listen(SimpleClass.Simple.One, OneHandler);
}
private async Task OneHandler() { ... do something ... }
public void Dispose()
{
_token.Dispose();
}
}
So when a referenced SimpleClass instance fires the 'One' event, the corresponding instance of SomeOtherClass reacts to it. The token should be disposed when you no longer want to subscribe to the event. The handler could be a Func lambda (like 'async () => DoSomething()'), as well, not a full-fledged declared method.
Happy coding!