8000 fix(typings): publish variants will properly return ConnectableObserv… · ReactiveX/rxjs@d563bfa · GitHub
[go: up one dir, main page]

Skip to content

Commit d563bfa

Browse files
cartantbenlesh
authored andcommitted
fix(typings): publish variants will properly return ConnectableObservable(#2983)
* chore(typings): improve publish typings Use ConnectableObservable in signatures that do not have selectors. * chore(typings): add publish type tests
1 parent 0753ff7 commit d563bfa

File tree

8 files changed

+131
-9
lines changed

8 files changed

+131
-9
lines changed

spec/operators/multicast-spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ describe('Observable.prototype.multicast', () => {
644644
type('should infer the type', () => {
645645
/* tslint:disable:no-unused-variable */
646646
const source = Rx.Observable.of<number>(1, 2, 3);
647-
const result: Rx.Observable<number> = source.multicast(() => new Subject<number>());
647+
const result: Rx.ConnectableObservable<number> = source.multicast(() => new Subject<number>());
648648
/* tslint:enable:no-unused-variable */
649649
});
650650

@@ -661,5 +661,27 @@ describe('Observable.prototype.multicast', () => {
661661
const result: Rx.Observable<string> = source.multicast(() => new Subject<number>(), s => s.map(x => x + '!'));
662662
/* tslint:enable:no-unused-variable */
663663
});
664+
665+
type('should infer the type for the pipeable operator', () => {
666+
/* tslint:disable:no-unused-variable */
667+
const source = Rx.Observable.of<number>(1, 2, 3);
668+
// TODO: https://github.com/ReactiveX/rxjs/issues/2972
669+
const result: Rx.ConnectableObservable<number> = Rx.operators.multicast(() => new Subject<number>())(source);
670+
/* tslint:enable:no-unused-variable */
671+
});
672+
673+
type('should infer the type for the pipeable operator with a selector', () => {
674+
/* tslint:disable:no-unused-variable */
675+
const source = Rx.Observable.of<number>(1, 2, 3);
676+
const result: Rx.Observable<number> = source.pipe(Rx.operators.multicast(() => new Subject<number>(), s => s.map(x => x)));
677+
/* tslint:enable:no-unused-variable */
678+
});
679+
680+
type('should infer the type for the pipeable operator with a type-changing selector', () => {
681+
/* tslint:disable:no-unused-variable */
682+
const source = Rx.Observable.of<number>(1, 2, 3);
683+
const result: Rx.Observable<string> = source.pipe(Rx.operators.multicast(() => new Subject<number>(), s => s.map(x => x + '!')));
684+
/* tslint:enable:no-unused-variable */
685+
});
664686
});
665687
});

spec/operators/publish-spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ describe('Observable.prototype.publish', () => {
337337
type('should infer the type', () => {
338338
/* tslint:disable:no-unused-variable */
339339
const source = Rx.Observable.of<number>(1, 2, 3);
340-
const result: Rx.Observable<number> = source.publish();
340+
const result: Rx.ConnectableObservable<number> = source.publish();
341341
/* tslint:enable:no-unused-variable */
342342
});
343343

@@ -354,4 +354,26 @@ describe('Observable.prototype.publish', () => {
354354
const result: Rx.Observable<string> = source.publish(s => s.map(x => x + '!'));
355355
/* tslint:enable:no-unused-variable */
356356
});
357+
358+
type('should infer the type for the pipeable operator', () => {
359+
/* tslint:disable:no-unused-variable */
360+
const source = Rx.Observable.of<number>(1, 2, 3);
361+
// TODO: https://github.com/ReactiveX/rxjs/issues/2972
362+
const result: Rx.ConnectableObservable<number> = Rx.operators.publish()(source);
363+
/* tslint:enable:no-unused-variable */
364+
});
365+
366+
type('should infer the type for the pipeable operator with a selector', () => {
367+
/* tslint:disable:no-unused-variable */
368+
const source = Rx.Observable.of<number>(1, 2, 3);
369+
const result: Rx.Observable<number> = source.pipe(Rx.operators.publish(s => s.map(x => x)));
370+
/* tslint:enable:no-unused-variable */
371+
});
372+
373+
type('should infer the type for the pipeable operator with a type-changing selector', () => {
374+
/* tslint:disable:no-unused-variable */
375+
const source = Rx.Observable.of<number>(1, 2, 3);
376+
const result: Rx.Observable<string> = source.pipe(Rx.operators.publish(s => s.map(x => x + '!')));
377+
/* tslint:enable:no-unused-variable */
378+
});
357379
});

