11using GitHub . Logging ;
22using System ;
3- using System . Threading ;
43using System . Threading . Tasks ;
54
65namespace GitHub . Unity
76{
87 static class TaskExtensions
98 {
10- private static Task completedTask ;
11-
12- public static Task CompletedTask
13- {
14- get
15- {
16- if ( completedTask == null )
17- {
18- completedTask = TaskEx . FromResult ( true ) ;
19- }
20- return completedTask ;
21- }
22- }
23-
24- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Usage" , "CA1801:ReviewUnusedParameters" , MessageId = "task" ) ]
25- public static void Forget ( this Task task )
26- {
27- }
28-
29- public static async Task SafeAwait ( this Task source , Action < Exception > handler = null )
30- {
31- try
32- {
33- await source ;
34- }
35- catch ( Exception ex )
36- {
37- LogHelper . GetLogger ( ) . Error ( ex ) ;
38- if ( handler == null )
39- throw ;
40- handler ( ex ) ;
41- }
42- }
43-
44- public static async Task < T > SafeAwait < T > ( this Task < T > source , Func < Exception , T > handler = null )
45- {
46- try
47- {
48- return await source ;
49- }
50- catch ( Exception ex )
51- {
52- LogHelper . GetLogger ( ) . Error ( ex ) ;
53- if ( handler == null )
54- throw ;
55- return handler ( ex ) ;
56- }
57- }
58-
599 public static async Task StartAwait ( this ITask source , Action < Exception > handler = null )
6010 {
6111 try
@@ -86,41 +36,6 @@ public static async Task<T> StartAwait<T>(this ITask<T> source, Func<Exception,
8636 }
8737 }
8838
89- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Usage" , "CA1801:ReviewUnusedParameters" , MessageId = "task" ) ]
90- public static void Forget ( this ITask task )
91- {
92- task . Task . Forget ( ) ;
93- }
94-
95- //http://stackoverflow.com/a/29491927
96- public static Action < T > Debounce < T > ( this Action < T > func , int milliseconds = 300 )
97- {
98- var last = 0 ;
99- return arg =>
100- {
101- var current = Interlocked . Increment ( ref last ) ;
102- TaskEx . Delay ( milliseconds ) . ContinueWith ( task =>
103- {
104- if ( current == last ) func ( arg ) ;
105- task . Dispose ( ) ;
106- } ) ;
107- } ;
108- }
109-
110- public static Action Debounce ( this Action func , int milliseconds = 300 )
111- {
112- var last = 0 ;
113- return ( ) =>
114- {
115- var current = Interlocked . Increment ( ref last ) ;
116- TaskEx . Delay ( milliseconds ) . ContinueWith ( task =>
117- {
118- if ( current == last ) func ( ) ;
119- task . Dispose ( ) ;
120- } ) ;
121- } ;
122- }
123-
12439 public static ITask Then ( this ITask task , Action continuation , TaskAffinity affinity = TaskAffinity . Concurrent , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
12540 {
12641 Guard . ArgumentNotNull ( continuation , "continuation" ) ;
@@ -133,13 +48,6 @@ public static ITask Then(this ITask task, Action<bool> continuation, TaskAffinit
13348 return task . Then ( new ActionTask ( task . Token , continuation ) { Affinity = affinity , Name = "Then" } , runOptions ) ;
13449 }
13550
136- public static ITask Then < T > ( this ITask task , ActionTask < T > nextTask , T valueForNextTask , TaskAffinity affinity = TaskAffinity . Concurrent , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
137- {
138- Guard . ArgumentNotNull ( nextTask , nameof ( nextTask ) ) ;
139- nextTask . PreviousResult = valueForNextTask ;
140- return task . Then ( nextTask , runOptions ) ;
141- }
142-
14351 public static ITask Then < T > ( this ITask < T > task , Action < bool , T > continuation , TaskAffinity affinity = TaskAffinity . Concurrent , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
14452 {
14553 Guard . ArgumentNotNull ( continuation , "continuation" ) ;
@@ -164,11 +72,6 @@ public static ITask<T> Then<T>(this ITask task, Task<T> continuation, TaskAffini
16472 return task . Then ( cont , runOptions ) ;
16573 }
16674
167- public static ITask < T > Then < T > ( this ITask task , Func < Task < T > > continuation , TaskAffinity affinity = TaskAffinity . Concurrent , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
168- {
169- return task . Then ( continuation ( ) , affinity , runOptions ) ;
170- }
171-
17275 public static ITask ThenInUI ( this ITask task , Action continuation , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
17376 {
17477 return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
@@ -184,11 +87,6 @@ public static ITask ThenInUI<T>(this ITask<T> task, Action<bool, T> continuation
18487 return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
18588 }
18689
187- public static ITask < T > ThenInUI < T > ( this ITask task , Func < bool , T > continuation , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
188- {
189- return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
190- }
191-
19290 public static ITask < TRet > ThenInUI < T , TRet > ( this ITask < T > task , Func < bool , T , TRet > continuation , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
19391 {
19492 return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
@@ -200,27 +98,11 @@ public static ITask FinallyInUI<T>(this T task, Action<bool, Exception> continua
20098 return task . Finally ( continuation , TaskAffinity . UI ) ;
20199 }
202100
203- public static ITask FinallyInUI < T > ( this T task , Action continuation )
204- where T : ITask
205- {
206- return task . Finally ( ( s , e ) => continuation ( ) , TaskAffinity . UI ) ;
207- }
208-
209101 public static ITask FinallyInUI < T > ( this ITask < T > task , Action < bool , Exception , T > continuation )
210102 {
211103 return task . Finally ( continuation , TaskAffinity . UI ) ;
212104 }
213105
214- public static ITask < T > FinallyInUI < T > ( this ITask < T > task , Func < T > continuation )
215- {
216- return task . Finally ( ( s , e , r ) => continuation ( ) , TaskAffinity . UI ) ;
217- }
218-
219- public static ITask < T > FinallyInUI < T > ( this ITask < T > task , Func < bool , Exception , T , T > continuation )
220- {
221- return task . Finally ( continuation , TaskAffinity . UI ) ;
222- }
223-
224106 public static Task < T > StartAsAsync < T > ( this ITask < T > task )
225107 {
226108 var tcs = new TaskCompletionSource < T > ( ) ;
0 commit comments