-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
6d1742e
6daa0a6
a0182ce
684ec1e
2a157ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find any other sane way to call
.. but I think this should work. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The https://github.com/ReactiveX/rxjs/blob/master/src/operators/publish.ts#L6-L8 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed all default values here and left it up to |
||
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>; | ||
} |
There was a problem hiding this comment.
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
normulticast
(if I didn't miss anything) so I'm not sure I'm doing it right.There was a problem hiding this comment.
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 withobserver.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.
There was a problem hiding this comment.
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 themap()
operator does) because that's where the selector is called: https://github.com/ReactiveX/rxjs/blob/master/src/operators/multicast.ts#L64