|
import { isArrayLike } from '../util/isArrayLike'; |
|
import { isPromise } from '../util/isPromise'; |
|
import { Observable } from '../Observable'; |
|
import { ObservableInput, ObservedValueOf, ReadableStreamLike } from '../types'; |
|
import { isInteropObservable } from '../util/isInteropObservable'; |
|
import { isAsyncIterable } from '../util/isAsyncIterable'; |
|
import { createInvalidObservableTypeError } from '../util/throwUnobservableError'; |
|
import { isIterable } from '../util/isIterable'; |
|
import { isReadableStreamLike, readableStreamLikeToAsyncGenerator } from '../util/isReadableStreamLike'; |
|
import { Subscriber } from '../Subscriber'; |
|
import { isFunction } from '../util/isFunction'; |
|
import { reportUnhandledError } from '../util/reportUnhandledError'; |
|
import { observable as Symbol_observable } from '../symbol/observable'; |
|
|
|
export function innerFrom<O extends ObservableInput<any>>(input: O): Observable<ObservedValueOf<O>>; |
|
export function innerFrom<T>(input: ObservableInput<T>): Observable<T> { |
|
if (input instanceof Observable) { |
|
return input; |
|
} |
|
if (input != null) { |
|
if (isInteropObservable(input)) { |
|
return fromInteropObservable(input); |
|
} |
|
if (isArrayLike(input)) { |
|
return fromArrayLike(input); |
|
} |
|
if (isPromise(input)) { |
|
return fromPromise(input); |
|
} |
|
if (isAsyncIterable(input)) { |
|
return fromAsyncIterable(input); |
|
} |
|
if (isIterable(input)) { |
|
return fromIterable(input); |
|
} |
|
if (isReadableStreamLike(input)) { |
|
return fromReadableStreamLike(input); |
|
} |
|
} |
|
|
|
throw createInvalidObservableTypeError(input); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function fromInteropObservable<T>(obj: any) { |
|
return new Observable((subscriber: Subscriber<T>) => { |
|
const obs = obj[Symbol_observable](); |
|
if (isFunction(obs.subscribe)) { |
|
return obs.subscribe(subscriber); |
|
} |
|
|
|
throw new TypeError('Provided object does not correctly implement Symbol.observable'); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fromArrayLike<T>(array: ArrayLike<T>) { |
|
return new Observable((subscriber: Subscriber<T>) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < array.length && !subscriber.closed; i++) { |
|
subscriber.next(array[i]); |
|
} |
|
subscriber.complete(); |
|
}); |
|
} |
|
|
|
export function fromPromise<T>(promise: PromiseLike<T>) { |
|
return new Observable((subscriber: Subscriber<T>) => { |
|
promise |
|
.then( |
|
(value) => { |
|
if (!subscriber.closed) { |
|
subscriber.next(value); |
|
subscriber.complete(); |
|
} |
|
}, |
|
(err: any) => subscriber.error(err) |
|
) |
|
.then(null, reportUnhandledError); |
|
}); |
|
} |
|
|
|
export function fromIterable<T>(iterable: Iterable<T>) { |
|
return new Observable((subscriber: Subscriber<T>) => { |
|
for (const value of iterable) { |
|
subscriber.next(value); |
|
if (subscriber.closed) { |
|
return; |
|
} |
|
} |
|
subscriber.complete(); |
|
}); |
|
} |
|
|
|
export function fromAsyncIterable<T>(asyncIterable: AsyncIterable<T>) { |
|
return new Observable((subscriber: Subscriber<T>) => { |
|
process(asyncIterable, subscriber).catch((err) => subscriber.error(err)); |
|
}); |
|
} |
|
|
|
export function fromReadableStreamLike<T>(readableStream: ReadableStreamLike<T>) { |
|
return fromAsyncIterable(readableStreamLikeToAsyncGenerator(readableStream)); |
|
} |
|
|
|
async function process<T>(asyncIterable: AsyncIterable<T>, subscriber: Subscriber<T>) { |
|
for await (const value of asyncIterable) { |
|
subscriber.next(value); |
|
|
|
|
|
if (subscriber.closed) { |
|
return; |
|
} |
|
} |
|
subscriber.complete(); |
|
} |
|
|