File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,23 @@ describe('Observable.prototype.publish', () => {
77
77
published . connect ( ) ;
78
78
} ) ;
79
79
80
+ it ( 'should accept selectors' , ( ) => {
81
+ const source = hot ( '-1-2-3----4-|' ) ;
82
+ const sourceSubs = [ '^ !' ] ;
83
+ const published = source . publish ( x => x . zip ( x , ( a , b ) => ( parseInt ( a ) + parseInt ( b ) ) . toString ( ) ) ) ;
84
+ const subscriber1 = hot ( 'a| ' ) . mergeMapTo ( published ) ;
85
+ const expected1 = '-2-4-6----8-|' ;
86
+ const subscriber2 = hot ( ' b| ' ) . mergeMapTo ( published ) ;
87
+ const expected2 = ' -6----8-|' ;
88
+ const subscriber3 = hot ( ' c| ' ) . mergeMapTo ( published ) ;
89
+ const expected3 = ' --8-|' ;
90
+
91
+ expectObservable ( subscriber1 ) . toBe ( expected1 ) ;
92
+ expectObservable ( subscriber2 ) . toBe ( expected2 ) ;
93
+ expectObservable ( subscriber3 ) . toBe ( expected3 ) ;
94
+ expectSubscriptions ( source . subscriptions ) . toBe ( sourceSubs ) ;
95
+ } ) ;
96
+
80
97
it ( 'should multicast an error from the source to multiple observers' , ( ) => {
81
98
const source = cold ( '-1-2-3----4-#' ) ;
82
99
const sourceSubs = '^ !' ;
Original file line number Diff line number Diff line change 2
2
import { Observable } from '../../Observable' ;
3
3
import { publish , PublishSignature } from '../../operator/publish' ;
4
4
5
- Observable . prototype . publish = publish ;
5
+ Observable . prototype . publish = < any > publish ;
6
6
7
7
declare module '../../Observable' {
8
8
interface Observable < T > {
Original file line number Diff line number Diff line change 1
1
import { Subject } from '../Subject' ;
2
+ import { Observable } from '../Observable' ;
2
3
import { multicast } from './multicast' ;
3
4
import { ConnectableObservable } from '../observable/ConnectableObservable' ;
4
5
@@ -8,14 +9,21 @@ import {ConnectableObservable} from '../observable/ConnectableObservable';
8
9
*
9
10
* <img src="./img/publish.png" width="100%">
10
11
*
12
+ * @param {Function } Optional selector function which can use the multicasted source sequence as many times as needed,
13
+ * without causing multiple subscriptions to the source sequence.
14
+ * Subscribers to the given source will receive all notifications of the source from the time of the subscription on.
11
15
* @return a ConnectableObservable that upon connection causes the source Observable to emit items to its Observers.
12
16
* @method publish
13
17
* @owner Observable
14
18
*/
15
- export function publish < T > ( ) : ConnectableObservable < T > {
16
- return multicast . call ( this , new Subject < T > ( ) ) ;
19
+ export function publish < T > ( selector ?: ( source : Observable < T > ) => Observable < T > ) : Observable < T > | ConnectableObservable < T > {
20
+ return selector ? multicast . call ( this , ( ) => new Subject < T > ( ) , selector ) :
21
+ multicast . call ( this , new Subject < T > ( ) ) ;
17
22
}
18
23
24
+ export type selector < T > = ( source : Observable < T > ) => Observable < T > ;
25
+
19
26
export interface PublishSignature < T > {
20
27
( ) : ConnectableObservable < T > ;
28
+ ( selector : selector < T > ) : Observable < T > ;
21
29
}
You can’t perform that action at this time.
0 commit comments