-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(share): add the share operator #440
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
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 hidden or 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,20 @@ | ||
| var RxOld = require("rx"); | ||
| var RxNew = require("../../../../index"); | ||
|
|
||
| module.exports = function (suite) { | ||
|
|
||
| var oldShareWithImmediateScheduler = RxOld.Observable.range(0, 25, RxOld.Scheduler.immediate).share(); | ||
| var newShareWithImmediateScheduler = RxNew.Observable.range(0, 25).share(); | ||
|
|
||
| return suite | ||
| .add('old share with immediate scheduler', function () { | ||
| oldShareWithImmediateScheduler.subscribe(_next, _error, _complete); | ||
| }) | ||
| .add('new share with immediate scheduler', function () { | ||
| newShareWithImmediateScheduler.subscribe(_next, _error, _complete); | ||
| }); | ||
|
|
||
| function _next(x) { } | ||
| function _error(e){ } | ||
| function _complete(){ } | ||
| }; |
This file contains hidden or 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 hidden or 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,64 @@ | ||
| /* globals describe, expect, it, hot, cold, expectObservable */ | ||
|
|
||
| var Rx = require('../../dist/cjs/Rx'); | ||
| var Observable = Rx.Observable; | ||
|
|
||
| describe('Observable.prototype.share()', function (){ | ||
| it('should share a single subscription', function (){ | ||
| var subscriptionCount = 0; | ||
| var obs = new Observable(function(observer){ | ||
| subscriptionCount++; | ||
| }); | ||
|
|
||
| var source = obs.share(); | ||
|
|
||
| expect(subscriptionCount).toBe(0); | ||
|
|
||
| source.subscribe(); | ||
| source.subscribe(); | ||
|
|
||
| expect(subscriptionCount).toBe(1); | ||
| }); | ||
|
|
||
| it('should not change the output of the observable when successful', function (){ | ||
| var e1 = hot('---a--^--b--c--d--e--|'); | ||
| var expected = '---b--c--d--e--|'; | ||
|
|
||
| expectObservable(e1.share()).toBe(expected); | ||
| }); | ||
|
|
||
| it('should not change the output of the observable when error', function (){ | ||
| var e1 = hot('---a--^--b--c--d--e--#'); | ||
| var expected = '---b--c--d--e--#'; | ||
|
|
||
| expectObservable(e1.share()).toBe(expected); | ||
| }); | ||
|
|
||
| it('should not change the output of the observable when successful with cold observable', function (){ | ||
| var e1 = cold('---a--b--c--d--e--|'); | ||
| var expected = '---a--b--c--d--e--|'; | ||
|
|
||
| expectObservable(e1.share()).toBe(expected); | ||
| }); | ||
|
|
||
| it('should not change the output of the observable when error with cold observable', function (){ | ||
| var e1 = cold('---a--b--c--d--e--#'); | ||
| var expected = '---a--b--c--d--e--#'; | ||
|
|
||
| expectObservable(e1.share()).toBe(expected); | ||
| }); | ||
|
|
||
| it('should not change the output of the observable when never', function (){ | ||
| var e1 = Observable.never(); | ||
| var expected = '-'; | ||
|
|
||
| expectObservable(e1.share()).toBe(expected); | ||
| }); | ||
|
|
||
| it('should not change the output of the observable when empty', function (){ | ||
| var e1 = Observable.empty(); | ||
| var expected = '|'; | ||
|
|
||
| expectObservable(e1.share()).toBe(expected); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| import Observable from '../Observable'; | ||
| import publish from './publish'; | ||
|
|
||
| export default function share<T>() : Observable<T> { | ||
| return publish.call(this).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.
Should we also have a marble test where
e1is a cold Observable to make sense applyingshareon it? Also the case of multiple subscribers to a was-cold shared Observable makes a lot of sense to have a subscribe-and-unsubscribe marble diagram test that we mentioned in #428.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.
Probably