/
AsyncOperationExtensions.cs
75 lines (68 loc) · 2.44 KB
/
AsyncOperationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections;
using System.Threading;
using UnityEngine;
#if !UniRxLibrary
using ObservableUnity = UniRx.Observable;
#endif
namespace UniRx
{
public static partial class AsyncOperationExtensions
{
/// <summary>
/// If you needs return value, use AsAsyncOperationObservable instead.
/// </summary>
public static IObservable<AsyncOperation> AsObservable(this AsyncOperation asyncOperation, IProgress<float> progress = null)
{
return ObservableUnity.FromCoroutine<AsyncOperation>((observer, cancellation) => AsObservableCore(asyncOperation, observer, progress, cancellation));
}
// T: where T : AsyncOperation is ambigious with IObservable<T>.AsObservable
public static IObservable<T> AsAsyncOperationObservable<T>(this T asyncOperation, IProgress<float> progress = null)
where T : AsyncOperation
{
return ObservableUnity.FromCoroutine<T>((observer, cancellation) => AsObservableCore(asyncOperation, observer, progress, cancellation));
}
static IEnumerator AsObservableCore<T>(T asyncOperation, IObserver<T> observer, IProgress<float> reportProgress, CancellationToken cancel)
where T : AsyncOperation
{
if (reportProgress != null)
{
while (!asyncOperation.isDone && !cancel.IsCancellationRequested)
{
try
{
reportProgress.Report(asyncOperation.progress);
}
catch (Exception ex)
{
observer.OnError(ex);
yield break;
}
yield return null;
}
}
else
{
if (!asyncOperation.isDone)
{
yield return asyncOperation;
}
}
if (cancel.IsCancellationRequested) yield break;
if (reportProgress != null)
{
try
{
reportProgress.Report(asyncOperation.progress);
}
catch (Exception ex)
{
observer.OnError(ex);
yield break;
}
}
observer.OnNext(asyncOperation);
observer.OnCompleted();
}
}
}