From d57fa52c6aca3161622f62d81f1985f3b559b8e1 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Mon, 22 Jan 2018 15:51:14 -0800 Subject: [PATCH] feat(never): always return the same instance (#3249) - never always returns the same Observable instance - Remove NeverObservable - never return value now typed as `Observable` (lol) BREAKING CHANGE: `never()` always returns the same instance BREAKING CHANGE: TypeScript typing for `never()` is now `Observable` and the function no longer requires a generic type. --- spec/observables/never-spec.ts | 15 +++--- src/internal/observable/NeverObservable.ts | 53 ---------------------- src/internal/observable/never.ts | 41 ++++++++++++++++- 3 files changed, 48 insertions(+), 61 deletions(-) delete mode 100644 src/internal/observable/NeverObservable.ts diff --git a/spec/observables/never-spec.ts b/spec/observables/never-spec.ts index 124de22f9e..4a4710d38a 100644 --- a/spec/observables/never-spec.ts +++ b/spec/observables/never-spec.ts @@ -1,16 +1,19 @@ -import * as Rx from '../../src/Rx'; +import { never } from '../../src/create'; +import { expect } from 'chai'; import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports -declare const { asDiagram }; +declare const asDiagram: any; declare const expectObservable: typeof marbleTestingSignature.expectObservable; -const Observable = Rx.Observable; - /** @test {never} */ -describe('Observable.never', () => { +describe('never', () => { asDiagram('never')('should create a cold observable that never emits', () => { const expected = '-'; - const e1 = Observable.never(); + const e1 = never(); expectObservable(e1).toBe(expected); }); + + it('should return the same instance every time', () => { + expect(never()).to.equal(never()); + }); }); diff --git a/src/internal/observable/NeverObservable.ts b/src/internal/observable/NeverObservable.ts deleted file mode 100644 index 9a4beae2f3..0000000000 --- a/src/internal/observable/NeverObservable.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { Observable } from '../Observable'; -import { Subscriber } from '../Subscriber'; -import { noop } from '..//util/noop'; - -/** - * We need this JSDoc comment for affecting ESDoc. - * @extends {Ignored} - * @hide true - */ -export class NeverObservable extends Observable { - /** - * Creates an Observable that emits no items to the Observer. - * - * An Observable that never emits anything. - * - * - * - * 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. - * - * @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); - * result.subscribe(x => console.log(x), info, info); - * - * @see {@link create} - * @see {@link empty} - * @see {@link of} - * @see {@link throw} - * - * @return {Observable} A "never" Observable: never emits anything. - * @static true - * @name never - * @owner Observable - */ - static create() { - return new NeverObservable(); - } - - constructor() { - super(); - } - - protected _subscribe(subscriber: Subscriber): void { - noop(); - } -} diff --git a/src/internal/observable/never.ts b/src/internal/observable/never.ts index 5efa8b58c4..1d1998ccbe 100644 --- a/src/internal/observable/never.ts +++ b/src/internal/observable/never.ts @@ -1,3 +1,40 @@ -import { NeverObservable } from './NeverObservable'; +import { Observable } from '../Observable'; +import { noop } from '../util/noop'; -export const never = NeverObservable.create; \ No newline at end of file +/** @internal */ +export const NEVER = new Observable(noop); + +/** + * Creates an Observable that emits no items to the Observer. + * + * An Observable that never emits anything. + * + * + * + * 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. + * + * @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); + * result.subscribe(x => console.log(x), info, info); + * + * @see {@link create} + * @see {@link empty} + * @see {@link of} + * @see {@link throw} + * + * @return {Observable} A "never" Observable: never emits anything. + * @static true + * @name never + * @owner Observable + */ +export function never(): Observable { + return NEVER; +}