Skip to content

Commit

Permalink
fix(Observable): empty ctor returns valid Observable (#3464)
Browse files Browse the repository at this point in the history
Before if you subscribed such as `(new Observable()).subscribe()` it would throw an error, this remedies that and will effectively return a never observable, however _not_ the same instance as `NEVER`
  • Loading branch information
benlesh authored Mar 24, 2018
1 parent f25b38d commit 58b8ebc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ describe('Observable', () => {
});
});

it('should allow empty ctor, which is effectively a never-observable', () => {
const result = new Observable<any>();
expectObservable(result).toBe('-');
});

describe('forEach', () => {
it('should iterate and return a Promise', (done) => {
const expected = [1, 2, 3];
Expand Down
6 changes: 3 additions & 3 deletions src/internal/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ export class Observable<T> implements Subscribable<T> {
/** @internal */
public _isScalar: boolean = false;

/** @internal */
protected source: Observable<any>;
/** @internal */

protected operator: Operator<any, T>;

/**
Expand Down Expand Up @@ -248,7 +247,8 @@ export class Observable<T> implements Subscribable<T> {

/** @internal */
protected _subscribe(subscriber: Subscriber<any>): TeardownLogic {
return this.source.subscribe(subscriber);
const { source } = this;
return source && source.subscribe(subscriber);
}

// TODO(benlesh): determine if this is still necessary
Expand Down

0 comments on commit 58b8ebc

Please sign in to comment.