Permalink
Browse files

Room - UnsubscribeOnLeave

  • Loading branch information...
bobbymcr committed Dec 2, 2018
1 parent 590414b commit 77f0af89e1b524a59f44d1d2538cd7c0488f789f
Showing with 24 additions and 1 deletion.
  1. +8 −1 core/src/Room.cs
  2. +16 −0 core/test/RoomTest.cs
@@ -12,6 +12,8 @@ public abstract class Room
private readonly MessageBus bus;
private readonly Dictionary<string, Action<Word, Word>> verbs;

private IDisposable sub;

protected Room(MessageBus bus)
{
this.bus = bus;
@@ -20,10 +22,15 @@ protected Room(MessageBus bus)

public void Enter()
{
this.bus.Subscribe<SentenceMessage>(this.Process);
this.sub = this.bus.Subscribe<SentenceMessage>(this.Process);
this.EnterCore();
}

public void Leave()
{
this.sub.Dispose();
}

protected virtual void EnterCore()
{
}
@@ -26,6 +26,22 @@ public void RegisterOnEnter()
output.Should().ContainSingle().Which.Should().Be("Hello, world!");
}

[Fact]
public void UnsubscribeOnLeave()
{
MessageBus bus = new MessageBus();
List<string> output = new List<string>();
Action<OutputMessage> subscriber = m => output.Add(m.Text);
bus.Subscribe(subscriber);
Room room = new TestRoom(bus);

room.Enter();
room.Leave();
bus.Send(new SentenceMessage(new Word("hello", "hello"), new Word("world", "world")));

output.Should().BeEmpty();
}

private sealed class TestRoom : Room
{
public TestRoom(MessageBus bus)

0 comments on commit 77f0af8

Please sign in to comment.