Skip to content

Commit 854c05f

Browse files
authored
Added Scheduler for Terminal (#103)
1 parent a2ecc6c commit 854c05f

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/ReactiveMvvm.Terminal/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Reactive.Concurrency;
23
using ReactiveMvvm.Services;
34
using ReactiveMvvm.Terminal.Services;
45
using ReactiveMvvm.Terminal.Views;
56
using ReactiveMvvm.ViewModels;
7+
using ReactiveUI;
68
using Terminal.Gui;
79

810
namespace ReactiveMvvm.Terminal
@@ -12,6 +14,8 @@ public static class Program
1214
public static void Main(string[] args)
1315
{
1416
Application.Init();
17+
RxApp.MainThreadScheduler = TerminalScheduler.Default;
18+
RxApp.TaskpoolScheduler = TaskPoolScheduler.Default;
1519
Application.Run(
1620
new FeedbackView(
1721
new(
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Reactive.Concurrency;
3+
using System.Reactive.Disposables;
4+
using Terminal.Gui;
5+
6+
namespace ReactiveMvvm;
7+
8+
public class TerminalScheduler : LocalScheduler
9+
{
10+
public static readonly TerminalScheduler Default = new ();
11+
private TerminalScheduler () { }
12+
13+
public override IDisposable Schedule<TState> (
14+
TState state,
15+
TimeSpan dueTime,
16+
Func<IScheduler, TState, IDisposable> action
17+
)
18+
{
19+
IDisposable PostOnMainLoop ()
20+
{
21+
var composite = new CompositeDisposable (2);
22+
var cancellation = new CancellationDisposable ();
23+
24+
Application.Invoke (
25+
() =>
26+
{
27+
if (!cancellation.Token.IsCancellationRequested)
28+
{
29+
composite.Add (action (this, state));
30+
}
31+
}
32+
);
33+
composite.Add (cancellation);
34+
35+
return composite;
36+
}
37+
38+
IDisposable PostOnMainLoopAsTimeout ()
39+
{
40+
var composite = new CompositeDisposable (2);
41+
42+
object timeout = Application.AddTimeout (
43+
dueTime,
44+
() =>
45+
{
46+
composite.Add (action (this, state));
47+
48+
return false;
49+
}
50+
);
51+
composite.Add (Disposable.Create (() => Application.RemoveTimeout (timeout)));
52+
53+
return composite;
54+
}
55+
56+
return dueTime == TimeSpan.Zero
57+
? PostOnMainLoop ()
58+
: PostOnMainLoopAsTimeout ();
59+
}
60+
}

0 commit comments

Comments
 (0)