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 1 commit
Commits
File filter

Filter by extension

Filter by extension

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

published.connect();
});

it('should mirror a simple source Observable with selector', () => {
const selector = observable => observable.map(v => String.fromCharCode(96 + parseInt(v)));
Copy link
Member

Choose a reason for hiding this comment

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

this one is a little too clever. :)

Can you use something more straightforward? A hash lookup or something?

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);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
});
Copy link
Member

Choose a reason for hiding this comment

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

Need to add tests for:

  • errors thrown in selector directly.
  • when selector returns an errored Observable (--a--b--#)
  • when selector returns empty Observable
  • when selector returns never
  • when selector returns Observable.throw

4 changes: 3 additions & 1 deletion src/operator/publishReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { publishReplay as higherOrder } from '../operators';
/**
* @param bufferSize
* @param windowTime
* @param selector
* @param scheduler
* @return {ConnectableObservable<T>}
* @method publishReplay
* @owner Observable
*/
export function publishReplay<T>(this: Observable<T>, bufferSize: number = Number.POSITIVE_INFINITY,
Copy link
Member

Choose a reason for hiding this comment

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

Like below, we'll need to add some function overloads for TypeScript users to keep this change from being breaking. Here's what the publish counterpart to this file is doing...

export function publish<T>(this: Observable<T>): ConnectableObservable<T>;
export function publish<T>(this: Observable<T>, selector: (source: Observable<T>) => Observable<T>): Observable<T>;
export function publish<T, R>(this: Observable<T>, selector: (source: Observable<T>) => Observable<R>): Observable<R>;

windowTime: number = Number.POSITIVE_INFINITY,
selector?: (source: Observable<T>) => Observable<T>,
scheduler?: IScheduler): ConnectableObservable<T> {
return higherOrder(bufferSize, windowTime, scheduler)(this);
return higherOrder(bufferSize, windowTime, selector, scheduler)(this);
}
4 changes: 3 additions & 1 deletion src/operators/publishReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { UnaryFunction } from '../interfaces';

export function publishReplay<T>(bufferSize: number = Number.POSITIVE_INFINITY,
Copy link
Member

Choose a reason for hiding this comment

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

I think we'll need some function signature overloads for this so we don't break compatability for people expecting ConnectableObservable<T> to be the only return type...

See what publish is doing here for a better idea:

export function publish<T>(): MonoTypeOperatorFunction<T>;
export function publish<T>(selector: MonoTypeOperatorFunction<T>): MonoTypeOperatorFunction<T>;
export function publish<T, R>(selector: OperatorFunction<T, R>): OperatorFunction<T, R>;

windowTime: number = Number.POSITIVE_INFINITY,
selector: (source: Observable<T>) => Observable<T>,
scheduler?: IScheduler): UnaryFunction<Observable<T>, ConnectableObservable<T>> {
return (source: Observable<T>) => multicast(new ReplaySubject<T>(bufferSize, windowTime, scheduler))(source) as ConnectableObservable<T>;
const subject = new ReplaySubject<T>(bufferSize, windowTime, scheduler);
return (source: Observable<T>) => multicast(() => subject, selector)(source) as ConnectableObservable<T>;
}