Permalink
Browse files

MessageBus - refactor Subscription -> more general Disposable

  • Loading branch information...
bobbymcr committed Nov 26, 2018
1 parent c3f8df7 commit 1df769f0994ff31edcdcd55c6aaf929dcda3c0c0
Showing with 6 additions and 8 deletions.
  1. +6 −8 core/src/MessageBus.cs
@@ -34,28 +34,26 @@ public IDisposable Add<TMessage>(Action<TMessage> subscriber)
{
Action<object> next = o => subscriber((TMessage)o);
this.subscribers += next;
return new Subscription(this, next);
return new Disposable(() => this.subscribers -= next);
}

public void Invoke<TMessage>(TMessage message)
{
this.subscribers?.Invoke(message);
}

private sealed class Subscription : IDisposable
private sealed class Disposable : IDisposable
{
private readonly Subscribers parent;
private readonly Action<object> self;
private readonly Action onDispose;

public Subscription(Subscribers parent, Action<object> self)
public Disposable(Action onDispose)
{
this.parent = parent;
this.self = self;
this.onDispose = onDispose;
}

public void Dispose()
{
this.parent.subscribers -= this.self;
this.onDispose();
}
}
}

0 comments on commit 1df769f

Please sign in to comment.