spec/operators/publishBehavior-spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Rx from '../../dist/package/Rx';
33
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
44

55
declare const { asDiagram };
6+
declare const type;
67
declare const hot: typeof marbleTestingSignature.hot;
78
declare const cold: typeof marbleTestingSignature.cold;
89
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
@@ -336,4 +337,19 @@ describe('Observable.prototype.publishBehavior', () => {
336337
expect(results).to.deep.equal([]);
337338
done();
338339
});
340+
341+
type('should infer the type', () => {
342+
/* tslint:disable:no-unused-variable */
343+
const source = Rx.Observable.of<number>(1, 2, 3);
344+
const result: Rx.ConnectableObservable<number> = source.publishBehavior(0);
345+
/* tslint:enable:no-unused-variable */
346+
});
347+
348+
type('should infer the type for the pipeable operator', () => {
349+
/* tslint:disable:no-unused-variable */
350+
const source = Rx.Observable.of<number>(1, 2, 3);
351+
// TODO: https://github.com/ReactiveX/rxjs/issues/2972
352+
const result: Rx.ConnectableObservable<number> = Rx.operators.publishBehavior(0)(source);
353+
/* tslint:enable:no-unused-variable */
354+
});
339355
});

spec/operators/publishLast-spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Rx from '../../dist/package/Rx';
33
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
44

55
declare const { asDiagram };
6+
declare const type;
67
declare const hot: typeof marbleTestingSignature.hot;
78
declare const cold: typeof marbleTestingSignature.cold;
89
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
@@ -257,4 +258,19 @@ describe('Observable.prototype.publishLast', () => {
257258
expect(subscriptions).to.equal(1);
258259
done();
259260
});
261+
262+
type('should infer the type', () => {
263+
/* tslint:disable:no-unused-variable */
264+
const source = Rx.Observable.of<number>(1, 2, 3);
265+
const result: Rx.ConnectableObservable<number> = source.publishLast();
266+
/* tslint:enable:no-unused-variable */
267+
});
268+
269+
type('should infer the type for the pipeable operator', () => {
270+
/* tslint:disable:no-unused-variable */
271+
const source = Rx.Observable.of<number>(1, 2, 3);
272+
// TODO: https://github.com/ReactiveX/rxjs/issues/2972
273+
const result: Rx.ConnectableObservable<number> = Rx.operators.publishLast()(source);
274+
/* tslint:enable:no-unused-variable */
275+
});
260276
});

spec/operators/publishReplay-spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Rx from '../../dist/package/Rx';
33
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
44

