Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(subscribeToResult): accept array-like as result #2364

Merged
merged 1 commit into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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