Permalink
Browse files

MessageBus - SendStringOneBoolSubscriber

  • Loading branch information...
bobbymcr committed Dec 5, 2018
1 parent 27e9415 commit 52f65dd351c74e773c2e2675ffe0adc51ce4e44c
Showing with 26 additions and 4 deletions.
  1. +12 −3 core/src/MessageBus.cs
  2. +1 −1 core/src/Room.cs
  3. +13 −0 core/test/MessageBusTest.cs
@@ -17,6 +17,15 @@ public MessageBus()
}

public IDisposable Subscribe<TMessage>(Action<TMessage> subscriber)
{
return this.Subscribe<TMessage>(m =>
{
subscriber(m);
return false;
});
}

public IDisposable Subscribe<TMessage>(Func<TMessage, bool> subscriber)
{
return this.subscribers[typeof(TMessage)].Add(subscriber);
}
@@ -28,11 +37,11 @@ public void Send<TMessage>(TMessage message)

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

public IDisposable Add<TMessage>(Action<TMessage> subscriber)
public IDisposable Add<TMessage>(Func<TMessage, bool> subscriber)
{
Action<object> next = o => subscriber((TMessage)o);
Func<object, bool> next = o => subscriber((TMessage)o);
this.subscribers += next;
return new Disposable(() => this.subscribers -= next);
}
@@ -27,7 +27,7 @@ public void Enter()
throw new InvalidOperationException("Cannot Enter again.");
}

this.sub = this.bus.Subscribe<SentenceMessage>(this.Process);
this.sub = this.bus.Subscribe<SentenceMessage>(m => this.Process(m));
this.EnterCore();
}

@@ -176,5 +176,18 @@ public void ThreeSubscribersUnsubscribeSecond()

received.Should().Equal("S1=hello", "S3=hello");
}

[Fact]
public void SendStringOneBoolSubscriber()
{
bool received = false;
MessageBus bus = new MessageBus();
Func<string, bool> subscriber = m => received = bool.Parse(m);

bus.Subscribe(subscriber);
bus.Send("true");

received.Should().BeTrue();
}
}
}

0 comments on commit 52f65dd

Please sign in to comment.