diff --git a/perf/micro/immediate-scheduler/operators/share.js b/perf/micro/immediate-scheduler/operators/share.js new file mode 100644 index 0000000000..728937a8fd --- /dev/null +++ b/perf/micro/immediate-scheduler/operators/share.js @@ -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(){ } +}; \ No newline at end of file diff --git a/perf/micro/index.js b/perf/micro/index.js index ce1a5654d1..4ab4e4a1ea 100644 --- a/perf/micro/index.js +++ b/perf/micro/index.js @@ -29,7 +29,7 @@ Observable.create(function(observer) { }) .filter(function(filePath) { var argv = process.argv; - if(argv && argv.length > 2) { + if(argv && argv.length > 2) { return argv.slice(2).some(function(val) { return path.parse(filePath).name === val; }); diff --git a/spec/operators/share-spec.js b/spec/operators/share-spec.js new file mode 100644 index 0000000000..f98b70b02a --- /dev/null +++ b/spec/operators/share-spec.js @@ -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); + }); +}); \ No newline at end of file diff --git a/src/CoreOperators.ts b/src/CoreOperators.ts index f920f89823..ef6845a833 100644 --- a/src/CoreOperators.ts +++ b/src/CoreOperators.ts @@ -54,6 +54,7 @@ export interface CoreOperators { sample?: (notifier: Observable) => Observable; sampleTime?: (delay: number, scheduler?: Scheduler) => Observable; scan?: (project: (acc: R, x: T) => R, acc?: R) => Observable; + share?: () => Observable; single?: (predicate?: (value: T, index:number) => boolean, thisArg?: any) => Observable; skip?: (count: number) => Observable; skipUntil?: (notifier: Observable) => Observable; diff --git a/src/Rx.KitchenSink.ts b/src/Rx.KitchenSink.ts index 5e50eb51e5..c5173b7f15 100644 --- a/src/Rx.KitchenSink.ts +++ b/src/Rx.KitchenSink.ts @@ -223,6 +223,9 @@ observableProto.sampleTime = sampleTime; import scan from './operators/scan'; observableProto.scan = scan; +import share from './operators/share'; +observableProto.share = share; + import single from './operators/single'; observableProto.single = single; diff --git a/src/Rx.ts b/src/Rx.ts index a959519777..e8939574ab 100644 --- a/src/Rx.ts +++ b/src/Rx.ts @@ -203,6 +203,9 @@ observableProto.sampleTime = sampleTime; import scan from './operators/scan'; observableProto.scan = scan; +import share from './operators/share'; +observableProto.share = share; + import single from './operators/single'; observableProto.single = single; diff --git a/src/operators/share.ts b/src/operators/share.ts new file mode 100644 index 0000000000..667bb99dcc --- /dev/null +++ b/src/operators/share.ts @@ -0,0 +1,6 @@ +import Observable from '../Observable'; +import publish from './publish'; + +export default function share() : Observable { + return publish.call(this).refCount(); +}; \ No newline at end of file