Debian Codegraph @

node/rxjs/src/internal/observable/innerFrom.ts

raw
1 import { isArrayLike } from '../util/isArrayLike';
2 import { isPromise } from '../util/isPromise';
3 import { Observable } from '../Observable';
4 import { ObservableInput, ObservedValueOf, ReadableStreamLike } from '../types';
5 import { isInteropObservable } from '../util/isInteropObservable';
6 import { isAsyncIterable } from '../util/isAsyncIterable';
7 import { createInvalidObservableTypeError } from '../util/throwUnobservableError';
8 import { isIterable } from '../util/isIterable';
9 import { isReadableStreamLike, readableStreamLikeToAsyncGenerator } from '../util/isReadableStreamLike';
10 import { Subscriber } from '../Subscriber';
11 import { isFunction } from '../util/isFunction';
12 import { reportUnhandledError } from '../util/reportUnhandledError';
13 import { observable as Symbol_observable } from '../symbol/observable';
14
15 export function innerFrom<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>;
16 export function innerFrom<T>(input: ObservableInput<T>): Observable<T> {
17 if (input instanceof Observable) {
18 return input;
19 }
20 if (input != null) {
21 if (isInteropObservable(input)) {
22 return fromInteropObservable(input);
23 }
24 if (isArrayLike(input)) {
25 return fromArrayLike(input);
26 }
27 if (isPromise(input)) {
28 return fromPromise(input);
29 }
30 if (isAsyncIterable(input)) {
31 return fromAsyncIterable(input);
32 }
33 if (isIterable(input)) {
34 return fromIterable(input);
35 }
36 if (isReadableStreamLike(input)) {
37 return fromReadableStreamLike(input);
38 }
39 }
40
41 throw createInvalidObservableTypeError(input);
42 }
43
44 /**
45 * Creates an RxJS Observable from an object that implements `Symbol.observable`.
46 * @param obj An object that properly implements `Symbol.observable`.
47 */
48 export function fromInteropObservable<T>(obj: any) {
49 return new Observable((subscriber: Subscriber<T>) => {
50 const obs = obj[Symbol_observable]();
51 if (isFunction(obs.subscribe)) {
52 return obs.subscribe(subscriber);
53 }
54 // Should be caught by observable subscribe function error handling.
55 throw new TypeError('Provided object does not correctly implement Symbol.observable');
56 });
57 }
58
59 /**
60 * Synchronously emits the values of an array like and completes.
61 * This is exported because there are creation functions and operators that need to
62 * make direct use of the same logic, and there's no reason to make them run through
63 * `from` conditionals because we *know* they're dealing with an array.
64 * @param array The array to emit values from
65 */
66 export function fromArrayLike<T>(array: ArrayLike<T>) {
67 return new Observable((subscriber: Subscriber<T>) => {
68 // Loop over the array and emit each value. Note two things here:
69 // 1. We're making sure that the subscriber is not closed on each loop.
70 // This is so we don't continue looping over a very large array after
71 // something like a `take`, `takeWhile`, or other synchronous unsubscription
72 // has already unsubscribed.
73 // 2. In this form, reentrant code can alter that array we're looping over.
74 // This is a known issue, but considered an edge case. The alternative would
75 // be to copy the array before executing the loop, but this has
76 // performance implications.
77 for (let i = 0; i < array.length && !subscriber.closed; i++) {
78 subscriber.next(array[i]);
79 }
80 subscriber.complete();
81 });
82 }
83
84 export function fromPromise<T>(promise: PromiseLike<T>) {
85 return new Observable((subscriber: Subscriber<T>) => {
86 promise
87 .then(
88 (value) => {
89 if (!subscriber.closed) {
90 subscriber.next(value);
91 subscriber.complete();
92 }
93 },
94 (err: any) => subscriber.error(err)
95 )
96 .then(null, reportUnhandledError);
97 });
98 }
99
100 export function fromIterable<T>(iterable: Iterable<T>) {
101 return new Observable((subscriber: Subscriber<T>) => {
102 for (const value of iterable) {
103 subscriber.next(value);
104 if (subscriber.closed) {
105 return;
106 }
107 }
108 subscriber.complete();
109 });
110 }
111
112 export function fromAsyncIterable<T>(asyncIterable: AsyncIterable<T>) {
113 return new Observable((subscriber: Subscriber<T>) => {
114 process(asyncIterable, subscriber).catch((err) => subscriber.error(err));
115 });
116 }
117
118 export function fromReadableStreamLike<T>(readableStream: ReadableStreamLike<T>) {
119 return fromAsyncIterable(readableStreamLikeToAsyncGenerator(readableStream));
120 }
121
122 async function process<T>(asyncIterable: AsyncIterable<T>, subscriber: Subscriber<T>) {
123 for await (const value of asyncIterable) {
124 subscriber.next(value);
125 // A side-effect may have closed our subscriber,
126 // check before the next iteration.
127 if (subscriber.closed) {
128 return;
129 }
130 }
131 subscriber.complete();
132 }