Permalink
Browse files

TextConsole - refactor Run / ReadLine(s)

  • Loading branch information...
bobbymcr committed Nov 30, 2018
1 parent 20e8d37 commit 19d1c7e52891545a11b13cb2223d6a09b8e845d7
Showing with 28 additions and 8 deletions.
  1. +28 −8 core/src/TextConsole.cs
@@ -4,6 +4,7 @@

namespace Adventure
{
using System;
using System.IO;
using System.Threading;

@@ -24,17 +25,36 @@ public void Run(CancellationToken token)
{
using (this.bus.Subscribe<OutputMessage>(m => this.writer.WriteLine(m.Text)))
{
string line;
do
try
{
line = this.reader.ReadLine();
if (line != null)
{
this.bus.Send(new InputMessage(line));
}
this.ReadLines(token);
}
while (!token.IsCancellationRequested && (line != null));
catch (OperationCanceledException)
{
}
}
}

private void ReadLines(CancellationToken token)
{
string line;
do
{
line = this.ReadLine(token);
}
while (line != null);
}

private string ReadLine(CancellationToken token)
{
token.ThrowIfCancellationRequested();
string line = this.reader.ReadLine();
if (line != null)
{
this.bus.Send(new InputMessage(line));
}

return line;
}
}
}

0 comments on commit 19d1c7e

Please sign in to comment.