From 4ccf7945eff50453f70c634dc2dffbf0ac7a018a Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Thu, 15 Jun 2017 17:37:58 -0700 Subject: [PATCH] feat(publish): add higher-order lettable variant of publish --- src/operator/publish.ts | 7 +++---- src/operators/index.ts | 1 + src/operators/publish.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/operators/publish.ts diff --git a/src/operator/publish.ts b/src/operator/publish.ts index a2e8dc1c95..960bc7cac4 100644 --- a/src/operator/publish.ts +++ b/src/operator/publish.ts @@ -1,7 +1,7 @@ -import { Subject } from '../Subject'; + import { Observable } from '../Observable'; -import { multicast } from './multicast'; import { ConnectableObservable } from '../observable/ConnectableObservable'; +import { publish as higherOrder } from '../operators'; /* tslint:disable:max-line-length */ export function publish(this: Observable): ConnectableObservable; @@ -22,8 +22,7 @@ export function publish(this: Observable, selector: selector): Observab * @owner Observable */ export function publish(this: Observable, selector?: (source: Observable) => Observable): Observable | ConnectableObservable { - return selector ? multicast.call(this, () => new Subject(), selector) : - multicast.call(this, new Subject()); + return higherOrder(selector)(this); } export type selector = (source: Observable) => Observable; diff --git a/src/operators/index.ts b/src/operators/index.ts index e2052f5f7b..a4838cc8d9 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -6,6 +6,7 @@ export { max } from './max'; export { mergeMap } from './mergeMap'; export { min } from './min'; export { multicast } from './multicast'; +export { publish } from './publish'; export { reduce } from './reduce'; export { scan } from './scan'; export { switchMap } from './switchMap'; diff --git a/src/operators/publish.ts b/src/operators/publish.ts new file mode 100644 index 0000000000..95330bab94 --- /dev/null +++ b/src/operators/publish.ts @@ -0,0 +1,27 @@ +import { Subject } from '../Subject'; +import { multicast } from './multicast'; +import { MonoTypeOperatorFunction } from '../interfaces'; + +/* tslint:disable:max-line-length */ +export function publish(): MonoTypeOperatorFunction; +export function publish(selector: MonoTypeOperatorFunction): MonoTypeOperatorFunction; +/* tslint:enable:max-line-length */ + +/** + * Returns a ConnectableObservable, which is a variety of Observable that waits until its connect method is called + * before it begins emitting items to those Observers that have subscribed to it. + * + * + * + * @param {Function} [selector] - Optional selector function which can use the multicasted source sequence as many times + * as needed, without causing multiple subscriptions to the source sequence. + * Subscribers to the given source will receive all notifications of the source from the time of the subscription on. + * @return A ConnectableObservable that upon connection causes the source Observable to emit items to its Observers. + * @method publish + * @owner Observable + */ +export function publish(selector?: MonoTypeOperatorFunction): MonoTypeOperatorFunction { + return selector ? + multicast(() => new Subject(), selector) : + multicast(new Subject()); +}