Observable.Conversions.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using UniRx.Operators;
  4. namespace UniRx
  5. {
  6. public static partial class Observable
  7. {
  8. public static IObservable<T> AsObservable<T>(this IObservable<T> source)
  9. {
  10. if (source == null) throw new ArgumentNullException("source");
  11. // optimize, don't double wrap
  12. if (source is UniRx.Operators.AsObservableObservable<T>)
  13. {
  14. return source;
  15. }
  16. return new AsObservableObservable<T>(source);
  17. }
  18. public static IObservable<T> ToObservable<T>(this IEnumerable<T> source)
  19. {
  20. return ToObservable(source, Scheduler.DefaultSchedulers.Iteration);
  21. }
  22. public static IObservable<T> ToObservable<T>(this IEnumerable<T> source, IScheduler scheduler)
  23. {
  24. return new ToObservableObservable<T>(source, scheduler);
  25. }
  26. public static IObservable<TResult> Cast<TSource, TResult>(this IObservable<TSource> source)
  27. {
  28. return new CastObservable<TSource, TResult>(source);
  29. }
  30. /// <summary>
  31. /// witness is for type inference.
  32. /// </summary>
  33. public static IObservable<TResult> Cast<TSource, TResult>(this IObservable<TSource> source, TResult witness)
  34. {
  35. return new CastObservable<TSource, TResult>(source);
  36. }
  37. public static IObservable<TResult> OfType<TSource, TResult>(this IObservable<TSource> source)
  38. {
  39. return new OfTypeObservable<TSource, TResult>(source);
  40. }
  41. /// <summary>
  42. /// witness is for type inference.
  43. /// </summary>
  44. public static IObservable<TResult> OfType<TSource, TResult>(this IObservable<TSource> source, TResult witness)
  45. {
  46. return new OfTypeObservable<TSource, TResult>(source);
  47. }
  48. /// <summary>
  49. /// Converting .Select(_ => Unit.Default) sequence.
  50. /// </summary>
  51. public static IObservable<Unit> AsUnitObservable<T>(this IObservable<T> source)
  52. {
  53. return new AsUnitObservableObservable<T>(source);
  54. }
  55. /// <summary>
  56. /// Same as LastOrDefault().AsUnitObservable().
  57. /// </summary>
  58. public static IObservable<Unit> AsSingleUnitObservable<T>(this IObservable<T> source)
  59. {
  60. return new AsSingleUnitObservableObservable<T>(source);
  61. }
  62. }
  63. }