-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalScheduler.cs
More file actions
64 lines (54 loc) · 1.68 KB
/
TerminalScheduler.cs
File metadata and controls
64 lines (54 loc) · 1.68 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using Terminal.Gui.App;
namespace ReactiveExample;
public class TerminalScheduler : LocalScheduler
{
private readonly IApplication? _application;
public TerminalScheduler (IApplication? application) { _application = application; }
public override IDisposable Schedule<TState> (
TState state,
TimeSpan dueTime,
Func<IScheduler, TState, IDisposable> action
)
{
IDisposable PostOnMainLoop ()
{
CompositeDisposable composite = new (2);
CancellationDisposable cancellation = new ();
_application?.Invoke (_ =>
{
if (!cancellation.Token.IsCancellationRequested)
{
composite.Add (action (this, state));
}
}
);
composite.Add (cancellation);
return composite;
}
IDisposable PostOnMainLoopAsTimeout ()
{
CompositeDisposable composite = new (2);
var timeout = _application?.AddTimeout (
dueTime,
() =>
{
composite.Add (action (this, state));
return false;
}
);
composite.Add (Disposable.Create (() =>
{
if (timeout is not null)
{
_application?.RemoveTimeout (timeout);
}
}));
return composite;
}
return dueTime == TimeSpan.Zero
? PostOnMainLoop ()
: PostOnMainLoopAsTimeout ();
}
}