44
55namespace CatCode . Commands
66{
7- public static class AsyncCommandExtensions
7+ public static class CommandAsyncExtensions
88 {
99 public static Awaitable StartedToAwaitable ( this ICommand command , CancellationToken token )
1010 {
1111 var tcs = new AwaitableCompletionSource ( ) ;
12+ CancellationTokenRegistration ctr = default ;
13+
1214 if ( command . State > CommandState . Idle )
13- {
1415 tcs . SetResult ( ) ;
15- }
1616 else
1717 {
18- var cts = token . Register ( OnCancel ) ;
18+ ctr = token . Register ( OnCancel ) ;
1919 command . Started += OnStarted ;
2020 }
2121 return tcs . Awaitable ;
2222
2323 void OnStarted ( )
2424 {
2525 command . Started -= OnStarted ;
26+ ctr . Dispose ( ) ;
2627 tcs . TrySetResult ( ) ;
2728 }
2829
2930 void OnCancel ( )
3031 {
3132 command . Started -= OnStarted ;
33+ ctr . Dispose ( ) ;
3234 tcs . TrySetCanceled ( ) ;
3335 }
3436 }
3537
3638 public static Awaitable FinishToAwaitable ( this ICommand command , CancellationToken token )
3739 {
3840 var tcs = new AwaitableCompletionSource ( ) ;
41+ CancellationTokenRegistration ctr = default ;
42+
3943 if ( command . State == CommandState . Finished )
40- {
4144 tcs . SetResult ( ) ;
42- }
4345 else
4446 {
45- var ctr = token . Register ( OnCancel ) ;
47+ ctr = token . Register ( OnCancel ) ;
4648 command . Finished += OnFinished ;
4749 }
4850 return tcs . Awaitable ;
4951
5052 void OnFinished ( )
5153 {
5254 command . Finished -= OnFinished ;
55+ ctr . Dispose ( ) ;
5356 tcs . TrySetResult ( ) ;
5457 }
5558 void OnCancel ( )
5659 {
5760 command . Finished -= OnFinished ;
61+ ctr . Dispose ( ) ;
5862 tcs . TrySetCanceled ( ) ;
5963 }
6064 }
@@ -64,21 +68,23 @@ public static Task StartToTask(this ICommand command, CancellationToken token)
6468 {
6569 if ( command . State > CommandState . Idle )
6670 return Task . CompletedTask ;
67-
6871 var tcs = new TaskCompletionSource < bool > ( ) ;
69- var ctr = token . Register ( OnCancel ) ;
72+ CancellationTokenRegistration ctr = default ;
73+ ctr = token . Register ( OnCancel ) ;
7074 command . Started += OnStarted ;
7175 return tcs . Task ;
7276
7377 void OnStarted ( )
7478 {
7579 command . Started -= OnStarted ;
80+ ctr . Dispose ( ) ;
7681 tcs . TrySetResult ( true ) ;
7782 }
7883
7984 void OnCancel ( )
8085 {
8186 command . Started -= OnStarted ;
87+ ctr . Dispose ( ) ;
8288 tcs . TrySetCanceled ( ) ;
8389 }
8490 }
@@ -87,20 +93,23 @@ public static Task FinishToTask(this ICommand command, CancellationToken token)
8793 {
8894 if ( command . State == CommandState . Finished )
8995 return Task . CompletedTask ;
90-
96+
9197 var tcs = new TaskCompletionSource < bool > ( ) ;
92- var ctr = token . Register ( OnCancel ) ;
98+ CancellationTokenRegistration ctr = default ;
99+ ctr = token . Register ( OnCancel ) ;
93100 command . Finished += OnFinished ;
94101 return tcs . Task ;
95102
96103 void OnFinished ( )
97104 {
98105 command . Finished -= OnFinished ;
106+ ctr . Dispose ( ) ;
99107 tcs . TrySetResult ( true ) ;
100108 }
101109 void OnCancel ( )
102110 {
103111 command . Finished -= OnFinished ;
112+ ctr . Dispose ( ) ;
104113 tcs . TrySetCanceled ( ) ;
105114 }
106115 }
0 commit comments