From cae623c892b59be87ca475cbda2eff0b50f2929d Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 6 Mar 2018 14:17:58 -0800 Subject: [PATCH] feat(never): no longer export `never` function BREAKING CHANGE: no longer exported. Use the `NEVER` constant instead. --- spec/exports-spec.ts | 4 - spec/index-spec.ts | 2 +- spec/observables/fromEvent-spec.ts | 4 +- spec/observables/fromEventPattern-spec.ts | 4 +- spec/observables/interval-spec.ts | 4 +- spec/observables/never-spec.ts | 12 +-- spec/observables/timer-spec.ts | 4 +- spec/operators/buffer-spec.ts | 82 ++++++++--------- spec/operators/catch-spec.ts | 2 +- spec/operators/debounce-spec.ts | 3 +- spec/operators/mergeScan-spec.ts | 88 +++++++++--------- spec/operators/publishReplay-spec.ts | 44 ++++----- spec/operators/race-spec.ts | 7 +- spec/operators/refCount-spec.ts | 65 +++++++------ spec/operators/share-spec.ts | 9 +- spec/operators/switch-spec.ts | 107 +++++++++++----------- spec/operators/windowToggle-spec.ts | 95 +++++++++---------- spec/schedulers/TestScheduler-spec.ts | 33 +++---- src/add/observable/never.ts | 6 +- src/index.ts | 2 +- src/internal/observable/never.ts | 33 ++----- 21 files changed, 307 insertions(+), 303 deletions(-) diff --git a/spec/exports-spec.ts b/spec/exports-spec.ts index 67338a101f..8c7b0fd77e 100644 --- a/spec/exports-spec.ts +++ b/spec/exports-spec.ts @@ -82,10 +82,6 @@ describe('exports', () => { expect(merge).to.equal(Rx.Observable.merge); }); - it('should have rxjs/observable/never', () => { - expect(never).to.equal(Rx.Observable.never); - }); - it('should have rxjs/observable/of', () => { expect(of).to.equal(Rx.Observable.of); }); diff --git a/spec/index-spec.ts b/spec/index-spec.ts index da21298443..14d19c9d26 100644 --- a/spec/index-spec.ts +++ b/spec/index-spec.ts @@ -42,6 +42,7 @@ describe('index', () => { it('should export constants', () => { expect(index.EMPTY).to.exist; + expect(index.NEVER).to.exist; }); it('should export static observable creator functions', () => { @@ -60,7 +61,6 @@ describe('index', () => { expect(index.iif).to.exist; expect(index.interval).to.exist; expect(index.merge).to.exist; - expect(index.never).to.exist; expect(index.of).to.exist; expect(index.onErrorResumeNext).to.exist; expect(index.pairs).to.exist; diff --git a/spec/observables/fromEvent-spec.ts b/spec/observables/fromEvent-spec.ts index 893f66f6cd..5e3e5a84ee 100644 --- a/spec/observables/fromEvent-spec.ts +++ b/spec/observables/fromEvent-spec.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; import { expectObservable } from '../helpers/marble-testing'; -import { fromEvent, never, timer } from '../../src'; +import { fromEvent, NEVER, timer } from '../../src'; import { TestScheduler } from '../../src/testing'; declare function asDiagram(arg: string): Function; @@ -15,7 +15,7 @@ describe('fromEvent', () => { timer(50, 20, rxTestScheduler) .mapTo('ev') .take(2) - .concat(never()) + .concat(NEVER) .subscribe(listener); }, removeEventListener: () => void 0, diff --git a/spec/observables/fromEventPattern-spec.ts b/spec/observables/fromEventPattern-spec.ts index 4d5bb5adfb..cda75388c5 100644 --- a/spec/observables/fromEventPattern-spec.ts +++ b/spec/observables/fromEventPattern-spec.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; import { expectObservable } from '../helpers/marble-testing'; -import { fromEventPattern, noop, never, timer } from '../../src'; +import { fromEventPattern, noop, NEVER, timer } from '../../src'; import { TestScheduler } from '../../src/testing'; declare function asDiagram(arg: string): Function; @@ -16,7 +16,7 @@ describe('fromEventPattern', () => { timer(50, 20, rxTestScheduler) .mapTo('ev') .take(2) - .concat(never()) + .concat(NEVER) .subscribe(h); } const e1 = fromEventPattern(addHandler); diff --git a/spec/observables/interval-spec.ts b/spec/observables/interval-spec.ts index d44ecaab8a..4d7e8067a4 100644 --- a/spec/observables/interval-spec.ts +++ b/spec/observables/interval-spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; import { expectObservable } from '../helpers/marble-testing'; -import { asapScheduler, Observable, animationFrameScheduler, queueScheduler } from '../../src'; +import { NEVER, asapScheduler, Observable, animationFrameScheduler, queueScheduler } from '../../src'; import { TestScheduler } from '../../src/testing'; import { interval } from '../../src/'; @@ -13,7 +13,7 @@ describe('interval', () => { asDiagram('interval(1000)')('should create an observable emitting periodically', () => { const e1 = interval(20, rxTestScheduler) .take(6) // make it actually finite, so it can be rendered - .concat(Observable.never()); // but pretend it's infinite by not completing + .concat(NEVER); // but pretend it's infinite by not completing const expected = '--a-b-c-d-e-f-'; const values = { a: 0, diff --git a/spec/observables/never-spec.ts b/spec/observables/never-spec.ts index bfeda333a3..4196f0af3b 100644 --- a/spec/observables/never-spec.ts +++ b/spec/observables/never-spec.ts @@ -1,18 +1,18 @@ -import { never } from '../../src/'; +import { NEVER } from '../../src/'; import { expect } from 'chai'; import { expectObservable } from '../helpers/marble-testing'; declare const asDiagram: any; -/** @test {never} */ -describe('never', () => { - asDiagram('never')('should create a cold observable that never emits', () => { +/** @test {NEVER} */ +describe('NEVER', () => { + asDiagram('NEVER')('should create a cold observable that never emits', () => { const expected = '-'; - const e1 = never(); + const e1 = NEVER; expectObservable(e1).toBe(expected); }); it('should return the same instance every time', () => { - expect(never()).to.equal(never()); + expect(NEVER).to.equal(NEVER); }); }); diff --git a/spec/observables/timer-spec.ts b/spec/observables/timer-spec.ts index 9bec93268f..10a6bb1fb7 100644 --- a/spec/observables/timer-spec.ts +++ b/spec/observables/timer-spec.ts @@ -1,5 +1,5 @@ import { cold, expectObservable, time } from '../helpers/marble-testing'; -import { timer, never, merge } from '../../src/'; +import { timer, NEVER, merge } from '../../src/'; import { TestScheduler } from '../../src/testing'; import { mergeMap } from '../../src/operators'; @@ -11,7 +11,7 @@ describe('timer', () => { asDiagram('timer(3000, 1000)')('should create an observable emitting periodically', () => { const e1 = timer(60, 20, rxTestScheduler) .take(4) // make it actually finite, so it can be rendered - .concat(never()); // but pretend it's infinite by not completing + .concat(NEVER); // but pretend it's infinite by not completing const expected = '------a-b-c-d-'; const values = { a: 0, diff --git a/spec/operators/buffer-spec.ts b/spec/operators/buffer-spec.ts index 9559f058b6..693e425c2b 100644 --- a/spec/operators/buffer-spec.ts +++ b/spec/operators/buffer-spec.ts @@ -1,10 +1,9 @@ -import * as Rx from '../../src/Rx'; import { hot, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { buffer, mergeMap, take } from '../../src/operators'; +import { EMPTY, NEVER, throwError, of } from '../../src'; declare function asDiagram(arg: string): Function; -const Observable = Rx.Observable; - /** @test {buffer} */ describe('Observable.prototype.buffer', () => { asDiagram('buffer')('should emit buffers that close and reopen', () => { @@ -16,77 +15,77 @@ describe('Observable.prototype.buffer', () => { y: ['d', 'e', 'f'], z: ['g', 'h', 'i'] }; - expectObservable(a.buffer(b)).toBe(expected, expectedValues); + expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues); }); it('should work with empty and empty selector', () => { - const a = Observable.empty(); - const b = Observable.empty(); + const a = EMPTY; + const b = EMPTY; const expected = '|'; - expectObservable(a.buffer(b)).toBe(expected); + expectObservable(a.pipe(buffer(b))).toBe(expected); }); it('should work with empty and non-empty selector', () => { - const a = Observable.empty(); + const a = EMPTY; const b = hot('-----a-----'); const expected = '|'; - expectObservable(a.buffer(b)).toBe(expected); + expectObservable(a.pipe(buffer(b))).toBe(expected); }); it('should work with non-empty and empty selector', () => { const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|'); - const b = Observable.empty(); + const b = EMPTY; const expected = '|'; - expectObservable(a.buffer(b)).toBe(expected); + expectObservable(a.pipe(buffer(b))).toBe(expected); }); it('should work with never and never selector', () => { - const a = Observable.never(); - const b = Observable.never(); + const a = NEVER; + const b = NEVER; const expected = '-'; - expectObservable(a.buffer(b)).toBe(expected); + expectObservable(a.pipe(buffer(b))).toBe(expected); }); it('should work with never and empty selector', () => { - const a = Observable.never(); - const b = Observable.empty(); + const a = NEVER; + const b = EMPTY; const expected = '|'; - expectObservable(a.buffer(b)).toBe(expected); + expectObservable(a.pipe(buffer(b))).toBe(expected); }); it('should work with empty and never selector', () => { - const a = Observable.empty(); - const b = Observable.never(); + const a = EMPTY; + const b = NEVER; const expected = '|'; - expectObservable(a.buffer(b)).toBe(expected); + expectObservable(a.pipe(buffer(b))).toBe(expected); }); it('should work with non-empty and throw selector', () => { const a = hot('---^--a--'); - const b = Observable.throw(new Error('too bad')); + const b = throwError(new Error('too bad')); const expected = '#'; - expectObservable(a.buffer(b)).toBe(expected, null, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad')); }); it('should work with throw and non-empty selector', () => { - const a = Observable.throw(new Error('too bad')); + const a = throwError(new Error('too bad')); const b = hot('---^--a--'); const expected = '#'; - expectObservable(a.buffer(b)).toBe(expected, null, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad')); }); it('should work with error', () => { const a = hot('---^-------#', null, new Error('too bad')); const b = hot('---^--------'); const expected = '--------#'; - expectObservable(a.buffer(b)).toBe(expected, null, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad')); }); it('should work with error and non-empty selector', () => { const a = hot('---^-------#', null, new Error('too bad')); const b = hot('---^---a----'); const expected = '----a---#'; - expectObservable(a.buffer(b)).toBe(expected, { a: [] }, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, { a: [] }, new Error('too bad')); }); it('should work with selector', () => { @@ -98,11 +97,11 @@ describe('Observable.prototype.buffer', () => { a: ['3'], b: ['4', '5'], c: ['6'], - d: [], + d: [] as string[], e: ['7', '8', '9'], f: ['0'] }; - expectObservable(a.buffer(b)).toBe(expected, expectedValues); + expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues); }); it('should work with selector completed', () => { @@ -115,9 +114,9 @@ describe('Observable.prototype.buffer', () => { a: ['3'], b: ['4', '5'], c: ['6'], - d: [] + d: [] as string[] }; - expectObservable(a.buffer(b)).toBe(expected, expectedValues); + expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues); expectSubscriptions(a.subscriptions).toBe(subs); }); @@ -131,7 +130,7 @@ describe('Observable.prototype.buffer', () => { a: ['3'], b: ['4', '5'] }; - expectObservable(a.buffer(b), unsub).toBe(expected, expectedValues); + expectObservable(a.pipe(buffer(b)), unsub).toBe(expected, expectedValues); expectSubscriptions(a.subscriptions).toBe(subs); }); @@ -146,10 +145,11 @@ describe('Observable.prototype.buffer', () => { b: ['4', '5'] }; - const result = a - .mergeMap((x: any) => Observable.of(x)) - .buffer(b) - .mergeMap((x: any) => Observable.of(x)); + const result = a.pipe( + mergeMap((x: any) => of(x)), + buffer(b), + mergeMap((x: any) => of(x)), + ); expectObservable(result, unsub).toBe(expected, expectedValues); expectSubscriptions(a.subscriptions).toBe(subs); @@ -163,9 +163,9 @@ describe('Observable.prototype.buffer', () => { const expected = '---a--b--#'; const expectedValues = { a: [3], - b: [] + b: [] as string[] }; - expectObservable(a.buffer(b)).toBe(expected, expectedValues, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues, new Error('too bad')); expectSubscriptions(a.subscriptions).toBe(subs); }); @@ -173,7 +173,7 @@ describe('Observable.prototype.buffer', () => { const a = hot('--1--2--^--3--4--5---6----7--8--9---0---|'); const b = hot('--------^----------------#', null, new Error('too bad')); const expected = '-----------------#'; - expectObservable(a.buffer(b)).toBe(expected, null, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad')); }); it('should work with non-empty and selector error', () => { @@ -188,7 +188,7 @@ describe('Observable.prototype.buffer', () => { b: ['4', '5'], c: ['6'] }; - expectObservable(a.buffer(b)).toBe(expected, expectedValues, new Error('too bad')); + expectObservable(a.pipe(buffer(b))).toBe(expected, expectedValues, new Error('too bad')); expectSubscriptions(a.subscriptions).toBe(subs); }); @@ -204,7 +204,7 @@ describe('Observable.prototype.buffer', () => { b: ['4', '5'] }; - expectObservable(a.buffer(b), unsub).toBe(expected, expectedValues); + expectObservable(a.pipe(buffer(b)), unsub).toBe(expected, expectedValues); expectSubscriptions(a.subscriptions).toBe(subs); expectSubscriptions(b.subscriptions).toBe(bsubs); }); @@ -218,7 +218,7 @@ describe('Observable.prototype.buffer', () => { x: ['a', 'b', 'c'], }; - expectObservable(a.buffer(b).take(1)).toBe(expected, expectedValues); + expectObservable(a.pipe(buffer(b), take(1))).toBe(expected, expectedValues); expectSubscriptions(b.subscriptions).toBe(bsubs); }); }); diff --git a/spec/operators/catch-spec.ts b/spec/operators/catch-spec.ts index 9ca111a869..01dbb6d1e1 100644 --- a/spec/operators/catch-spec.ts +++ b/spec/operators/catch-spec.ts @@ -233,7 +233,7 @@ describe('Observable.prototype.catch', () => { expectSubscriptions(e2.subscriptions).toBe(e2subs); }); - it('should never terminate if you return Observable.never()', () => { + it('should never terminate if you return NEVER', () => { const e1 = hot('--a--b--#'); const e1subs = '^ !'; const e2 = cold( '-'); diff --git a/spec/operators/debounce-spec.ts b/spec/operators/debounce-spec.ts index 3c782481a8..40b12823d5 100644 --- a/spec/operators/debounce-spec.ts +++ b/spec/operators/debounce-spec.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { NEVER } from '../../src/internal/observable/never'; declare const type; declare function asDiagram(arg: string): Function; @@ -316,7 +317,7 @@ describe('Observable.prototype.debounce', () => { const e1subs = '^ !'; const expected = '------------------------------------(z|)'; - function selectorFunction(x) { return Observable.never(); } + function selectorFunction() { return NEVER; } expectObservable(e1.debounce(selectorFunction)).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); diff --git a/spec/operators/mergeScan-spec.ts b/spec/operators/mergeScan-spec.ts index b51e016b1f..db974ada9b 100644 --- a/spec/operators/mergeScan-spec.ts +++ b/spec/operators/mergeScan-spec.ts @@ -1,12 +1,12 @@ -import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; import { throwError } from '../../src/internal/observable/throwError'; +import { TestScheduler } from '../../src/testing'; +import { of, EMPTY, NEVER, concat } from '../../src'; +import { mergeScan, delay, mergeMap } from '../../src/operators'; -declare const rxTestScheduler: Rx.TestScheduler; -const Observable = Rx.Observable; - +declare const rxTestScheduler: TestScheduler; /** @test {mergeScan} */ -describe('Observable.prototype.mergeScan', () => { +describe('mergeScan', () => { it('should mergeScan things', () => { const e1 = hot('--a--^--b--c--d--e--f--g--|'); const e1subs = '^ !'; @@ -21,7 +21,7 @@ describe('Observable.prototype.mergeScan', () => { z: ['b', 'c', 'd', 'e', 'f', 'g'] }; - const source = e1.mergeScan((acc, x) => Observable.of(acc.concat(x)), []); + const source = e1.pipe(mergeScan((acc, x) => of(acc.concat(x)), [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -38,7 +38,7 @@ describe('Observable.prototype.mergeScan', () => { w: ['b', 'c', 'd'] }; - const source = e1.mergeScan((acc, x) => Observable.of(acc.concat(x)), []); + const source = e1.pipe(mergeScan((acc, x) => of(acc.concat(x)), [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -58,8 +58,8 @@ describe('Observable.prototype.mergeScan', () => { z: ['b', 'c', 'd', 'e', 'f', 'g'] }; - const source = e1.mergeScan((acc, x) => - Observable.of(acc.concat(x)).delay(20, rxTestScheduler), []); + const source = e1.pipe(mergeScan((acc, x) => + of(acc.concat(x)).delay(20, rxTestScheduler), [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -79,8 +79,8 @@ describe('Observable.prototype.mergeScan', () => { z: ['c', 'e', 'g'], }; - const source = e1.mergeScan((acc, x) => - Observable.of(acc.concat(x)).delay(50, rxTestScheduler), []); + const source = e1.pipe(mergeScan((acc, x) => + of(acc.concat(x)).delay(50, rxTestScheduler), [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -100,8 +100,8 @@ describe('Observable.prototype.mergeScan', () => { z: ['c', 'e', 'g'], }; - const source = e1.mergeScan((acc, x) => - Observable.of(acc.concat(x)).delay(50, rxTestScheduler), []); + const source = e1.pipe(mergeScan((acc, x) => + of(acc.concat(x)).delay(50, rxTestScheduler), [])); expectObservable(source, e1subs).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -123,10 +123,15 @@ describe('Observable.prototype.mergeScan', () => { }; const source = e1 - .mergeMap((x) => Observable.of(x)) - .mergeScan((acc, x) => - Observable.of(acc.concat(x)).delay(50, rxTestScheduler), []) - .mergeMap(function (x) { return Observable.of(x); }); + .pipe( + mergeMap((x) => of(x)), + mergeScan((acc, x) => + of([...acc, x]).pipe( + delay(50, rxTestScheduler) + ) + , []), + mergeMap(function (x) { return of(x); }) + ); expectObservable(source, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -142,14 +147,14 @@ describe('Observable.prototype.mergeScan', () => { v: ['b', 'c'] }; - const source = e1.mergeScan((acc, x) => { + const source = e1.pipe(mergeScan((acc, x) => { if (x === 'd') { - throw 'bad!'; + throw new Error('bad!'); } - return Observable.of(acc.concat(x)); - }, []); + return of(acc.concat(x)); + }, [])); - expectObservable(source).toBe(expected, values, 'bad!'); + expectObservable(source).toBe(expected, values, new Error('bad!')); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); @@ -158,9 +163,9 @@ describe('Observable.prototype.mergeScan', () => { const e1subs = '^ !'; const expected = '---#'; - const source = e1.mergeScan((acc, x) => throwError('bad!'), []); + const source = e1.pipe(mergeScan((acc, x) => throwError(new Error('bad!')), [])); - expectObservable(source).toBe(expected, undefined, 'bad!'); + expectObservable(source).toBe(expected, undefined, new Error('bad!')); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); @@ -171,7 +176,7 @@ describe('Observable.prototype.mergeScan', () => { const values = { x: [] }; - const source = e1.mergeScan((acc, x) => Observable.empty(), []); + const source = e1.pipe(mergeScan((acc, x) => EMPTY, [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -184,7 +189,7 @@ describe('Observable.prototype.mergeScan', () => { const values = { x: [] }; - const source = e1.mergeScan((acc, x) => Observable.never(), []); + const source = e1.pipe(mergeScan((acc, x) => NEVER, [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -199,7 +204,7 @@ describe('Observable.prototype.mergeScan', () => { u: [] }; - const source = e1.mergeScan((acc, x) => Observable.of(acc.concat(x)), []); + const source = e1.pipe(mergeScan((acc, x) => of(acc.concat(x)), [])); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -210,7 +215,7 @@ describe('Observable.prototype.mergeScan', () => { const e1subs = '^'; const expected = '-'; - const source = e1.mergeScan((acc, x) => Observable.of(acc.concat(x)), []); + const source = e1.pipe(mergeScan((acc, x) => of(acc.concat(x)), [])); expectObservable(source).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -221,7 +226,7 @@ describe('Observable.prototype.mergeScan', () => { const e1subs = '(^!)'; const expected = '#'; - const source = e1.mergeScan((acc, x) => Observable.of(acc.concat(x)), []); + const source = e1.pipe(mergeScan((acc, x) => of(acc.concat(x)), [])); expectObservable(source).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -240,7 +245,7 @@ describe('Observable.prototype.mergeScan', () => { z: ['b', 'c', 'd', 'e', 'f', 'g'] }; - const source = e1.mergeScan((acc, x) => Observable.of(acc.concat(x)), []); + const source = e1.pipe(mergeScan((acc, x) => of(acc.concat(x)), [])); expectObservable(source, sub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(sub); @@ -263,10 +268,10 @@ describe('Observable.prototype.mergeScan', () => { const expected = '--x-d--e--f--f-g--h--i--i-j--k--l--|'; let index = 0; - const source = e1.mergeScan((acc, x) => { + const source = e1.pipe(mergeScan((acc, x) => { const value = inner[index++]; return value.startWith(acc); - }, 'x', 1); + }, 'x', 1)); expectObservable(source).toBe(expected); @@ -281,7 +286,7 @@ describe('Observable.prototype.mergeScan', () => { const e1subs = '^ !'; const expected = '---------------------(x|)'; - const source = e1.mergeScan((acc, x) => Observable.empty(), ['1']); + const source = e1.pipe(mergeScan((acc, x) => EMPTY, ['1'])); expectObservable(source).toBe(expected, {x: ['1']}); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -292,8 +297,9 @@ describe('Observable.prototype.mergeScan', () => { const e1subs = '^ !'; const expected = '-----------------------(x|)'; - const source = e1.mergeScan((acc, x) => - Observable.empty().delay(50, rxTestScheduler), ['1']); + const source = e1.pipe( + mergeScan((acc, x) => EMPTY.delay(50, rxTestScheduler), ['1']) + ); expectObservable(source).toBe(expected, {x: ['1']}); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -316,10 +322,10 @@ describe('Observable.prototype.mergeScan', () => { const expected = '---x-e--f--f--i----i-l------|'; let index = 0; - const source = e1.mergeScan((acc, x) => { + const source = e1.pipe(mergeScan((acc, x) => { const value = inner[index++]; return value.startWith(acc); - }, 'x', 1); + }, 'x', 1)); expectObservable(source).toBe(expected); @@ -346,10 +352,10 @@ describe('Observable.prototype.mergeScan', () => { const expected = '----x--d-d-eg--fh--hi-j---k---l---|'; let index = 0; - const source = e1.mergeScan((acc, x) => { + const source = e1.pipe(mergeScan((acc, x) => { const value = inner[index++]; return value.startWith(acc); - }, 'x', 2); + }, 'x', 2)); expectObservable(source).toBe(expected); @@ -376,10 +382,10 @@ describe('Observable.prototype.mergeScan', () => { const expected = '---x-e-efh-h-ki------l------|'; let index = 0; - const source = e1.mergeScan((acc, x) => { + const source = e1.pipe(mergeScan((acc, x) => { const value = inner[index++]; return value.startWith(acc); - }, 'x', 2); + }, 'x', 2)); expectObservable(source).toBe(expected); diff --git a/spec/operators/publishReplay-spec.ts b/spec/operators/publishReplay-spec.ts index ba70605883..c4f52f888c 100644 --- a/spec/operators/publishReplay-spec.ts +++ b/spec/operators/publishReplay-spec.ts @@ -1,13 +1,13 @@ import { expect } from 'chai'; -import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; import { throwError } from '../../src/internal/observable/throwError'; +import { EMPTY, NEVER, of, Observable, Subscription } from '../../src'; +import { publishReplay } from '../../src/operators'; +import { ConnectableObservable } from '../../src/internal/observable/ConnectableObservable'; declare function asDiagram(arg: string): Function; declare const type: Function; -const Observable = Rx.Observable; - /** @test {publishReplay} */ describe('Observable.prototype.publishReplay', () => { asDiagram('publishReplay(1)')('should mirror a simple source Observable', () => { @@ -23,7 +23,7 @@ describe('Observable.prototype.publishReplay', () => { }); it('should return a ConnectableObservable-ish', () => { - const source = Observable.of(1).publishReplay(); + const source = of(1).publishReplay(); expect(typeof ( source)._subscribe === 'function').to.be.true; expect(typeof ( source).getSubject === 'function').to.be.true; expect(typeof source.connect === 'function').to.be.true; @@ -116,7 +116,7 @@ describe('Observable.prototype.publishReplay', () => { expectSubscriptions(source.subscriptions).toBe(sourceSubs); // Set up unsubscription action - let connection: Rx.Subscription; + let connection: Subscription; expectObservable(hot(unsub).do(() => { connection.unsubscribe(); })).toBe(unsub); @@ -128,7 +128,7 @@ describe('Observable.prototype.publishReplay', () => { const source = cold('-1-2-3----4-|'); const sourceSubs = '^ ! '; const published = source - .mergeMap((x) => Observable.of(x)) + .mergeMap((x) => of(x)) .publishReplay(1); const subscriber1 = hot('a| ').mergeMapTo(published); const expected1 = '-1-2-3---- '; @@ -144,7 +144,7 @@ describe('Observable.prototype.publishReplay', () => { expectSubscriptions(source.subscriptions).toBe(sourceSubs); // Set up unsubscription action - let connection: Rx.Subscription; + let connection: Subscription; expectObservable(hot(unsub).do(() => { connection.unsubscribe(); })).toBe(unsub); @@ -410,7 +410,7 @@ describe('Observable.prototype.publishReplay', () => { it('should mirror a simple source Observable with selector', () => { const values = {a: 2, b: 4, c: 6, d: 8}; - const selector = (observable: Rx.Observable) => observable.map(v => 2 * +v); + const selector = (observable: Observable) => observable.map(v => 2 * +v); const source = cold('--1-2---3-4---|'); const sourceSubs = '^ !'; const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector); @@ -435,7 +435,7 @@ describe('Observable.prototype.publishReplay', () => { it('should emit an error when the selector returns an Observable that emits an error', () => { const error = "It's broken"; const innerObservable = cold('--5-6----#', undefined, error); - const selector = (observable: Rx.Observable) => observable.mergeMapTo(innerObservable); + const selector = (observable: Observable) => observable.mergeMapTo(innerObservable); const source = cold('--1--2---3---|'); const sourceSubs = '^ !'; const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector); @@ -457,7 +457,7 @@ describe('Observable.prototype.publishReplay', () => { }); it('should not emit and should not complete/error when the selector returns never', () => { - const selector = () => Observable.never(); + const selector = () => NEVER; const source = cold('-'); const sourceSubs = '^'; const published = source.publishReplay(1, Number.POSITIVE_INFINITY, selector); @@ -481,44 +481,44 @@ describe('Observable.prototype.publishReplay', () => { type('should infer the type', () => { /* tslint:disable:no-unused-variable */ - const source = Rx.Observable.of(1, 2, 3); - const result: Rx.ConnectableObservable = source.publishReplay(1); + const source = of(1, 2, 3); + const result: ConnectableObservable = source.publishReplay(1); /* tslint:enable:no-unused-variable */ }); type('should infer the type with a selector', () => { /* tslint:disable:no-unused-variable */ - const source = Rx.Observable.of(1, 2, 3); - const result: Rx.Observable = source.publishReplay(1, undefined, s => s.map(x => x)); + const source = of(1, 2, 3); + const result: Observable = source.publishReplay(1, undefined, s => s.map(x => x)); /* tslint:enable:no-unused-variable */ }); type('should infer the type with a type-changing selector', () => { /* tslint:disable:no-unused-variable */ - const source = Rx.Observable.of(1, 2, 3); - const result: Rx.Observable = source.publishReplay(1, undefined, s => s.map(x => x + '!')); + const source = of(1, 2, 3); + const result: Observable = source.publishReplay(1, undefined, s => s.map(x => x + '!')); /* tslint:enable:no-unused-variable */ }); type('should infer the type for the pipeable operator', () => { /* tslint:disable:no-unused-variable */ - const source = Rx.Observable.of(1, 2, 3); + const source = of(1, 2, 3); // TODO: https://github.com/ReactiveX/rxjs/issues/2972 - const result: Rx.ConnectableObservable = Rx.operators.publishReplay(1)(source); + const result: ConnectableObservable = publishReplay(1)(source); /* tslint:enable:no-unused-variable */ }); type('should infer the type for the pipeable operator with a selector', () => { /* tslint:disable:no-unused-variable */ - const source = Rx.Observable.of(1, 2, 3); - const result: Rx.Observable = source.pipe(Rx.operators.publishReplay(1, undefined, s => s.map(x => x))); + const source = of(1, 2, 3); + const result: Observable = source.pipe(publishReplay(1, undefined, s => s.map(x => x))); /* tslint:enable:no-unused-variable */ }); type('should infer the type for the pipeable operator with a type-changing selector', () => { /* tslint:disable:no-unused-variable */ - const source = Rx.Observable.of(1, 2, 3); - const result: Rx.Observable = source.pipe(Rx.operators.publishReplay(1, undefined, s => s.map(x => x + '!'))); + const source = of(1, 2, 3); + const result: Observable = source.pipe(publishReplay(1, undefined, s => s.map(x => x + '!'))); /* tslint:enable:no-unused-variable */ }); }); diff --git a/spec/operators/race-spec.ts b/spec/operators/race-spec.ts index aa6c4e6f13..9bf6f8e79a 100644 --- a/spec/operators/race-spec.ts +++ b/spec/operators/race-spec.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import * as sinon from 'sinon'; import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { NEVER } from '../../src/internal/observable/never'; const Observable = Rx.Observable; @@ -187,7 +188,7 @@ describe('Observable.prototype.race', () => { it('should unsubscribe former observables if a latter one emits immediately', () => { const onNext = sinon.spy(); const onUnsubscribe = sinon.spy(); - const e1 = Observable.never().finally(onUnsubscribe); // Should be unsubscribed + const e1 = NEVER.finally(onUnsubscribe); // Should be unsubscribed const e2 = Observable.of('b'); // Wins the race e1.race(e2).subscribe(onNext); @@ -198,8 +199,8 @@ describe('Observable.prototype.race', () => { it('should unsubscribe from immediately emitting observable on unsubscription', () => { const onNext = sinon.spy(); const onUnsubscribe = sinon.spy(); - const e1 = Observable.never().startWith('a').finally(onUnsubscribe); // Wins the race - const e2 = Observable.never(); // Loses the race + const e1 = NEVER.startWith('a').finally(onUnsubscribe); // Wins the race + const e2 = NEVER; // Loses the race const subscription = e1.race(e2).subscribe(onNext); expect(onNext.calledWithExactly('a')).to.be.true; diff --git a/spec/operators/refCount-spec.ts b/spec/operators/refCount-spec.ts index 3c93fdd829..723914272b 100644 --- a/spec/operators/refCount-spec.ts +++ b/spec/operators/refCount-spec.ts @@ -1,37 +1,45 @@ import { expect } from 'chai'; -import * as Rx from '../../src/Rx'; import { cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { refCount, publish, publishReplay } from '../../src/operators'; +import { ConnectableObservable } from '../../src/internal/observable/ConnectableObservable'; +import { NEVER, noop, Observable, Observer, Subject } from '../../src'; declare function asDiagram(arg: string): Function; -const Observable = Rx.Observable; - /** @test {refCount} */ -describe('ConnectableObservable.prototype.refCount', () => { +describe('refCount', () => { asDiagram('refCount')('should turn a multicasted Observable an automatically ' + '(dis)connecting hot one', () => { const source = cold('--1-2---3-4--5-|'); const sourceSubs = '^ !'; const expected = '--1-2---3-4--5-|'; - const result = source.publish().refCount(); + const result = source.pipe( + publish(), + refCount() + ); expectObservable(result).toBe(expected); expectSubscriptions(source.subscriptions).toBe(sourceSubs); }); it('should count references', () => { - const connectable = Observable.never().publish(); - const refCounted = connectable.refCount(); + const connectable = NEVER.pipe(publish()); + const refCounted = connectable.pipe( + refCount() + ); - const sub1 = refCounted.subscribe({ next: function () { //noop - } }); - const sub2 = refCounted.subscribe({ next: function () { //noop - } }); - const sub3 = refCounted.subscribe({ next: function () { //noop - } }); + const sub1 = refCounted.subscribe({ + next: noop + }); + const sub2 = refCounted.subscribe({ + next: noop, + }); + const sub3 = refCounted.subscribe({ + next: noop, + }); - expect((connectable)._refCount).to.equal(3); + expect((connectable as any)._refCount).to.equal(3); sub1.unsubscribe(); sub2.unsubscribe(); @@ -40,13 +48,14 @@ describe('ConnectableObservable.prototype.refCount', () => { it('should unsub from the source when all other subscriptions are unsubbed', (done: MochaDone) => { let unsubscribeCalled = false; - const connectable = new Observable((observer: Rx.Observer) => { + const connectable = new Observable(observer => { observer.next(true); return () => { unsubscribeCalled = true; }; - }).publish(); - const refCounted = connectable.refCount(); + }).pipe(publish()); + + const refCounted = connectable.pipe(refCount()); const sub1 = refCounted.subscribe(() => { //noop @@ -55,14 +64,14 @@ describe('ConnectableObservable.prototype.refCount', () => { //noop }); const sub3 = refCounted.subscribe((x: any) => { - expect((connectable)._refCount).to.equal(1); + expect((connectable as any)._refCount).to.equal(1); }); sub1.unsubscribe(); sub2.unsubscribe(); sub3.unsubscribe(); - expect((connectable)._refCount).to.equal(0); + expect((connectable as any)._refCount).to.equal(0); expect(unsubscribeCalled).to.be.true; done(); }); @@ -70,29 +79,29 @@ describe('ConnectableObservable.prototype.refCount', () => { it('should not unsubscribe when a subscriber synchronously unsubscribes if ' + 'other subscribers are present', () => { let unsubscribeCalled = false; - const connectable = new Observable((observer: Rx.Observer) => { + const connectable = new Observable(observer => { observer.next(true); return () => { unsubscribeCalled = true; }; - }).publishReplay(1); + }).pipe(publishReplay(1)); - const refCounted = connectable.refCount(); + const refCounted = connectable.pipe(refCount()); refCounted.subscribe(); refCounted.subscribe().unsubscribe(); - expect((connectable)._refCount).to.equal(1); + expect((connectable as any)._refCount).to.equal(1); expect(unsubscribeCalled).to.be.false; }); it('should not unsubscribe when a subscriber synchronously unsubscribes if ' + 'other subscribers are present and the source is a Subject', () => { - const arr = []; - const subject = new Rx.Subject(); - const connectable = subject.publishReplay(1); - const refCounted = connectable.refCount(); + const arr: string[] = []; + const subject = new Subject(); + const connectable = subject.pipe(publishReplay(1)); + const refCounted = connectable.pipe(refCount()); refCounted.subscribe((val) => { arr.push(val); @@ -104,7 +113,7 @@ describe('ConnectableObservable.prototype.refCount', () => { subject.next('the number two'); - expect((connectable)._refCount).to.equal(1); + expect((connectable as any)._refCount).to.equal(1); expect(arr[0]).to.equal('the number one'); expect(arr[1]).to.equal('the number two'); }); diff --git a/spec/operators/share-spec.ts b/spec/operators/share-spec.ts index ff0e7a3d0c..8cbddc7e8f 100644 --- a/spec/operators/share-spec.ts +++ b/spec/operators/share-spec.ts @@ -1,11 +1,10 @@ import { expect } from 'chai'; -import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { share } from '../../src/operators'; +import { Observable, EMPTY, NEVER } from '../../src'; declare function asDiagram(arg: string): Function; -const Observable = Rx.Observable; - /** @test {share} */ describe('Observable.prototype.share', () => { asDiagram('share')('should mirror a simple source Observable', () => { @@ -21,7 +20,7 @@ describe('Observable.prototype.share', () => { it('should share a single subscription', () => { let subscriptionCount = 0; - const obs = new Observable((observer: Rx.Observer) => { + const obs = new Observable(observer => { subscriptionCount++; }); @@ -298,7 +297,7 @@ describe('Observable.prototype.share', () => { }); it('should not change the output of the observable when never', () => { - const e1 = Observable.never(); + const e1 = NEVER; const expected = '-'; expectObservable(e1.share()).toBe(expected); diff --git a/spec/operators/switch-spec.ts b/spec/operators/switch-spec.ts index 69c00df844..278ab80d3f 100644 --- a/spec/operators/switch-spec.ts +++ b/spec/operators/switch-spec.ts @@ -1,30 +1,29 @@ import { expect } from 'chai'; -import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { Observable, of, NEVER, queueScheduler, Subject } from '../../src'; +import { switchAll } from '../../src/operators'; declare function asDiagram(arg: string): Function; declare const type: Function; -const Observable = Rx.Observable; -const queueScheduler = Rx.Scheduler.queue; /** @test {switch} */ -describe('Observable.prototype.switch', () => { - asDiagram('switch')('should switch a hot observable of cold observables', () => { +describe('switchAll', () => { + asDiagram('switchAll')('should switch a hot observable of cold observables', () => { const x = cold( '--a---b--c---d--| '); const y = cold( '----e---f--g---|'); const e1 = hot( '--x------y-------| ', { x: x, y: y }); const expected = '----a---b----e---f--g---|'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); }); it('should switch to each immediately-scheduled inner Observable', (done) => { - const a = Observable.of(1, 2, 3, queueScheduler); - const b = Observable.of(4, 5, 6, queueScheduler); + const a = of(1, 2, 3, queueScheduler); + const b = of(4, 5, 6, queueScheduler); const r = [1, 4, 5, 6]; let i = 0; - Observable.of(a, b, queueScheduler) - .switch() + of(a, b, queueScheduler) + .pipe(switchAll()) .subscribe((x) => { expect(x).to.equal(r[i++]); }, null, done); @@ -33,25 +32,25 @@ describe('Observable.prototype.switch', () => { it('should unsub inner observables', () => { const unsubbed: string[] = []; - Observable.of('a', 'b').map((x) => + of('a', 'b').map((x) => new Observable((subscriber) => { subscriber.complete(); return () => { unsubbed.push(x); }; })) - .switch() + .pipe(switchAll()) .subscribe(); expect(unsubbed).to.deep.equal(['a', 'b']); }); it('should switch to each inner Observable', (done) => { - const a = Observable.of(1, 2, 3); - const b = Observable.of(4, 5, 6); + const a = of(1, 2, 3); + const b = of(4, 5, 6); const r = [1, 2, 3, 4, 5, 6]; let i = 0; - Observable.of(a, b).switch().subscribe((x) => { + of(a, b).pipe(switchAll()).subscribe((x) => { expect(x).to.equal(r[i++]); }, null, done); }); @@ -63,7 +62,7 @@ describe('Observable.prototype.switch', () => { const ysubs = ' ^ !'; const e1 = hot( '------x-------y------| ', { x: x, y: y }); const expected = '--------a---b----d--e---f---|'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(y.subscriptions).toBe(ysubs); }); @@ -76,7 +75,7 @@ describe('Observable.prototype.switch', () => { const e1 = hot( '------x-------y------| ', { x: x, y: y }); const unsub = ' ! '; const expected = '--------a---b--- '; - expectObservable(e1.switch(), unsub).toBe(expected); + expectObservable(e1.pipe(switchAll()), unsub).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(y.subscriptions).toBe(ysubs); }); @@ -91,9 +90,9 @@ describe('Observable.prototype.switch', () => { const unsub = ' ! '; const result = e1 - .mergeMap((x) => Observable.of(x)) - .switch() - .mergeMap((x) => Observable.of(x)); + .mergeMap((x) => of(x)) + .pipe(switchAll()) + .mergeMap((x) => of(x)); expectObservable(result, unsub).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); @@ -107,7 +106,7 @@ describe('Observable.prototype.switch', () => { const ysubs = ' ^ '; const e1 = hot( '------x-------y------| ', { x: x, y: y }); const expected = '--------a---b----d--e---f-----'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(y.subscriptions).toBe(ysubs); }); @@ -119,7 +118,7 @@ describe('Observable.prototype.switch', () => { const ysubs = ' ^ ! '; const e1 = hot( '------(xy)------------|', { x: x, y: y }); const expected = '---------d--e---f-----|'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(y.subscriptions).toBe(ysubs); }); @@ -131,7 +130,7 @@ describe('Observable.prototype.switch', () => { const ysubs: string[] = []; const e1 = hot( '------x-------y------| ', { x: x, y: y }); const expected = '--------a---# '; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(y.subscriptions).toBe(ysubs); }); @@ -143,7 +142,7 @@ describe('Observable.prototype.switch', () => { const ysubs = ' ^ ! '; const e1 = hot( '------x-------y-------# ', { x: x, y: y }); const expected = '--------a---b----d--e-# '; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(y.subscriptions).toBe(ysubs); }); @@ -153,7 +152,7 @@ describe('Observable.prototype.switch', () => { const e1subs = '^ !'; const expected = '------|'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); @@ -162,7 +161,7 @@ describe('Observable.prototype.switch', () => { const e1subs = '^'; const expected = '-'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); @@ -173,7 +172,7 @@ describe('Observable.prototype.switch', () => { const e1subs = '^ !'; const expected = '--------a---b---c-----|'; - expectObservable(e1.switch()).toBe(expected); + expectObservable(e1.pipe(switchAll())).toBe(expected); expectSubscriptions(x.subscriptions).toBe(xsubs); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); @@ -181,8 +180,8 @@ describe('Observable.prototype.switch', () => { it('should handle an observable of promises', (done) => { const expected = [3]; - Observable.of(Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)) - .switch() + of(Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)) + .pipe(switchAll()) .subscribe((x) => { expect(x).to.equal(expected.shift()); }, null, () => { @@ -192,8 +191,8 @@ describe('Observable.prototype.switch', () => { }); it('should handle an observable of promises, where last rejects', (done) => { - Observable.of(Promise.resolve(1), Promise.resolve(2), Promise.reject(3)) - .switch() + of>(Promise.resolve(1), Promise.resolve(2), Promise.reject(3)) + .pipe(switchAll()) .subscribe(() => { done(new Error('should not be called')); }, (err) => { @@ -208,8 +207,8 @@ describe('Observable.prototype.switch', () => { const expected = [1, 2, 3, 4]; let completed = false; - Observable.of(Observable.never(), Observable.never(), [1, 2, 3, 4]) - .switch() + of(NEVER, NEVER, [1, 2, 3, 4]) + .pipe(switchAll()) .subscribe((x) => { expect(x).to.equal(expected.shift()); }, null, () => { @@ -221,12 +220,12 @@ describe('Observable.prototype.switch', () => { }); it('should not leak when child completes before each switch (prevent memory leaks #2355)', () => { - let iStream: Rx.Subject; - const oStreamControl = new Rx.Subject(); + let iStream: Subject; + const oStreamControl = new Subject(); const oStream = oStreamControl.map(() => { - return (iStream = new Rx.Subject()); + return (iStream = new Subject()); }); - const switcher = oStream.switch(); + const switcher = oStream.pipe(switchAll()); const result: number[] = []; let sub = switcher.subscribe((x) => result.push(x)); @@ -242,11 +241,11 @@ describe('Observable.prototype.switch', () => { }); it('should not leak if we switch before child completes (prevent memory leaks #2355)', () => { - const oStreamControl = new Rx.Subject(); + const oStreamControl = new Subject(); const oStream = oStreamControl.map(() => { - return (new Rx.Subject()); + return (new Subject()); }); - const switcher = oStream.switch(); + const switcher = oStream.pipe(switchAll()); const result: number[] = []; let sub = switcher.subscribe((x) => result.push(x)); @@ -255,57 +254,57 @@ describe('Observable.prototype.switch', () => { }); // Expect two children of switch(): The oStream and the first inner expect( - (sub)._subscriptions[0]._subscriptions.length + (sub as any)._subscriptions[0]._subscriptions.length ).to.equal(2); sub.unsubscribe(); }); type(() => { /* tslint:disable:no-unused-variable */ - const source1 = Rx.Observable.of(1, 2, 3); + const source1 = of(1, 2, 3); const source2 = [1, 2, 3]; const source3 = new Promise(d => d(1)); - let result: Rx.Observable = Rx.Observable + let result: Observable = Observable .of(source1, source2, source3) - .pipe(Rx.operators.switchAll()); + .pipe(switchAll()); /* tslint:enable:no-unused-variable */ }); type(() => { /* tslint:disable:no-unused-variable */ - const source1 = Rx.Observable.of(1, 2, 3); + const source1 = of(1, 2, 3); const source2 = [1, 2, 3]; const source3 = new Promise(d => d(1)); - let result: Rx.Observable = Rx.Observable + let result: Observable = Observable .of(source1, source2, source3) - .switch(); + .pipe(switchAll()); /* tslint:enable:no-unused-variable */ }); type(() => { // coerce type to a specific type /* tslint:disable:no-unused-variable */ - const source1 = Rx.Observable.of(1, 2, 3); + const source1 = of(1, 2, 3); const source2 = [1, 2, 3]; const source3 = new Promise(d => d(1)); - let result: Rx.Observable = Rx.Observable - .of(source1, source2, source3) - .pipe(Rx.operators.switchAll()); + let result: Observable = Observable + .of(source1 as any, source2 as any, source3 as any) + .pipe(switchAll()); /* tslint:enable:no-unused-variable */ }); type(() => { // coerce type to a specific type /* tslint:disable:no-unused-variable */ - const source1 = Rx.Observable.of(1, 2, 3); + const source1 = of(1, 2, 3); const source2 = [1, 2, 3]; const source3 = new Promise(d => d(1)); - let result: Rx.Observable = Rx.Observable - .of(source1, source2, source3) + let result: Observable = Observable + .of(source1 as any, source2 as any, source3 as any) .switch(); /* tslint:enable:no-unused-variable */ }); diff --git a/spec/operators/windowToggle-spec.ts b/spec/operators/windowToggle-spec.ts index b655165369..d934ec8c2f 100644 --- a/spec/operators/windowToggle-spec.ts +++ b/spec/operators/windowToggle-spec.ts @@ -1,15 +1,15 @@ import { expect } from 'chai'; -import * as Rx from '../../src/Rx'; -import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; +import { hot, cold, expectObservable, expectSubscriptions, time } from '../helpers/marble-testing'; +import { Observable, NEVER, of, ObjectUnsubscribedError } from '../../src'; +import { windowToggle, tap } from '../../src/operators'; +import { TestScheduler } from '../../src/testing'; +declare const rxTestScheduler: TestScheduler; declare const type: Function; declare const asDiagram: Function; -declare const rxTestScheduler: Rx.TestScheduler; -const Observable = Rx.Observable; - /** @test {windowToggle} */ -describe('Observable.prototype.windowToggle', () => { +describe('windowToggle', () => { asDiagram('windowToggle')('should emit windows governed by openings and closings', () => { const source = hot('--1--2--^-a--b--c--d--e--f--g--h-|'); const subs = '^ !'; @@ -25,9 +25,9 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '-b--c| '); const y = cold( '-e--f| '); const z = cold( '-h-|'); - const values = { x: x, y: y, z: z }; + const values = { x, y, z }; - const result = source.windowToggle(e2, () => e3); + const result = source.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected, values); expectSubscriptions(source.subscriptions).toBe(subs); @@ -52,12 +52,12 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '-c--d--e--(f|) '); const y = cold( '--f--g--h-| '); const z = cold( '---|'); - const values = { x: x, y: y, z: z }; + const values = { x, y, z }; - const source = e1.windowToggle(e2, (value: string) => { + const source = e1.pipe(windowToggle(e2, (value: string) => { expect(value).to.equal('x'); return e3; - }); + })); expectObservable(source).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -81,10 +81,10 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '--b---c---d---e| '); const y = cold( '--e-| '); const z = cold( '-g---h------| '); - const values = { x: x, y: y, z: z }; + const values = { x, y, z }; let i = 0; - const result = e1.windowToggle(e2, () => close[i++]); + const result = e1.pipe(windowToggle(e2, () => close[i++])); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -110,10 +110,10 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '--b---c---d---e| '); const y = cold( '--e-| '); const z = cold( '-g---h------| '); - const values = { x: x, y: y, z: z }; + const values = { x, y, z }; let i = 0; - const result = e1.windowToggle(e2, () => closings[i++].obs); + const result = e1.pipe(windowToggle(e2, () => closings[i++].obs)); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -135,10 +135,10 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '--b---c---d---e| '); const y = cold( '--e-| '); const z = cold( '-g---h------| '); - const values = { x: x, y: y, z: z }; + const values = { x, y, z }; let i = 0; - const result = e1.windowToggle(e2, () => close[i++]); + const result = e1.pipe(windowToggle(e2, () => close[i++])); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -159,10 +159,10 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '--b---c---d--| '); const y = cold( '--e- '); const unsub = ' ! '; - const values = { x: x, y: y }; + const values = { x, y }; let i = 0; - const result = e1.windowToggle(e2, () => close[i++]); + const result = e1.pipe(windowToggle(e2, () => close[i++])); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -186,13 +186,13 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '--b---c---d--- '); const y = cold( '-- '); const unsub = ' ! '; - const values = { x: x, y: y }; + const values = { x, y }; let i = 0; const result = e1 - .mergeMap((x: string) => Observable.of(x)) - .windowToggle(e2, () => close[i++]) - .mergeMap((x: Rx.Observable) => Observable.of(x)); + .mergeMap((x: string) => of(x)) + .pipe(windowToggle(e2, () => close[i++])) + .mergeMap(x => of(x)); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -209,19 +209,20 @@ describe('Observable.prototype.windowToggle', () => { const x = cold( '--a--b--c- '); const unsub = ' ! '; const late = time('---------------| '); - const values = { x: x }; + const values = { x }; - let window; - const result = source - .windowToggle(open, () => Observable.never()) - .do((w: any) => { window = w; }); + let window: Observable; + const result = source.pipe( + windowToggle(open, () => NEVER), + tap(w => { window = w; }), + ); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(source.subscriptions).toBe(sourceSubs); rxTestScheduler.schedule(() => { expect(() => { window.subscribe(); - }).to.throw(Rx.ObjectUnsubscribedError); + }).to.throw(ObjectUnsubscribedError); }, late); }); @@ -238,12 +239,12 @@ describe('Observable.prototype.windowToggle', () => { const values = { x: x }; let i = 0; - const result = e1.windowToggle(e2, () => { + const result = e1.pipe(windowToggle(e2, () => { if (i === 1) { throw 'error'; } return close[i++]; - }); + })); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -260,10 +261,10 @@ describe('Observable.prototype.windowToggle', () => { const expected = '--x-----------(y#) '; const x = cold( '--b---c---d-# '); const y = cold( '# '); - const values = { x: x, y: y }; + const values = { x, y }; let i = 0; - const result = e1.windowToggle(e2, () => close[i++]); + const result = e1.pipe(windowToggle(e2, () => close[i++])); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -280,10 +281,10 @@ describe('Observable.prototype.windowToggle', () => { const expected = '--x-----------y----# '; const x = cold( '--b---c---d---e| '); const y = cold( '--e--# '); - const values = { x: x, y: y }; + const values = { x, y }; let i = 0; - const result = e1.windowToggle(e2, () => close[i++]); + const result = e1.pipe(windowToggle(e2, () => close[i++])); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -300,10 +301,10 @@ describe('Observable.prototype.windowToggle', () => { const expected = '--x-----------y----# '; const x = cold( '--b---c---d---e| '); const y = cold( '--e--# '); - const values = { x: x, y: y }; + const values = { x, y }; let i = 0; - const result = e1.windowToggle(e2, () => close[i++]); + const result = e1.pipe(windowToggle(e2, () => close[i++])); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -318,7 +319,7 @@ describe('Observable.prototype.windowToggle', () => { const e3 = cold( '-----c--|'); const expected = '|'; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -333,7 +334,7 @@ describe('Observable.prototype.windowToggle', () => { const e3 = cold('-----c--|'); const expected = '#'; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -353,9 +354,9 @@ describe('Observable.prototype.windowToggle', () => { const y = cold( '--| '); const z = cold( '--| '); const unsub = ' !'; - const values = { u: u, v: v, x: x, y: y, z: z }; + const values = { u: u, v: v, x, y, z }; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result, unsub).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -370,7 +371,7 @@ describe('Observable.prototype.windowToggle', () => { const e3 = cold( '--c-| '); const expected = '-----------------------------------|'; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -386,9 +387,9 @@ describe('Observable.prototype.windowToggle', () => { const expected = '---x---------------y---------------|'; const x = cold( '-b---c---d---e---f---g---h------|'); const y = cold( '-f---g---h------|'); - const values = { x: x, y: y }; + const values = { x, y }; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); @@ -404,7 +405,7 @@ describe('Observable.prototype.windowToggle', () => { const subs = '(^!)'; const expected = '#'; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(subs); @@ -421,9 +422,9 @@ describe('Observable.prototype.windowToggle', () => { const expected = '---x---------------y---------------|'; const x = cold( '|'); const y = cold( '|'); - const values = { x: x, y: y }; + const values = { x, y }; - const result = e1.windowToggle(e2, () => e3); + const result = e1.pipe(windowToggle(e2, () => e3)); expectObservable(result).toBe(expected, values); expectSubscriptions(e1.subscriptions).toBe(e1subs); diff --git a/spec/schedulers/TestScheduler-spec.ts b/spec/schedulers/TestScheduler-spec.ts index 387737f6a6..f1ce4527cd 100644 --- a/spec/schedulers/TestScheduler-spec.ts +++ b/spec/schedulers/TestScheduler-spec.ts @@ -1,10 +1,11 @@ import { expect } from 'chai'; -import * as Rx from '../../src/Rx'; import { hot, cold, expectObservable, expectSubscriptions, time } from '../helpers/marble-testing'; -declare const rxTestScheduler: Rx.TestScheduler; -const Notification = Rx.Notification; -const TestScheduler = Rx.TestScheduler; +import { TestScheduler } from '../../src/testing'; +import { Notification } from '../../src/internal/Notification'; +import { Observable, NEVER, EMPTY, Subject, of } from '../../src'; + +declare const rxTestScheduler: TestScheduler; /** @test {TestScheduler} */ describe('TestScheduler', () => { @@ -108,9 +109,9 @@ describe('TestScheduler', () => { it('should create a cold observable', () => { const expected = ['A', 'B']; const scheduler = new TestScheduler(null); - const source = scheduler.createColdObservable('--a---b--|', { a: 'A', b: 'B' }); - expect(source instanceof Rx.Observable).to.be.true; - source.subscribe((x: string) => { + const source = scheduler.createColdObservable('--a---b--|', { a: 'A', b: 'B' }); + expect(source).to.be.an.instanceOf(Observable); + source.subscribe(x => { expect(x).to.equal(expected.shift()); }); scheduler.flush(); @@ -122,9 +123,9 @@ describe('TestScheduler', () => { it('should create a cold observable', () => { const expected = ['A', 'B']; const scheduler = new TestScheduler(null); - const source = scheduler.createHotObservable('--a---b--|', { a: 'A', b: 'B' }); - expect(source).to.be.an.instanceof(Rx.Subject); - source.subscribe((x: string) => { + const source = scheduler.createHotObservable('--a---b--|', { a: 'A', b: 'B' }); + expect(source).to.be.an.instanceof(Subject); + source.subscribe(x => { expect(x).to.equal(expected.shift()); }); scheduler.flush(); @@ -165,7 +166,7 @@ describe('TestScheduler', () => { it('should create a hot observable', () => { const source = hot('---^-a-b-|', { a: 1, b: 2 }); - expect(source instanceof Rx.Subject).to.be.true; + expect(source).to.be.an.instanceOf(Subject); expectObservable(source).toBe('--a-b-|', { a: 1, b: 2 }); }); }); @@ -188,21 +189,21 @@ describe('TestScheduler', () => { }); it('should return an object with a toBe function', () => { - expect(expectObservable(Rx.Observable.of(1)).toBe).to.be.a('function'); + expect(expectObservable(of(1)).toBe).to.be.a('function'); }); it('should append to flushTests array', () => { - expectObservable(Rx.Observable.empty()); + expectObservable(EMPTY); expect((rxTestScheduler).flushTests.length).to.equal(1); }); it('should handle empty', () => { - expectObservable(Rx.Observable.empty()).toBe('|', {}); + expectObservable(EMPTY).toBe('|', {}); }); it('should handle never', () => { - expectObservable(Rx.Observable.never()).toBe('-', {}); - expectObservable(Rx.Observable.never()).toBe('---', {}); + expectObservable(NEVER).toBe('-', {}); + expectObservable(NEVER).toBe('---', {}); }); it('should accept an unsubscription marble diagram', () => { diff --git a/src/add/observable/never.ts b/src/add/observable/never.ts index 83f3cd9752..bc1950fa11 100644 --- a/src/add/observable/never.ts +++ b/src/add/observable/never.ts @@ -1,5 +1,9 @@ import { Observable } from '../../internal/Observable'; -import { never as staticNever } from '../../internal/observable/never'; +import { NEVER } from '../../internal/observable/never'; + +export function staticNever() { + return NEVER; +} Observable.never = staticNever; diff --git a/src/index.ts b/src/index.ts index b8a8d1efdb..3d8430d224 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,6 @@ export { generate } from './internal/observable/generate'; export { iif } from './internal/observable/iif'; export { interval } from './internal/observable/interval'; export { merge } from './internal/observable/merge'; -export { never } from './internal/observable/never'; export { of } from './internal/observable/of'; export { onErrorResumeNext } from './internal/observable/onErrorResumeNext'; export { pairs } from './internal/observable/pairs'; @@ -58,6 +57,7 @@ export { zip } from './internal/observable/zip'; /* Constants */ export { EMPTY } from './internal/observable/empty'; +export { NEVER } from './internal/observable/never'; /* Types */ export * from './internal/types'; diff --git a/src/internal/observable/never.ts b/src/internal/observable/never.ts index 1d1998ccbe..cdf3831bb2 100644 --- a/src/internal/observable/never.ts +++ b/src/internal/observable/never.ts @@ -1,40 +1,27 @@ import { Observable } from '../Observable'; import { noop } from '../util/noop'; -/** @internal */ -export const NEVER = new Observable(noop); - /** - * Creates an Observable that emits no items to the Observer. - * - * An Observable that never emits anything. + * An Observable that emits no items to the Observer and never completes. * * * - * This static operator is useful for creating a simple Observable that emits - * neither values nor errors nor the completion notification. It can be used - * for testing purposes or for composing with other Observables. Please note - * that by never emitting a complete notification, this Observable keeps the - * subscription from being disposed automatically. Subscriptions need to be - * manually disposed. + * A simple Observable that emits neither values nor errors nor the completion + * notification. It can be used for testing purposes or for composing with other + * Observables. Please note that by never emitting a complete notification, this + * Observable keeps the subscription from being disposed automatically. + * Subscriptions need to be manually disposed. * * @example Emit the number 7, then never emit anything else (not even complete). * function info() { * console.log('Will not be called'); * } - * var result = Rx.Observable.never().startWith(7); + * var result = NEVER.startWith(7); * result.subscribe(x => console.log(x), info, info); * * @see {@link create} - * @see {@link empty} + * @see {@link EMPTY} * @see {@link of} - * @see {@link throw} - * - * @return {Observable} A "never" Observable: never emits anything. - * @static true - * @name never - * @owner Observable + * @see {@link throwError} */ -export function never(): Observable { - return NEVER; -} +export const NEVER = new Observable(noop);