21c2b1b2614065290d055927b523f42f41e87839577eb719f11db0bc4a4727f4e8056a6eefce6c014c2f7d10c84cc4667d2f397ca77dec7e521ff90414855f 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Observable } from '../Observable';
  2. import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
  3. import { scheduled } from '../scheduled/scheduled';
  4. import { innerFrom } from './innerFrom';
  5. export function from<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>;
  6. /** @deprecated The `scheduler` parameter will be removed in v8. Use `scheduled`. Details: https://rxjs.dev/deprecations/scheduler-argument */
  7. export function from<O extends ObservableInput<any>>(input: O, scheduler: SchedulerLike | undefined): Observable<ObservedValueOf<O>>;
  8. /**
  9. * Creates an Observable from an Array, an array-like object, a Promise, an iterable object, or an Observable-like object.
  10. *
  11. * <span class="informal">Converts almost anything to an Observable.</span>
  12. *
  13. * ![](from.png)
  14. *
  15. * `from` converts various other objects and data types into Observables. It also converts a Promise, an array-like, or an
  16. * <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable" target="_blank">iterable</a>
  17. * object into an Observable that emits the items in that promise, array, or iterable. A String, in this context, is treated
  18. * as an array of characters. Observable-like objects (contains a function named with the ES2015 Symbol for Observable) can also be
  19. * converted through this operator.
  20. *
  21. * ## Examples
  22. *
  23. * Converts an array to an Observable
  24. *
  25. * ```ts
  26. * import { from } from 'rxjs';
  27. *
  28. * const array = [10, 20, 30];
  29. * const result = from(array);
  30. *
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * // Logs:
  34. * // 10
  35. * // 20
  36. * // 30
  37. * ```
  38. *
  39. * Convert an infinite iterable (from a generator) to an Observable
  40. *
  41. * ```ts
  42. * import { from, take } from 'rxjs';
  43. *
  44. * function* generateDoubles(seed) {
  45. * let i = seed;
  46. * while (true) {
  47. * yield i;
  48. * i = 2 * i; // double it
  49. * }
  50. * }
  51. *
  52. * const iterator = generateDoubles(3);
  53. * const result = from(iterator).pipe(take(10));
  54. *
  55. * result.subscribe(x => console.log(x));
  56. *
  57. * // Logs:
  58. * // 3
  59. * // 6
  60. * // 12
  61. * // 24
  62. * // 48
  63. * // 96
  64. * // 192
  65. * // 384
  66. * // 768
  67. * // 1536
  68. * ```
  69. *
  70. * With `asyncScheduler`
  71. *
  72. * ```ts
  73. * import { from, asyncScheduler } from 'rxjs';
  74. *
  75. * console.log('start');
  76. *
  77. * const array = [10, 20, 30];
  78. * const result = from(array, asyncScheduler);
  79. *
  80. * result.subscribe(x => console.log(x));
  81. *
  82. * console.log('end');
  83. *
  84. * // Logs:
  85. * // 'start'
  86. * // 'end'
  87. * // 10
  88. * // 20
  89. * // 30
  90. * ```
  91. *
  92. * @see {@link fromEvent}
  93. * @see {@link fromEventPattern}
  94. *
  95. * @param input A subscription object, a Promise, an Observable-like,
  96. * an Array, an iterable, or an array-like object to be converted.
  97. * @param scheduler An optional {@link SchedulerLike} on which to schedule the emission of values.
  98. * @return An Observable converted from {@link ObservableInput}.
  99. */
  100. export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T> {
  101. return scheduler ? scheduled(input, scheduler) : innerFrom(input);
  102. }