refactor: Simplify FakeMDDaemon code that delays post

Use Task.Delay/ContinueWith instead of subscribing to the SparkTimer tag.
This commit is contained in:
John Colvin committed 2024-09-13 10:50:21 -04:00
1 parent 9c624a2f79
commit 859dfe3544
1 file changed
+9 -16
+9 -16
View File
@@ -7,7 +7,8 @@ using mbts.ui;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Threading;
using SecurityKey = System.UInt32;
using MDHandle = mbts.co2.FakeMDConsumerHandle;
@@ -18,10 +19,7 @@ namespace bts {
// publishes a fixed set of hard coded securities to the bus, remove the line
// above and this class wont be created
internal class FakeMDDaemon : IShellDaemon, IDisposable {
private readonly IBus _bus;
private IDisposable _dispose;
private bool _published = false;
private IReadOnlyDictionary<uint, SecurityMD> _sec_map = _get_fake_secs();
// Returns a bunch of hard coded SecurityMD
private static Dictionary<uint, SecurityMD> _get_fake_secs() {
@@ -72,19 +70,14 @@ namespace bts {
[ImportingConstructor]
public FakeMDDaemon(IBus bus) {
bts.Log.Gen.info("FakeMDDaemon started");
_bus = bus;
var observable = _bus.subscribe(SparkTags.SparkTimer).OnDispatcher().Subscribe(
d => _on_timer(d));
}
var cts = new CancellationTokenSource();
Task.Delay(TimeSpan.FromSeconds(5), cts.Token)
.ContinueWith(postFakeSecs, TaskContinuationOptions.OnlyOnRanToCompletion);
// 'Dispose' cancels the task to post securities
_dispose = Disposable.Create(cts.Cancel);
private void _on_timer(TimeSpan ts_from_start) {
if (!_published) {
if (ts_from_start.TotalSeconds >= 5) {
_published = true;
bts.Log.Gen.info("FakeMDDaemon publishing md");
_bus.post(SparkTags.FakeSecuritiesTag, _sec_map);
}
}
void postFakeSecs(Task delayTask) =>
bus.post(SparkTags.FakeSecuritiesTag, _get_fake_secs());
}
public void Dispose() =>