AsyncInfoObservableExtensions
Provides conversions from Windows Runtime asynchronous actions and operations to observable sequences.
            
                using System.Reactive.Linq;
using System.Runtime.CompilerServices;
using Windows.Foundation;
namespace System.Reactive.Windows.Foundation
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    [CLSCompliant(false)]
    public static class AsyncInfoObservableExtensions
    {
        public static IObservable<Unit> ToObservable(this IAsyncAction source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return new AsyncInfoToObservableBridge<Unit, Unit>(source, delegate(IAsyncInfo iai, Action<IAsyncInfo, AsyncStatus> a) {
                IAsyncAction obj = (IAsyncAction)iai;
                obj.Completed = (AsyncActionCompletedHandler)Delegate.Combine(obj.Completed, (AsyncActionCompletedHandler)delegate(IAsyncAction iaa, AsyncStatus status) {
                    a(iaa, status);
                });
            }, (IAsyncInfo iai) => Unit.Default, null, null, false);
        }
        public static IObservable<Unit> ToObservable<[System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncActionWithProgress<TProgress> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return source.ToObservable_(null);
        }
        public static IObservable<Unit> ToObservable<[System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncActionWithProgress<TProgress> source, IProgress<TProgress> progress)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (progress == null)
                throw new ArgumentNullException("progress");
            return source.ToObservable_(progress);
        }
        public static IObservable<TProgress> ToObservableProgress<[System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncActionWithProgress<TProgress> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return Observable.Create(delegate(IObserver<TProgress> observer) {
                IProgress<TProgress> progress = Observer.ToProgress<TProgress>(observer);
                return ObservableExtensions.Subscribe<Unit>(AsyncInfoObservableExtensions.ToObservable_<TProgress>(source, progress), (Action<Unit>)delegate {
                }, (Action<Exception>)observer.OnError, (Action)observer.OnCompleted);
            });
        }
        private static IObservable<Unit> ToObservable_<[System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncActionWithProgress<TProgress> source, [System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })] IProgress<TProgress> progress)
        {
            return new AsyncInfoToObservableBridge<Unit, TProgress>((IAsyncInfo)source, (Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>>)delegate(IAsyncInfo iai, Action<IAsyncInfo, AsyncStatus> a) {
                IAsyncActionWithProgress<TProgress> obj2 = (IAsyncActionWithProgress<TProgress>)iai;
                obj2.Completed = (AsyncActionWithProgressCompletedHandler<TProgress>)Delegate.Combine(obj2.Completed, (AsyncActionWithProgressCompletedHandler<TProgress>)delegate(IAsyncActionWithProgress<TProgress> iaa, AsyncStatus status) {
                    a(iaa, status);
                });
            }, (Func<IAsyncInfo, Unit>)((IAsyncInfo iai) => Unit.Default), (Action<IAsyncInfo, Action<IAsyncInfo, TProgress>>)delegate(IAsyncInfo iai, Action<IAsyncInfo, TProgress> a) {
                IAsyncActionWithProgress<TProgress> obj = (IAsyncActionWithProgress<TProgress>)iai;
                obj.Progress = (AsyncActionProgressHandler<TProgress>)Delegate.Combine(obj.Progress, (AsyncActionProgressHandler<TProgress>)delegate(IAsyncActionWithProgress<TProgress> iap, TProgress p) {
                    a(iap, p);
                });
            }, progress, false);
        }
        public static IObservable<TResult> ToObservable<[System.Runtime.CompilerServices.Nullable(2)] TResult>(this IAsyncOperation<TResult> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return new AsyncInfoToObservableBridge<TResult, Unit>((IAsyncInfo)source, (Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>>)delegate(IAsyncInfo iai, Action<IAsyncInfo, AsyncStatus> a) {
                IAsyncOperation<TResult> obj = (IAsyncOperation<TResult>)iai;
                obj.Completed = (AsyncOperationCompletedHandler<TResult>)Delegate.Combine(obj.Completed, (AsyncOperationCompletedHandler<TResult>)delegate(IAsyncOperation<TResult> iao, AsyncStatus status) {
                    a(iao, status);
                });
            }, (Func<IAsyncInfo, TResult>)((IAsyncInfo iai) => ((IAsyncOperation<TResult>)iai).GetResults()), (Action<IAsyncInfo, Action<IAsyncInfo, Unit>>)null, (IProgress<Unit>)null, false);
        }
        public static IObservable<TResult> ToObservable<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return source.ToObservable_(null, false);
        }
        public static IObservable<TResult> ToObservable<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (progress == null)
                throw new ArgumentNullException("progress");
            return source.ToObservable_(progress, false);
        }
        public static IObservable<TProgress> ToObservableProgress<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return Observable.Create(delegate(IObserver<TProgress> observer) {
                IProgress<TProgress> progress = Observer.ToProgress<TProgress>(observer);
                return ObservableExtensions.Subscribe<TResult>(AsyncInfoObservableExtensions.ToObservable_<TResult, TProgress>(source, progress, false), (Action<TResult>)delegate {
                }, (Action<Exception>)observer.OnError, (Action)observer.OnCompleted);
            });
        }
        public static IObservable<TResult> ToObservableMultiple<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            return source.ToObservable_(null, true);
        }
        public static IObservable<TResult> ToObservableMultiple<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, IProgress<TProgress> progress)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (progress == null)
                throw new ArgumentNullException("progress");
            return source.ToObservable_(progress, true);
        }
        private static IObservable<TResult> ToObservable_<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TProgress>(this IAsyncOperationWithProgress<TResult, TProgress> source, [System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })] IProgress<TProgress> progress, bool supportsMultiple)
        {
            return new AsyncInfoToObservableBridge<TResult, TProgress>((IAsyncInfo)source, (Action<IAsyncInfo, Action<IAsyncInfo, AsyncStatus>>)delegate(IAsyncInfo iai, Action<IAsyncInfo, AsyncStatus> a) {
                IAsyncOperationWithProgress<TResult, TProgress> obj2 = (IAsyncOperationWithProgress<TResult, TProgress>)iai;
                obj2.Completed = (AsyncOperationWithProgressCompletedHandler<TResult, TProgress>)Delegate.Combine(obj2.Completed, (AsyncOperationWithProgressCompletedHandler<TResult, TProgress>)delegate(IAsyncOperationWithProgress<TResult, TProgress> iao, AsyncStatus status) {
                    a(iao, status);
                });
            }, (Func<IAsyncInfo, TResult>)((IAsyncInfo iai) => ((IAsyncOperationWithProgress<TResult, TProgress>)iai).GetResults()), (Action<IAsyncInfo, Action<IAsyncInfo, TProgress>>)delegate(IAsyncInfo iai, Action<IAsyncInfo, TProgress> a) {
                IAsyncOperationWithProgress<TResult, TProgress> obj = (IAsyncOperationWithProgress<TResult, TProgress>)iai;
                obj.Progress = (AsyncOperationProgressHandler<TResult, TProgress>)Delegate.Combine(obj.Progress, (AsyncOperationProgressHandler<TResult, TProgress>)delegate(IAsyncOperationWithProgress<TResult, TProgress> iap, TProgress p) {
                    a(iap, p);
                });
            }, progress, supportsMultiple);
        }
    }
}