Skip to content

Commit

Permalink
feat(shareReplay): add higher-order lettable version of shareReplay
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonaden committed Sep 6, 2017
1 parent f10c42e commit e8be197
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/operator/shareReplay.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { Observable } from '../Observable';
import { multicast } from './multicast';
import { ReplaySubject } from '../ReplaySubject';
import { ConnectableObservable } from '../observable/ConnectableObservable';
import { IScheduler } from '../Scheduler';
import { shareReplay as higherOrder } from '../operators/shareReplay';

/**
* @method shareReplay
* @owner Observable
*/
export function shareReplay<T>(
this: Observable<T>,
bufferSize?: number,
windowTime?: number,
scheduler?: IScheduler
): Observable<T> {
let subject: ReplaySubject<T>;
const connectable = multicast.call(this, function shareReplaySubjectFactory(this: ConnectableObservable<T>) {
if (this._isComplete) {
return subject;
} else {
return (subject = new ReplaySubject<T>(bufferSize, windowTime, scheduler));
}
});
return connectable.refCount();
export function shareReplay<T>(this: Observable<T>, bufferSize?: number, windowTime?: number, scheduler?: IScheduler):
Observable<T> {
return higherOrder(bufferSize, windowTime, scheduler)(this);
};
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export { sampleTime } from './sampleTime';
export { scan } from './scan';
export { sequenceEqual } from './sequenceEqual';
export { share } from './share';
export { shareReplay } from './shareReplay';
export { subscribeOn } from './subscribeOn';
export { switchAll } from './switchAll';
export { switchMap } from './switchMap';
Expand Down
25 changes: 25 additions & 0 deletions src/operators/shareReplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Observable } from '../Observable';
import { multicast } from './multicast';
import { refCount } from './refCount';
import { ReplaySubject } from '../ReplaySubject';
import { ConnectableObservable } from '../observable/ConnectableObservable';
import { IScheduler } from '../Scheduler';

import { MonoTypeOperatorFunction } from '../interfaces';

/**
* @method shareReplay
* @owner Observable
*/
export function shareReplay<T>(bufferSize?: number, windowTime?: number, scheduler?: IScheduler ): MonoTypeOperatorFunction<T> {
let subject: ReplaySubject<T>;

const connectable = multicast(function shareReplaySubjectFactory(this: ConnectableObservable<T>) {
if (this._isComplete) {
return subject;
} else {
return (subject = new ReplaySubject<T>(bufferSize, windowTime, scheduler));
}
});
return (source: Observable<T>) => refCount()(connectable(source));
};

0 comments on commit e8be197

Please sign in to comment.