1 | import { ObservableInput } from '../types';
|
---|
2 | import { subscribeToArray } from './subscribeToArray';
|
---|
3 | import { subscribeToPromise } from './subscribeToPromise';
|
---|
4 | import { subscribeToIterable } from './subscribeToIterable';
|
---|
5 | import { subscribeToObservable } from './subscribeToObservable';
|
---|
6 | import { isArrayLike } from './isArrayLike';
|
---|
7 | import { isPromise } from './isPromise';
|
---|
8 | import { isObject } from './isObject';
|
---|
9 | import { iterator as Symbol_iterator } from '../symbol/iterator';
|
---|
10 | import { observable as Symbol_observable } from '../symbol/observable';
|
---|
11 | import { Subscription } from '../Subscription';
|
---|
12 | import { Subscriber } from '../Subscriber';
|
---|
13 |
|
---|
14 | export const subscribeTo = <T>(result: ObservableInput<T>): (subscriber: Subscriber<T>) => Subscription | void => {
|
---|
15 | if (!!result && typeof result[Symbol_observable] === 'function') {
|
---|
16 | return subscribeToObservable(result as any);
|
---|
17 | } else if (isArrayLike(result)) {
|
---|
18 | return subscribeToArray(result);
|
---|
19 | } else if (isPromise(result)) {
|
---|
20 | return subscribeToPromise(result as Promise<any>);
|
---|
21 | } else if (!!result && typeof result[Symbol_iterator] === 'function') {
|
---|
22 | return subscribeToIterable(result as any);
|
---|
23 | } else {
|
---|
24 | const value = isObject(result) ? 'an invalid object' : `'${result}'`;
|
---|
25 | const msg = `You provided ${value} where a stream was expected.`
|
---|
26 | + ' You can provide an Observable, Promise, Array, or Iterable.';
|
---|
27 | throw new TypeError(msg);
|
---|
28 | }
|
---|
29 | };
|
---|