AsyncObservable.cs 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. public static class AsyncObservable
  8. {
  9. public static IAsyncObservable<T> Create<T>(Func<IAsyncObserver<T>, Task<IAsyncDisposable>> subscribeAsync)
  10. {
  11. if (subscribeAsync == null)
  12. throw new ArgumentNullException(nameof(subscribeAsync));
  13. return new AnonymousAsyncObservable<T>(subscribeAsync);
  14. }
  15. private sealed class AnonymousAsyncObservable<T> : AsyncObservableBase<T>
  16. {
  17. private readonly Func<IAsyncObserver<T>, Task<IAsyncDisposable>> _subscribeAsync;
  18. public AnonymousAsyncObservable(Func<IAsyncObserver<T>, Task<IAsyncDisposable>> subscribeAsync)
  19. {
  20. _subscribeAsync = subscribeAsync;
  21. }
  22. protected override Task<IAsyncDisposable> SubscribeAsyncCore(IAsyncObserver<T> observer) => _subscribeAsync(observer);
  23. }
  24. }
  25. }