-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2ecc6c
commit 854c05f
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Reactive.Concurrency; | ||
using System.Reactive.Disposables; | ||
using Terminal.Gui; | ||
|
||
namespace ReactiveMvvm; | ||
|
||
public class TerminalScheduler : LocalScheduler | ||
{ | ||
public static readonly TerminalScheduler Default = new (); | ||
private TerminalScheduler () { } | ||
|
||
public override IDisposable Schedule<TState> ( | ||
TState state, | ||
TimeSpan dueTime, | ||
Func<IScheduler, TState, IDisposable> action | ||
) | ||
{ | ||
IDisposable PostOnMainLoop () | ||
{ | ||
var composite = new CompositeDisposable (2); | ||
var cancellation = new CancellationDisposable (); | ||
|
||
Application.Invoke ( | ||
() => | ||
{ | ||
if (!cancellation.Token.IsCancellationRequested) | ||
{ | ||
composite.Add (action (this, state)); | ||
} | ||
} | ||
); | ||
composite.Add (cancellation); | ||
|
||
return composite; | ||
} | ||
|
||
IDisposable PostOnMainLoopAsTimeout () | ||
{ | ||
var composite = new CompositeDisposable (2); | ||
|
||
object timeout = Application.AddTimeout ( | ||
dueTime, | ||
() => | ||
{ | ||
composite.Add (action (this, state)); | ||
|
||
return false; | ||
} | ||
); | ||
composite.Add (Disposable.Create (() => Application.RemoveTimeout (timeout))); | ||
|
||
return composite; | ||
} | ||
|
||
return dueTime == TimeSpan.Zero | ||
? PostOnMainLoop () | ||
: PostOnMainLoopAsTimeout (); | ||
} | ||
} |