Permalink
Browse files

MessageBus - SendStringTwoBoolSubscribersFirstReturnsTrue

  • Loading branch information...
bobbymcr committed Dec 5, 2018
1 parent 974875c commit cc4cefcb761476540bca272e81cba2999bfb9b42
Showing with 36 additions and 3 deletions.
  1. +21 −3 core/src/MessageBus.cs
  2. +15 −0 core/test/MessageBusTest.cs
@@ -37,18 +37,24 @@ public void Send<TMessage>(TMessage message)

private sealed class Subscribers
{
private Func<object, bool> subscribers;
private Action<Message> subscribers;

public IDisposable Add<TMessage>(Func<TMessage, bool> subscriber)
{
Func<object, bool> next = o => subscriber((TMessage)o);
Action<Message> next = m =>
{
if (!m.Consumed)
{
m.Consumed = subscriber((TMessage)m.Data);
}
};
this.subscribers += next;
return new Disposable(() => this.subscribers -= next);
}

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

private sealed class Disposable : IDisposable
@@ -65,6 +71,18 @@ public void Dispose()
this.onDispose();
}
}

private sealed class Message
{
public Message(object data)
{
this.Data = data;
}

public object Data { get; }

public bool Consumed { get; set; }
}
}

private sealed class TypeMap<TValue>
@@ -204,5 +204,20 @@ public void SendStringTwoBoolSubscribersFirstReturnsFalse()

charCount.Should().Be(6);
}

[Fact]
public void SendStringTwoBoolSubscribersFirstReturnsTrue()
{
int charCount = 0;
MessageBus bus = new MessageBus();
Func<string, bool> subscriber1 = m => (charCount += m.Length) == 3;
Func<string, bool> subscriber2 = m => (charCount += m.Length) == 6;

bus.Subscribe(subscriber1);
bus.Subscribe(subscriber2);
bus.Send("abc");

charCount.Should().Be(3);
}
}
}

0 comments on commit cc4cefc

Please sign in to comment.