-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputLoop.cs
39 lines (33 loc) · 1003 Bytes
/
InputLoop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// <copyright file="InputLoop.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>
namespace Adventure
{
using System;
using System.Threading;
using Adventure.Messages;
public sealed class InputLoop : IDisposable
{
private readonly MessageBus bus;
private readonly IDisposable sub;
private readonly string prompt;
private bool inputEnded;
public InputLoop(MessageBus bus, string prompt = null)
{
this.bus = bus;
this.sub = bus.Subscribe<InputEndedMessage>(_ => this.inputEnded = true);
this.prompt = prompt;
}
public void Run(CancellationToken token)
{
while (!this.inputEnded && !token.IsCancellationRequested)
{
this.bus.Send(new InputRequestedMessage(this.prompt));
}
}
public void Dispose()
{
this.sub.Dispose();
}
}
}