|
| 1 | +import { Subject } from '../Subject'; |
| 2 | +import { Operator } from '../Operator'; |
| 3 | +import { Subscriber } from '../Subscriber'; |
| 4 | +import { Observable } from '../Observable'; |
| 5 | +import { ConnectableObservable, connectableObservableDescriptor } from '../observable/ConnectableObservable'; |
| 6 | +import { FactoryOrValue, MonoTypeOperatorFunction } from '../interfaces'; |
| 7 | + |
| 8 | +/* tslint:disable:max-line-length */ |
| 9 | +export function multicast<T>(subjectOrSubjectFactory: FactoryOrValue<Subject<T>>): MonoTypeOperatorFunction<T>; |
| 10 | +export function multicast<T>(SubjectFactory: (this: Observable<T>) => Subject<T>, selector?: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>; |
| 11 | +/* tslint:enable:max-line-length */ |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns an Observable that emits the results of invoking a specified selector on items |
| 15 | + * emitted by a ConnectableObservable that shares a single subscription to the underlying stream. |
| 16 | + * |
| 17 | + * <img src="./img/multicast.png" width="100%"> |
| 18 | + * |
| 19 | + * @param {Function|Subject} subjectOrSubjectFactory - Factory function to create an intermediate subject through |
| 20 | + * which the source sequence's elements will be multicast to the selector function |
| 21 | + * or Subject to push source elements into. |
| 22 | + * @param {Function} [selector] - Optional selector function that can use the multicasted source stream |
| 23 | + * as many times as needed, without causing multiple subscriptions to the source stream. |
| 24 | + * Subscribers to the given source will receive all notifications of the source from the |
| 25 | + * time of the subscription forward. |
| 26 | + * @return {Observable} An Observable that emits the results of invoking the selector |
| 27 | + * on the items emitted by a `ConnectableObservable` that shares a single subscription to |
| 28 | + * the underlying stream. |
| 29 | + * @method multicast |
| 30 | + * @owner Observable |
| 31 | + */ |
| 32 | +export function multicast<T>(subjectOrSubjectFactory: Subject<T> | (() => Subject<T>), |
| 33 | + selector?: (source: Observable<T>) => Observable<T>): MonoTypeOperatorFunction<T> { |
| 34 | + return function multicastOperatorFunction(source: Observable<T>): Observable<T> { |
| 35 | + let subjectFactory: () => Subject<T>; |
| 36 | + if (typeof subjectOrSubjectFactory === 'function') { |
| 37 | + subjectFactory = <() => Subject<T>>subjectOrSubjectFactory; |
| 38 | + } else { |
| 39 | + subjectFactory = function subjectFactory() { |
| 40 | + return <Subject<T>>subjectOrSubjectFactory; |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + if (typeof selector === 'function') { |
| 45 | + return source.lift(new MulticastOperator(subjectFactory, selector)); |
| 46 | + } |
| 47 | + |
| 48 | + const connectable: any = Object.create(source, connectableObservableDescriptor); |
| 49 | + connectable.source = source; |
| 50 | + connectable.subjectFactory = subjectFactory; |
| 51 | + |
| 52 | + return <ConnectableObservable<T>> connectable; |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +export class MulticastOperator<T> implements Operator<T, T> { |
| 57 | + constructor(private subjectFactory: () => Subject<T>, |
| 58 | + private selector: (source: Observable<T>) => Observable<T>) { |
| 59 | + } |
| 60 | + call(subscriber: Subscriber<T>, source: any): any { |
| 61 | + const { selector } = this; |
| 62 | + const subject = this.subjectFactory(); |
| 63 | + const subscription = selector(subject).subscribe(subscriber); |
| 64 | + subscription.add(source.subscribe(subject)); |
| 65 | + return subscription; |
| 66 | + } |
| 67 | +} |
0 commit comments