Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

from.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Observable } from '../Observable';
  2. import { subscribeTo } from '../util/subscribeTo';
  3. import { ObservableInput, SchedulerLike, ObservedValueOf } from '../types';
  4. import { scheduled } from '../scheduled/scheduled';
  5. export function from<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>;
  6. /** @deprecated use {@link scheduled} instead. */
  7. export function from<O extends ObservableInput<any>>(input: O, scheduler: SchedulerLike): 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. * ---
  40. *
  41. * ### Convert an infinite iterable (from a generator) to an Observable
  42. *
  43. * ```ts
  44. * import { from } from 'rxjs';
  45. * import { take } from 'rxjs/operators';
  46. *
  47. * function* generateDoubles(seed) {
  48. * let i = seed;
  49. * while (true) {
  50. * yield i;
  51. * i = 2 * i; // double it
  52. * }
  53. * }
  54. *
  55. * const iterator = generateDoubles(3);
  56. * const result = from(iterator).pipe(take(10));
  57. *
  58. * result.subscribe(x => console.log(x));
  59. *
  60. * // Logs:
  61. * // 3
  62. * // 6
  63. * // 12
  64. * // 24
  65. * // 48
  66. * // 96
  67. * // 192
  68. * // 384
  69. * // 768
  70. * // 1536
  71. * ```
  72. *
  73. * ---
  74. *
  75. * ### With async scheduler
  76. *
  77. * ```ts
  78. * import { from, asyncScheduler } from 'rxjs';
  79. *
  80. * console.log('start');
  81. *
  82. * const array = [10, 20, 30];
  83. * const result = from(array, asyncScheduler);
  84. *
  85. * result.subscribe(x => console.log(x));
  86. *
  87. * console.log('end');
  88. *
  89. * // Logs:
  90. * // start
  91. * // end
  92. * // 10
  93. * // 20
  94. * // 30
  95. * ```
  96. *
  97. * @see {@link fromEvent}
  98. * @see {@link fromEventPattern}
  99. *
  100. * @param {ObservableInput<T>} A subscription object, a Promise, an Observable-like,
  101. * an Array, an iterable, or an array-like object to be converted.
  102. * @param {SchedulerLike} An optional {@link SchedulerLike} on which to schedule the emission of values.
  103. * @return {Observable<T>}
  104. * @name from
  105. * @owner Observable
  106. */
  107. export function from<T>(input: ObservableInput<T>, scheduler?: SchedulerLike): Observable<T> {
  108. if (!scheduler) {
  109. if (input instanceof Observable) {
  110. return input;
  111. }
  112. return new Observable<T>(subscribeTo(input));
  113. } else {
  114. return scheduled(input, scheduler);
  115. }
  116. }