-
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(shareReplay): adds shareReplay
variant of publishReplay
#2443
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import {expect} from 'chai'; | ||
import * as Rx from '../../dist/cjs/Rx'; | ||
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports | ||
|
||
declare const { asDiagram }; | ||
declare const hot: typeof marbleTestingSignature.hot; | ||
declare const cold: typeof marbleTestingSignature.cold; | ||
declare const expectObservable: typeof marbleTestingSignature.expectObservable; | ||
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions; | ||
|
||
const Observable = Rx.Observable; | ||
|
||
/** @test {shareReplay} */ | ||
describe('Observable.prototype.shareReplay', () => { | ||
it('should mirror a simple source Observable', () => { | ||
const source = cold('--1-2---3-4--5-|'); | ||
const sourceSubs = '^ !'; | ||
const published = source.shareReplay(); | ||
const expected = '--1-2---3-4--5-|'; | ||
|
||
expectObservable(published).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should do nothing if result is not subscribed', () => { | ||
let subscribed = false; | ||
const source = new Observable(() => { | ||
subscribed = true; | ||
}); | ||
source.shareReplay(); | ||
expect(subscribed).to.be.false; | ||
}); | ||
|
||
it('should multicast the same values to multiple observers, bufferSize=1', () => { | ||
const source = cold('-1-2-3----4-|'); const shared = source.shareReplay(1); | ||
const sourceSubs = '^ !'; | ||
const subscriber1 = hot('a| ').mergeMapTo(shared); | ||
const expected1 = '-1-2-3----4-|'; | ||
const subscriber2 = hot(' b| ').mergeMapTo(shared); | ||
const expected2 = ' 23----4-|'; | ||
const subscriber3 = hot(' c| ').mergeMapTo(shared); | ||
const expected3 = ' 3-4-|'; | ||
|
||
expectObservable(subscriber1).toBe(expected1); | ||
expectObservable(subscriber2).toBe(expected2); | ||
expectObservable(subscriber3).toBe(expected3); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should multicast the same values to multiple observers, bufferSize=2', () => { | ||
const source = cold('-1-2-----3------4-|'); const shared = source.shareReplay(2); | ||
const sourceSubs = '^ !'; | ||
const subscriber1 = hot('a| ').mergeMapTo(shared); | ||
const expected1 = '-1-2-----3------4-|'; | ||
const subscriber2 = hot(' b| ').mergeMapTo(shared); | ||
const expected2 = ' (12)-3------4-|'; | ||
const subscriber3 = hot(' c| ').mergeMapTo(shared); | ||
const expected3 = ' (23)-4-|'; | ||
|
||
expectObservable(subscriber1).toBe(expected1); | ||
expectObservable(subscriber2).toBe(expected2); | ||
expectObservable(subscriber3).toBe(expected3); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should multicast an error from the source to multiple observers', () => { | ||
const source = cold('-1-2-3----4-#'); const shared = source.shareReplay(1); | ||
const sourceSubs = '^ !'; | ||
const subscriber1 = hot('a| ').mergeMapTo(shared); | ||
const expected1 = '-1-2-3----4-#'; | ||
const subscriber2 = hot(' b| ').mergeMapTo(shared); | ||
const expected2 = ' 23----4-#'; | ||
const subscriber3 = hot(' c| ').mergeMapTo(shared); | ||
const expected3 = ' 3-4-#'; | ||
|
||
expectObservable(subscriber1).toBe(expected1); | ||
expectObservable(subscriber2).toBe(expected2); | ||
expectObservable(subscriber3).toBe(expected3); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should multicast an empty source', () => { | ||
const source = cold('|'); | ||
const sourceSubs = '(^!)'; | ||
const shared = source.shareReplay(1); | ||
const expected = '|'; | ||
|
||
expectObservable(shared).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should multicast a never source', () => { | ||
const source = cold('-'); | ||
const sourceSubs = '^'; | ||
|
||
const shared = source.shareReplay(1); | ||
const expected = '-'; | ||
|
||
expectObservable(shared).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should multicast a throw source', () => { | ||
const source = cold('#'); | ||
const sourceSubs = '(^!)'; | ||
const shared = source.shareReplay(1); | ||
const expected = '#'; | ||
|
||
expectObservable(shared).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should replay results to subsequent subscriptions if source completes, bufferSize=2', () => { | ||
const source = cold('-1-2-----3-| '); | ||
const shared = source.shareReplay(2); | ||
const sourceSubs = '^ ! '; | ||
const subscriber1 = hot('a| ').mergeMapTo(shared); | ||
const expected1 = '-1-2-----3-| '; | ||
const subscriber2 = hot(' b| ').mergeMapTo(shared); | ||
const expected2 = ' (12)-3-| '; | ||
const subscriber3 = hot(' (c|) ').mergeMapTo(shared); | ||
const expected3 = ' (23|)'; | ||
|
||
expectObservable(subscriber1).toBe(expected1); | ||
expectObservable(subscriber2).toBe(expected2); | ||
expectObservable(subscriber3).toBe(expected3); | ||
expectSubscriptions(source.subscriptions).toBe(sourceSubs); | ||
}); | ||
|
||
it('should completely restart for subsequent subscriptions if source errors, bufferSize=2', () => { | ||
const source = cold('-1-2-----3-# '); | ||
const shared = source.shareReplay(2); | ||
const sourceSubs1 = '^ ! '; | ||
const subscriber1 = hot('a| ').mergeMapTo(shared); | ||
const expected1 = '-1-2-----3-# '; | ||
const subscriber2 = hot(' b| ').mergeMapTo(shared); | ||
const expected2 = ' (12)-3-# '; | ||
const subscriber3 = hot(' (c|) ').mergeMapTo(shared); | ||
const expected3 = ' -1-2-----3-#'; | ||
const sourceSubs2 = ' ^ !'; | ||
|
||
expectObservable(subscriber1).toBe(expected1); | ||
expectObservable(subscriber2).toBe(expected2); | ||
expectObservable(subscriber3).toBe(expected3); | ||
expectSubscriptions(source.subscriptions).toBe([sourceSubs1, sourceSubs2]); | ||
}); | ||
|
||
it('should be retryable, bufferSize=2', () => { | ||
const subs = []; | ||
const source = cold('-1-2-----3-# '); | ||
const shared = source.shareReplay(2).retry(1); | ||
subs.push( '^ ! '); | ||
subs.push( ' ^ ! '); | ||
subs.push( ' ^ !'); | ||
const subscriber1 = hot('a| ').mergeMapTo(shared); | ||
const expected1 = '-1-2-----3--1-2-----3-# '; | ||
const subscriber2 = hot(' b| ').mergeMapTo(shared); | ||
const expected2 = ' (12)-3--1-2-----3-# '; | ||
const subscriber3 = hot(' (c|) ').mergeMapTo(shared); | ||
const expected3 = ' (12)-3--1-2-----3-#'; | ||
|
||
expectObservable(subscriber1).toBe(expected1); | ||
expectObservable(subscriber2).toBe(expected2); | ||
expectObservable(subscriber3).toBe(expected3); | ||
expectSubscriptions(source.subscriptions).toBe(subs); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
import { Observable } from '../../Observable'; | ||
import { shareReplay } from '../../operator/shareReplay'; | ||
|
||
Observable.prototype.shareReplay = shareReplay; | ||
|
||
declare module '../../Observable' { | ||
interface Observable<T> { | ||
shareReplay: typeof shareReplay; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Observable } from '../Observable'; | ||
import { multicast } from './multicast'; | ||
import { ReplaySubject } from '../ReplaySubject'; | ||
import { ConnectableObservable } from '../observable/ConnectableObservable'; | ||
import { IScheduler } from '../Scheduler'; | ||
|
||
/** | ||
* @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(); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't
expected3
be(23)#
?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.
Ignore me. It's correct. (because the source errored, it didn't complete, so we create a new ReplaySubject for the next multicast)