55 lines
2.4 KiB
JavaScript
55 lines
2.4 KiB
JavaScript
import { connectableObservableDescriptor } from '../observable/ConnectableObservable';
|
|
/* tslint:enable:max-line-length */
|
|
/**
|
|
* Returns an Observable that emits the results of invoking a specified selector on items
|
|
* emitted by a ConnectableObservable that shares a single subscription to the underlying stream.
|
|
*
|
|
* <img src="./img/multicast.png" width="100%">
|
|
*
|
|
* @param {Function|Subject} subjectOrSubjectFactory - Factory function to create an intermediate subject through
|
|
* which the source sequence's elements will be multicast to the selector function
|
|
* or Subject to push source elements into.
|
|
* @param {Function} [selector] - Optional selector function that can use the multicasted source stream
|
|
* as many times as needed, without causing multiple subscriptions to the source stream.
|
|
* Subscribers to the given source will receive all notifications of the source from the
|
|
* time of the subscription forward.
|
|
* @return {Observable} An Observable that emits the results of invoking the selector
|
|
* on the items emitted by a `ConnectableObservable` that shares a single subscription to
|
|
* the underlying stream.
|
|
* @method multicast
|
|
* @owner Observable
|
|
*/
|
|
export function multicast(subjectOrSubjectFactory, selector) {
|
|
return function multicastOperatorFunction(source) {
|
|
let subjectFactory;
|
|
if (typeof subjectOrSubjectFactory === 'function') {
|
|
subjectFactory = subjectOrSubjectFactory;
|
|
}
|
|
else {
|
|
subjectFactory = function subjectFactory() {
|
|
return subjectOrSubjectFactory;
|
|
};
|
|
}
|
|
if (typeof selector === 'function') {
|
|
return source.lift(new MulticastOperator(subjectFactory, selector));
|
|
}
|
|
const connectable = Object.create(source, connectableObservableDescriptor);
|
|
connectable.source = source;
|
|
connectable.subjectFactory = subjectFactory;
|
|
return connectable;
|
|
};
|
|
}
|
|
export class MulticastOperator {
|
|
constructor(subjectFactory, selector) {
|
|
this.subjectFactory = subjectFactory;
|
|
this.selector = selector;
|
|
}
|
|
call(subscriber, source) {
|
|
const { selector } = this;
|
|
const subject = this.subjectFactory();
|
|
const subscription = selector(subject).subscribe(subscriber);
|
|
subscription.add(source.subscribe(subject));
|
|
return subscription;
|
|
}
|
|
}
|
|
//# sourceMappingURL=multicast.js.map
|