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