55
declare const { asDiagram };
6+
declare const type;
67
declare const hot: typeof marbleTestingSignature.hot;
78
declare const cold: typeof marbleTestingSignature.cold;
89
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
@@ -480,4 +481,47 @@ describe('Observable.prototype.publishReplay', () => {
480481
expectObservable(published).toBe(expected, undefined, error);
481482
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
482483
});
484+
485+
type('should infer the type', () => {
486+
/* tslint:disable:no-unused-variable */
487+
const source = Rx.Observable.of<number>(1, 2, 3);
488+
const result: Rx.ConnectableObservable<number> = source.publishReplay(1);
489+
/* tslint:enable:no-unused-variable */
490+
});
491+
492+
type('should infer the type with a selector', () => {
493+
/* tslint:disable:no-unused-variable */
494+
const source = Rx.Observable.of<number>(1, 2, 3);
495+
const result: Rx.Observable<number> = source.publishReplay(1, undefined, s => s.map(x => x));
496+
/* tslint:enable:no-unused-variable 10000 */
497+
});
498+
499+
type('should infer the type with a type-changing selector', () => {
500+
/* tslint:disable:no-unused-variable */
501+
const source = Rx.Observable.of<number>(1, 2, 3);
502+
const result: Rx.Observable<string> = source.publishReplay(1, undefined, s => s.map(x => x + '!'));
503+
/* tslint:enable:no-unused-variable */
504+
});
505+
506+
type('should infer the type for the pipeable operator', () => {
507+
/* tslint:disable:no-unused-variable */
508+
const source = Rx.Observable.of<number>(1, 2, 3);
509+
// TODO: https://github.com/ReactiveX/rxjs/issues/2972
510+
const result: Rx.ConnectableObservable<number> = Rx.operators.publishReplay(1)(source);
511+
/* tslint:enable:no-unused-variable */
512+
});
513+
514+
type('should infer the type for the pipeable operator with a selector', () => {
515+
/* tslint:disable:no-unused-variable */
516+
const source = Rx.Observable.of<number>(1, 2, 3);
517+
const result: Rx.Observable<number> = source.pipe(Rx.operators.publishReplay(1, undefined, s => s.map(x => x)));
518+
/* tslint:enable:no-unused-variable */
519+
});
520+
521+
type('should infer the type for the pipeable operator with a type-changing selector', () => {
522+
/* tslint:disable:no-unused-variable */
523+
const source = Rx.Observable.of<number>(1, 2, 3);
524+
const result: Rx.Observable<string> = source.pipe(Rx.operators.publishReplay(1, undefined, s => s.map(x => x + '!')));
525+
/* tslint:enable:no-unused-variable */
526+
});
483527
});

src/operators/multicast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { Operator } from '../Operator';
33
import { Subscriber } from '../Subscriber';
44
import { Observable } from '../Observable';
55
import { ConnectableObservable, connectableObservableDescriptor } from '../observable/ConnectableObservable';
6-
import { FactoryOrValue, MonoTypeOperatorFunction, OperatorFunction } from '../interfaces';
6+
import { FactoryOrValue, MonoTypeOperatorFunction, OperatorFunction, UnaryFunction } from '../interfaces';
77

88
/* tslint:disable:max-line-length */
9-
export function multicast<T>(subjectOrSubjectFactory: FactoryOrValue<Subject<T>>): MonoTypeOperatorFunction<T>;
9+
export function multicast<T>(subjectOrSubjectFactory: FactoryOrValue<Subject<T>>): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
1010
export function multicast<T>(SubjectFactory: (this: Observable<T>) => Subject<T>, selector?: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
1111
export function multicast<T, R>(SubjectFactory: (this: Observable<T>) => Subject<T>, selector?: OperatorFunction<T, R>): OperatorFunction<T, R>;
1212
/* tslint:enable:max-line-length */

src/operators/publish.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { Observable } from '../Observable';
12
import { Subject } from '../Subject';
23
import { multicast } from './multicast';
3-
import { MonoTypeOperatorFunction, OperatorFunction } from '../interfaces';
4+
import { ConnectableObservable } from '../observable/ConnectableObservable';
5+
import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction } from '../interfaces';
46

57
/* tslint:disable:max-line-length */
6-
export function publish<T>(): MonoTypeOperatorFunction<T>;
8+
export function publish<T>(): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
79
export function publish<T>(selector: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
810
export function publish<T, R>(selector: OperatorFunction<T, R>): OperatorFunction<T, R>;
911
/* tslint:enable:max-line-length */

src/operators/publishLast.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Observable } from '../Observable';
22
import { AsyncSubject } from '../AsyncSubject';
33
import { multicast } from './multicast';
4-
import { OperatorFunction } from '../interfaces';
4+
import { ConnectableObservable } from '../observable/ConnectableObservable';
5+
import { UnaryFunction } from '../interfaces';
56

6-
//TODO(benlesh): specify that the second type is actually a ConnectableObservable
7-
export function publishLast<T>(): OperatorFunction<T, T> {
7+
export function publishLast<T>(): UnaryFunction<Observable<T>, ConnectableObservable<T>> {
88
return (source: Observable<T>) => multicast(new AsyncSubject<T>())(source);
99
}

0 commit comments

Comments
 (0)
0