4
4
5
5
#if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
6
6
using System . Threading . Tasks ;
7
+ using UniRx . InternalUtil ;
7
8
#endif
8
9
namespace UniRx
9
10
{
10
11
public interface IReactiveCommand < T > : IObservable < T >
11
12
{
12
13
IReadOnlyReactiveProperty < bool > CanExecute { get ; }
13
14
bool Execute ( T parameter ) ;
14
-
15
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
16
- Task < T > WaitUntilExecuteAsync ( CancellationToken cancellationToken ) ;
17
- #endif
18
15
}
19
16
20
17
public interface IAsyncReactiveCommand < T >
21
18
{
22
19
IReadOnlyReactiveProperty < bool > CanExecute { get ; }
23
20
IDisposable Execute ( T parameter ) ;
24
21
IDisposable Subscribe ( Func < T , IObservable < Unit > > asyncAction ) ;
25
-
26
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
27
- Task < T > WaitUntilExecuteAsync ( CancellationToken cancellationToken ) ;
28
- #endif
29
22
}
30
23
31
24
/// <summary>
@@ -103,15 +96,6 @@ public bool Execute(T parameter)
103
96
if ( canExecute . Value )
104
97
{
105
98
trigger . OnNext ( parameter ) ;
106
-
107
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
108
- commonPromise ? . InvokeContinuation ( ref parameter ) ;
109
- if ( removablePromises != null )
110
- {
111
- PromiseHelper . TrySetResultAll ( removablePromises . Values , parameter ) ;
112
- }
113
- #endif
114
-
115
99
return true ;
116
100
}
117
101
else
@@ -144,65 +128,7 @@ public void Dispose()
144
128
trigger . OnCompleted ( ) ;
145
129
trigger . Dispose ( ) ;
146
130
canExecuteSubscription . Dispose ( ) ;
147
-
148
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
149
- commonPromise ? . SetCanceled ( ) ;
150
- commonPromise = null ;
151
- if ( removablePromises != null )
152
- {
153
- foreach ( var item in removablePromises )
154
- {
155
- item . Value . SetCanceled ( ) ;
156
- }
157
- removablePromises = null ;
158
- }
159
- #endif
160
131
}
161
-
162
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
163
-
164
- static readonly Action < object > Callback = CancelCallback ;
165
- ReactivePropertyReusablePromise < T > commonPromise ;
166
- Dictionary < CancellationToken , ReactivePropertyReusablePromise < T > > removablePromises ;
167
-
168
- public UniTask < T > WaitUntilExecuteAsync ( CancellationToken cancellationToken )
169
- {
170
- if ( IsDisposed ) throw new ObjectDisposedException ( "ReadOnlyReactiveProperty" ) ;
171
-
172
- if ( ! cancellationToken . CanBeCanceled )
173
- {
174
- if ( commonPromise != null ) return commonPromise . Task ;
175
- commonPromise = new ReactivePropertyReusablePromise < T > ( CancellationToken . None ) ;
176
- return commonPromise . Task ;
177
- }
178
-
179
- if ( removablePromises == null )
180
- {
181
- removablePromises = new Dictionary < CancellationToken , ReactivePropertyReusablePromise < T > > ( CancellationTokenEqualityComparer . Default ) ;
182
- }
183
-
184
- if ( removablePromises . TryGetValue ( cancellationToken , out var newPromise ) )
185
- {
186
- return newPromise . Task ;
187
- }
188
-
189
- newPromise = new ReactivePropertyReusablePromise < T > ( cancellationToken ) ;
190
- removablePromises . Add ( cancellationToken , newPromise ) ;
191
- cancellationToken . RegisterWithoutCaptureExecutionContext ( Callback , Tuple . Create ( this , newPromise ) ) ;
192
-
193
- return newPromise . Task ;
194
- }
195
-
196
- static void CancelCallback ( object state )
197
- {
198
- var tuple = ( Tuple < ReactiveCommand < T > , ReactivePropertyReusablePromise < T > > ) state ;
199
- if ( tuple . Item1 . IsDisposed ) return ;
200
-
201
- tuple . Item2 . SetCanceled ( ) ;
202
- tuple . Item1 . removablePromises . Remove ( tuple . Item2 . RegisteredCancelationToken ) ;
203
- }
204
-
205
- #endif
206
132
}
207
133
208
134
/// <summary>
@@ -302,14 +228,6 @@ public IDisposable Execute(T parameter)
302
228
{
303
229
try
304
230
{
305
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
306
- commonPromise ? . InvokeContinuation ( ref parameter ) ;
307
- if ( removablePromises != null )
308
- {
309
- PromiseHelper . TrySetResultAll ( removablePromises . Values , parameter ) ;
310
- }
311
- #endif
312
-
313
231
var asyncState = a [ 0 ] . Invoke ( parameter ) ?? Observable . ReturnUnit ( ) ;
314
232
return asyncState . Finally ( ( ) => canExecuteSource . Value = true ) . Subscribe ( ) ;
315
233
}
@@ -324,14 +242,6 @@ public IDisposable Execute(T parameter)
324
242
var xs = new IObservable < Unit > [ a . Length ] ;
325
243
try
326
244
{
327
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
328
- commonPromise ? . InvokeContinuation ( ref parameter ) ;
329
- if ( removablePromises != null )
330
- {
331
- PromiseHelper . TrySetResultAll ( removablePromises . Values , parameter ) ;
332
- }
333
- #endif
334
-
335
245
for ( int i = 0 ; i < a . Length ; i ++ )
336
246
{
337
247
xs [ i ] = a [ i ] . Invoke ( parameter ) ?? Observable . ReturnUnit ( ) ;
@@ -372,66 +282,7 @@ public void Dispose()
372
282
373
283
IsDisposed = true ;
374
284
asyncActions = UniRx . InternalUtil . ImmutableList < Func < T , IObservable < Unit > > > . Empty ;
375
-
376
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
377
- commonPromise ? . SetCanceled ( ) ;
378
- commonPromise = null ;
379
- if ( removablePromises != null )
380
- {
381
- foreach ( var item in removablePromises )
382
- {
383
- item . Value . SetCanceled ( ) ;
384
- }
385
- removablePromises = null ;
386
- }
387
- #endif
388
- }
389
-
390
- #if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
391
-
392
- static readonly Action < object > Callback = CancelCallback ;
393
- ReactivePropertyReusablePromise < T > commonPromise ;
394
- Dictionary < CancellationToken , ReactivePropertyReusablePromise < T > > removablePromises ;
395
-
396
- public UniTask < T > WaitUntilExecuteAsync ( CancellationToken cancellationToken )
397
- {
398
- if ( IsDisposed ) throw new ObjectDisposedException ( "ReadOnlyReactiveProperty" ) ;
399
-
400
- if ( ! cancellationToken . CanBeCanceled )
401
- {
402
- if ( commonPromise != null ) return commonPromise . Task ;
403
- commonPromise = new ReactivePropertyReusablePromise < T > ( CancellationToken . None ) ;
404
- return commonPromise . Task ;
405
- }
406
-
407
- if ( removablePromises == null )
408
- {
409
- removablePromises = new Dictionary < CancellationToken , ReactivePropertyReusablePromise < T > > ( CancellationTokenEqualityComparer . Default ) ;
410
- }
411
-
412
- if ( removablePromises . TryGetValue ( cancellationToken , out var newPromise ) )
413
- {
414
- return newPromise . Task ;
415
- }
416
-
417
- newPromise = new ReactivePropertyReusablePromise < T > ( cancellationToken ) ;
418
- removablePromises . Add ( cancellationToken , newPromise ) ;
419
- cancellationToken . RegisterWithoutCaptureExecutionContext ( Callback , Tuple . Create ( this , newPromise ) ) ;
420
-
421
- return newPromise . Task ;
422
285
}
423
-
424
- static void CancelCallback ( object state )
425
- {
426
- var tuple = ( Tuple < AsyncReactiveCommand < T > , ReactivePropertyReusablePromise < T > > ) state ;
427
- if ( tuple . Item1 . IsDisposed ) return ;
428
-
429
- tuple . Item2 . SetCanceled ( ) ;
430
- tuple . Item1 . removablePromises . Remove ( tuple . Item2 . RegisteredCancelationToken ) ;
431
- }
432
-
433
- #endif
434
-
435
286
class Subscription : IDisposable
436
287
{
437
288
readonly AsyncReactiveCommand < T > parent ;
@@ -473,7 +324,26 @@ public static ReactiveCommand<T> ToReactiveCommand<T>(this IObservable<bool> can
473
324
474
325
#if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
475
326
476
- public static UniTask < T > . Awaiter GetAwaiter < T > ( this IReactiveCommand < T > command )
327
+ static readonly Action < object > Callback = CancelCallback ;
328
+
329
+ static void CancelCallback ( object state )
330
+ {
331
+ var tuple = ( Tuple < ICancellableTaskCompletionSource , IDisposable > ) state ;
332
+ tuple . Item2 . Dispose ( ) ;
333
+ tuple . Item1 . TrySetCanceled ( ) ;
334
+ }
335
+
336
+ public static Task < T > WaitUntilExecuteAsync < T > ( this IReactiveCommand < T > source , CancellationToken cancellationToken = default ( CancellationToken ) )
337
+ {
338
+ var tcs = new CancellableTaskCompletionSource < T > ( ) ;
339
+
340
+ var subscription = source . Subscribe ( x => tcs . TrySetResult ( x ) , ex => tcs . TrySetException ( ex ) , ( ) => tcs . TrySetCanceled ( ) ) ;
341
+ cancellationToken . Register ( Callback , Tuple . Create ( tcs , subscription ) , false ) ;
342
+
343
+ return tcs . Task ;
344
+ }
345
+
346
+ public static System . Runtime . CompilerServices . TaskAwaiter < T > GetAwaiter < T > ( this IReactiveCommand < T > command )
477
347
{
478
348
return command . WaitUntilExecuteAsync ( CancellationToken . None ) . GetAwaiter ( ) ;
479
349
}
@@ -534,13 +404,33 @@ public static AsyncReactiveCommand<T> ToAsyncReactiveCommand<T>(this IReactivePr
534
404
535
405
#if CSHARP_7_OR_LATER || ( UNITY_2018_3_OR_NEWER && ( NET_STANDARD_2_0 || NET_4_6 ) )
536
406
537
- public static UniTask < T > . Awaiter GetAwaiter < T > ( this IAsyncReactiveCommand < T > command )
407
+ static readonly Action < object > Callback = CancelCallback ;
408
+
409
+ static void CancelCallback ( object state )
410
+ {
411
+ var tuple = ( Tuple < ICancellableTaskCompletionSource , IDisposable > ) state ;
412
+ tuple . Item2 . Dispose ( ) ;
413
+ tuple . Item1 . TrySetCanceled ( ) ;
414
+ }
415
+
416
+ public static Task < T > WaitUntilExecuteAsync < T > ( this IAsyncReactiveCommand < T > source , CancellationToken cancellationToken = default ( CancellationToken ) )
417
+ {
418
+ var tcs = new CancellableTaskCompletionSource < T > ( ) ;
419
+
420
+ var subscription = source . Subscribe ( x => { tcs . TrySetResult ( x ) ; return Observable . ReturnUnit ( ) ; } ) ;
421
+ cancellationToken . Register ( Callback , Tuple . Create ( tcs , subscription ) , false ) ;
422
+
423
+ return tcs . Task ;
424
+ }
425
+
426
+ public static System . Runtime . CompilerServices . TaskAwaiter < T > GetAwaiter < T > ( this IAsyncReactiveCommand < T > command )
538
427
{
539
428
return command . WaitUntilExecuteAsync ( CancellationToken . None ) . GetAwaiter ( ) ;
540
429
}
541
430
542
431
#endif
543
432
433
+
544
434
#if ! UniRxLibrary
545
435
546
436
// for uGUI(from 4.6)
0 commit comments