| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if HAS_WINRT |
| 4 |
using System.Reactive.Disposables; |
| 5 |
using System.Reactive.Subjects; |
| 6 |
using Windows.Foundation; |
| 7 |
|
| 8 |
namespace System.Reactive.Windows.Foundation |
| 9 |
{ |
| 10 |
class AsyncInfoToObservableBridge<TResult, TProgress> : ObservableBase<TResult> |
| 11 |
{ |
| 12 |
private readonly Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>> _onCompleted; |
| 13 |
private readonly Func<IAsyncInfo, TResult> _getResult; |
| 14 |
private readonly AsyncSubject<TResult> _subject; |
| 15 |
|
| 16 |
public AsyncInfoToObservableBridge(IAsyncInfo info, Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>> onCompleted, Func<IAsyncInfo, TResult> getResult, Action<IAsyncInfo, Action<IAsyncInfo, TProgress>> onProgress, IProgress<TProgress> progress, bool multiValue) |
| 17 |
{ |
| 18 |
_onCompleted = onCompleted; |
| 19 |
_getResult = getResult; |
| 20 |
|
| 21 |
_subject = new AsyncSubject<TResult>(); |
| 22 |
|
| 23 |
if (onProgress != null) |
| 24 |
{ |
| 25 |
onProgress(info, (iai, p) => |
| 26 |
{ |
| 27 |
if (multiValue && getResult != null) |
| 28 |
_subject.OnNext(getResult(iai)); |
| 29 |
|
| 30 |
if (progress != null) |
| 31 |
progress.Report(p); |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
Done(info, info.Status, true); |
| 36 |
} |
| 37 |
|
| 38 |
private void Done(IAsyncInfo info, AsyncStatus status, bool initial) |
| 39 |
{ |
| 40 |
var error = default(Exception); |
| 41 |
var result = default(TResult); |
| 42 |
|
| 43 |
// |
| 44 |
// Initial interactions with the IAsyncInfo object. Those could fail, which indicates |
| 45 |
// a rogue implementation. Failure is just propagated out. |
| 46 |
// |
| 47 |
switch (status) |
| 48 |
{ |
| 49 |
case AsyncStatus.Error: |
| 50 |
error = info.ErrorCode; |
| 51 |
if (error == null) |
| 52 |
throw new InvalidOperationException("The asynchronous operation failed with a null error code."); |
| 53 |
break; |
| 54 |
case AsyncStatus.Canceled: |
| 55 |
error = new OperationCanceledException(); |
| 56 |
break; |
| 57 |
case AsyncStatus.Completed: |
| 58 |
if (_getResult != null) |
| 59 |
result = _getResult(info); |
| 60 |
break; |
| 61 |
default: |
| 62 |
if (!initial) |
| 63 |
throw new InvalidOperationException("The asynchronous operation completed unexpectedly."); |
| 64 |
|
| 65 |
_onCompleted(info, (iai, s) => Done(iai, s, false)); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// |
| 70 |
// Close as early as possible, before running continuations which could fail. In case of |
| 71 |
// failure above, we don't close out the object in order to allow for debugging of the |
| 72 |
// rogue implementation without losing state prematurely. Notice _getResults is merely |
| 73 |
// an indirect call to the appropriate GetResults method, which is not supposed to throw. |
| 74 |
// Instead, an Error status should be returned. |
| 75 |
// |
| 76 |
info.Close(); |
| 77 |
|
| 78 |
// |
| 79 |
// Now we run the continuations, which could take a long time. Failure here is catastrophic |
| 80 |
// and under control of the upstream subscriber. |
| 81 |
// |
| 82 |
if (error != null) |
| 83 |
{ |
| 84 |
_subject.OnError(error); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
if (_getResult != null) |
| 89 |
_subject.OnNext(result); |
| 90 |
|
| 91 |
_subject.OnCompleted(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
protected override IDisposable SubscribeCore(IObserver<TResult> observer) |
| 96 |
{ |
| 97 |
return _subject.Subscribe(observer); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
#endif |