Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(publishReplay): add selector function to publishReplay #2885

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions spec/operators/publishReplay-spec.ts
Expand Up @@ -409,4 +409,75 @@ describe('Observable.prototype.publishReplay', () => {

published.connect();
});

it('should mirror a simple source Observable with selector', () => {
const values = {a: 2, b: 4, c: 6, d: 8};
const selector = observable => observable.map(v => 2 * v);
const source = cold('--1-2---3-4---|');
const sourceSubs = '^ !';
const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector);
const expected = '--a-b---c-d---|';

expectObservable(published).toBe(expected, values);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});

it('should emit an error when the selector throws an exception', () => {
const error = "It's broken";
const selector = () => {
throw error;
};
const source = cold('--1-2---3-4---|');
const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector);

// The exception is thrown outside Rx chain (not as an error notification).
expect(() => published.subscribe()).to.throw(error);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception is thrown outside the chain so I need to check it like this. This situations isn't tested in neither publish nor multicast (if I didn't miss anything) so I'm not sure I'm doing it right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH GEEZ! I missed this... So what should happen is you should catch the error and send it down the error path with observer.error(err). There are all sorts of examples of how to handle this around teh library.

Basically any user-supplied function needs to have it's errors caught and sent down the error channel of observation.

Copy link
Contributor Author

@martinsik martinsik Oct 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I would expect multicast to catch this error for me (for example like the map() operator does) because that's where the selector is called: https://github.com/ReactiveX/rxjs/blob/master/src/operators/multicast.ts#L64

});

it('should emit an error when the selector returns an Observable that emits an error', () => {
const error = "It's broken";
const innerObservable = cold('--5-6----#', undefined, error);
const selector = observable => observable.mergeMapTo(innerObservable);
const source = cold('--1--2---3---|');
const sourceSubs = '^ !';
const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector);
const expected = '----5-65-6-#';

expectObservable(published).toBe(expected, undefined, error);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});

it('should terminate immediately when the selector returns an empty Observable', () => {
const selector = () => Observable.empty();
const source = cold('--1--2---3---|');
const sourceSubs = '(^!)';
const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector);
const expected = '|';

expectObservable(published).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});

it('should not emit and should not complete/error when the selector returns never', () => {
const selector = () => Observable.never();
const source = cold('-');
const sourceSubs = '^';
const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector);
const expected = '-';

expectObservable(published).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});

it('should emit error when the selector returns Observable.throw', () => {
const error = "It's broken";
const selector = () => Observable.throw(error);
const source = cold('--1--2---3---|');
const sourceSubs = '(^!)';
const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector);
const expected = '#';

expectObservable(published).toBe(expected, undefined, error);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
20 changes: 15 additions & 5 deletions src/operator/publishReplay.ts
Expand Up @@ -2,17 +2,27 @@ import { Observable } from '../Observable';
import { IScheduler } from '../Scheduler';
import { ConnectableObservable } from '../observable/ConnectableObservable';
import { publishReplay as higherOrder } from '../operators/publishReplay';
import { OperatorFunction, MonoTypeOperatorFunction } from '../interfaces';

/* tslint:disable:max-line-length */
export function publishReplay<T>(this: Observable<T>, bufferSize?: number, windowTime?: number, scheduler?: IScheduler): ConnectableObservable<T>;
export function publishReplay<T>(this: Observable<T>, bufferSize?: number, windowTime?: number, selector?: MonoTypeOperatorFunction<T>, scheduler?: IScheduler): Observable<T>;
export function publishReplay<T, R>(this: Observable<T>, bufferSize?: number, windowTime?: number, selector?: OperatorFunction<T, R>): Observable<R>;
/* tslint:enable:max-line-length */

/**
* @param bufferSize
* @param windowTime
* @param selectorOrScheduler
* @param scheduler
* @return {ConnectableObservable<T>}
* @return {Observable<T> | ConnectableObservable<T>}
* @method publishReplay
* @owner Observable
*/
export function publishReplay<T>(this: Observable<T>, bufferSize: number = Number.POSITIVE_INFINITY,
windowTime: number = Number.POSITIVE_INFINITY,
scheduler?: IScheduler): ConnectableObservable<T> {
return higherOrder(bufferSize, windowTime, scheduler)(this) as ConnectableObservable<T>;
export function publishReplay<T, R>(this: Observable<T>, bufferSize?: number,
windowTime?: number,
selectorOrScheduler?: IScheduler | OperatorFunction<T, R>,
scheduler?: IScheduler): Observable<R> | ConnectableObservable<R> {

return higherOrder(bufferSize, windowTime, selectorOrScheduler as any, scheduler)(this);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any other sane way to call higherOrder. TypeScript didn't allow me just using:

higherOrder(bufferSize, windowTime, selectorOrScheduler, scheduler)

.. but I think this should work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do, the other thing you can do is duplicate the logic that figures out what was passed from the other method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, I didn't want to write the same logic twice so I'll leave it as it is.

}
25 changes: 20 additions & 5 deletions src/operators/publishReplay.ts
Expand Up @@ -3,10 +3,25 @@ import { ReplaySubject } from '../ReplaySubject';
import { IScheduler } from '../Scheduler';
import { multicast } from './multicast';
import { ConnectableObservable } from '../observable/ConnectableObservable';
import { UnaryFunction } from '../interfaces';
import { UnaryFunction, MonoTypeOperatorFunction, OperatorFunction } from '../interfaces';

export function publishReplay<T>(bufferSize: number = Number.POSITIVE_INFINITY,
windowTime: number = Number.POSITIVE_INFINITY,
scheduler?: IScheduler): UnaryFunction<Observable<T>, ConnectableObservable<T>> {
return (source: Observable<T>) => multicast(new ReplaySubject<T>(bufferSize, windowTime, scheduler))(source) as ConnectableObservable<T>;
/* tslint:disable:max-line-length */
export function publishReplay<T>(bufferSize?: number, windowTime?: number, scheduler?: IScheduler): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
Copy link
Contributor Author

@martinsik martinsik Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The publish operator defines MonoTypeOperatorFunction return type but I don't think that's correct because this results into Observable<T> while publish should return ConnectableObservable<T>.

https://github.com/ReactiveX/rxjs/blob/master/src/operators/publish.ts#L6-L8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now... we might need to address this more generally across the library.

export function publishReplay<T>(bufferSize?: number, windowTime?: number, selector?: MonoTypeOperatorFunction<T>, scheduler?: IScheduler): MonoTypeOperatorFunction<T>;
export function publishReplay<T, R>(bufferSize?: number, windowTime?: number, selector?: OperatorFunction<T, R>, scheduler?: IScheduler): OperatorFunction<T, R>;
/* tslint:enable:max-line-length */

export function publishReplay<T, R>(bufferSize?: number,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed all default values here and left it up to ReplaySubject to decide.

windowTime?: number,
selectorOrScheduler?: IScheduler | OperatorFunction<T, R>,
scheduler?: IScheduler): UnaryFunction<Observable<T>, ConnectableObservable<R> | Observable<R>> {

if (selectorOrScheduler && typeof selectorOrScheduler !== 'function') {
scheduler = selectorOrScheduler;
}

const selector = typeof selectorOrScheduler === 'function' ? selectorOrScheduler : undefined;
const subject = new ReplaySubject<T>(bufferSize, windowTime, scheduler);

return (source: Observable<T>) => multicast(() => subject, selector)(source) as Observable<R> | ConnectableObservable<R>;
}