Skip to content

Commit

Permalink
fix(subscribeToResult): accept array-like as result
Browse files Browse the repository at this point in the history
Accept array-like as a result to subscribe, so that
Observable.from and operators using subscribeToResult
have identical behaviour.
  • Loading branch information
mpodlasin committed Feb 12, 2017
1 parent 31dfc73 commit 14685ba
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
11 changes: 11 additions & 0 deletions spec/util/subscribeToResult-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ describe('subscribeToResult', () => {
expect(expected).to.be.deep.equal(result);
});

it('should subscribe to an array-like and emit synchronously', () => {
const result = {0: 0, 1: 1, 2: 2, length: 3};
const expected = [];

const subscriber = new OuterSubscriber(x => expected.push(x));

subscribeToResult(subscriber, result);

expect(expected).to.be.deep.equal([0, 1, 2]);
});

it('should subscribe to a promise', (done: MochaDone) => {
const result = Promise.resolve(42);

Expand Down
3 changes: 1 addition & 2 deletions src/observable/FromObservable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isArray } from '../util/isArray';
import { isArrayLike } from '../util/isArrayLike';
import { isPromise } from '../util/isPromise';
import { PromiseObservable } from './PromiseObservable';
import { IteratorObservable } from'./IteratorObservable';
Expand All @@ -12,8 +13,6 @@ import { Subscriber } from '../Subscriber';
import { ObserveOnSubscriber } from '../operator/observeOn';
import { $$observable } from '../symbol/observable';

const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number');

/**
* We need this JSDoc comment for affecting ESDoc.
* @extends {Ignored}
Expand Down
1 change: 1 addition & 0 deletions src/util/isArrayLike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number');
4 changes: 2 additions & 2 deletions src/util/subscribeToResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { root } from './root';
import { isArray } from './isArray';
import { isArrayLike } from './isArrayLike';
import { isPromise } from './isPromise';
import { isObject } from './isObject';
import { Subscriber } from '../Subscriber';
Expand Down Expand Up @@ -32,7 +32,7 @@ export function subscribeToResult<T>(outerSubscriber: OuterSubscriber<any, any>,
} else {
return result.subscribe(destination);
}
} else if (isArray(result)) {
} else if (isArrayLike(result)) {
for (let i = 0, len = result.length; i < len && !destination.closed; i++) {
destination.next(result[i]);
}
Expand Down

0 comments on commit 14685ba

Please sign in to comment.