Skip to content

Commit 4ccf794

Browse files
committed
feat(publish): add higher-order lettable variant of publish
1 parent 1109697 commit 4ccf794

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/operator/publish.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Subject } from '../Subject';
1+
22
import { Observable } from '../Observable';
3-
import { multicast } from './multicast';
43
import { ConnectableObservable } from '../observable/ConnectableObservable';
4+
import { publish as higherOrder } from '../operators';
55

66
/* tslint:disable:max-line-length */
77
export function publish<T>(this: Observable<T>): ConnectableObservable<T>;
@@ -22,8 +22,7 @@ export function publish<T>(this: Observable<T>, selector: selector<T>): Observab
2222
* @owner Observable
2323
*/
2424
export function publish<T>(this: Observable<T>, selector?: (source: Observable<T>) => Observable<T>): Observable<T> | ConnectableObservable<T> {
25-
return selector ? multicast.call(this, () => new Subject<T>(), selector) :
26-
multicast.call(this, new Subject<T>());
25+
return higherOrder(selector)(this);
2726
}
2827

2928
export type selector<T> = (source: Observable<T>) => Observable<T>;

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { max } from './max';
66
export { mergeMap } from './mergeMap';
77
export { min } from './min';
88
export { multicast } from './multicast';
9+
export { publish } from './publish';
910
export { reduce } from './reduce';
1011
export { scan } from './scan';
1112
export { switchMap } from './switchMap';

src/operators/publish.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Subject } from '../Subject';
2+
import { multicast } from './multicast';
3+
import { MonoTypeOperatorFunction } from '../interfaces';
4+
5+
/* tslint:disable:max-line-length */
6+
export function publish<T>(): MonoTypeOperatorFunction<T>;
7+
export function publish<T>(selector: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
8+
/* tslint:enable:max-line-length */
9+
10+
/**
11+
* Returns a ConnectableObservable, which is a variety of Observable that waits until its connect method is called
12+
* before it begins emitting items to those Observers that have subscribed to it.
13+
*
14+
* <img src="./img/publish.png" width="100%">
15+
*
16+
* @param {Function} [selector] - Optional selector function which can use the multicasted source sequence as many times
17+
* as needed, without causing multiple subscriptions to the source sequence.
18+
* Subscribers to the given source will receive all notifications of the source from the time of the subscription on.
19+
* @return A ConnectableObservable that upon connection causes the source Observable to emit items to its Observers.
20+
* @method publish
21+
* @owner Observable
22+
*/
23+
export function publish<T>(selector?: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T> {
24+
return selector ?
25+
multicast(() => new Subject<T>(), selector) :
26+
multicast(new Subject<T>());
27+
}

0 commit comments

Comments
 (0)