1
1
using GitHub . Logging ;
2
2
using System ;
3
- using System . Threading ;
4
3
using System . Threading . Tasks ;
5
4
6
5
namespace GitHub . Unity
7
6
{
8
7
static class TaskExtensions
9
8
{
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
-
59
9
public static async Task StartAwait ( this ITask source , Action < Exception > handler = null )
60
10
{
61
11
try
@@ -86,41 +36,6 @@ public static async Task<T> StartAwait<T>(this ITask<T> source, Func<Exception,
86
36
}
87
37
}
88
38
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
-
124
39
public static ITask Then ( this ITask task , Action continuation , TaskAffinity affinity = TaskAffinity . Concurrent , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
125
40
{
126
41
Guard . ArgumentNotNull ( continuation , "continuation" ) ;
@@ -133,13 +48,6 @@ public static ITask Then(this ITask task, Action<bool> continuation, TaskAffinit
133
48
return task . Then ( new ActionTask ( task . Token , continuation ) { Affinity = affinity , Name = "Then" } , runOptions ) ;
134
49
}
135
50
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
-
143
51
public static ITask Then < T > ( this ITask < T > task , Action < bool , T > continuation , TaskAffinity affinity = TaskAffinity . Concurrent , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
144
52
{
145
53
Guard . ArgumentNotNull ( continuation , "continuation" ) ;
@@ -164,11 +72,6 @@ public static ITask<T> Then<T>(this ITask task, Task<T> continuation, TaskAffini
164
72
return task . Then ( cont , runOptions ) ;
165
73
}
166
74
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
-
172
75
public static ITask ThenInUI ( this ITask task , Action continuation , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
173
76
{
174
77
return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
@@ -184,11 +87,6 @@ public static ITask ThenInUI<T>(this ITask<T> task, Action<bool, T> continuation
184
87
return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
185
88
}
186
89
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
-
192
90
public static ITask < TRet > ThenInUI < T , TRet > ( this ITask < T > task , Func < bool , T , TRet > continuation , TaskRunOptions runOptions = TaskRunOptions . OnSuccess )
193
91
{
194
92
return task . Then ( continuation , TaskAffinity . UI , runOptions ) ;
@@ -200,27 +98,11 @@ public static ITask FinallyInUI<T>(this T task, Action<bool, Exception> continua
200
98
return task . Finally ( continuation , TaskAffinity . UI ) ;
201
99
}
202
100
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
-
209
101
public static ITask FinallyInUI < T > ( this ITask < T > task , Action < bool , Exception , T > continuation )
210
102
{
211
103
return task . Finally ( continuation , TaskAffinity . UI ) ;
212
104
}
213
105
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
-
224
106
public static Task < T > StartAsAsync < T > ( this ITask < T > task )
225
107
{
226
108
var tcs = new TaskCompletionSource < T > ( ) ;
0 commit comments