From 14685bac5acafe0553eaa5d2885c635d10141d9b Mon Sep 17 00:00:00 2001 From: Mateusz Podlasin Date: Sun, 12 Feb 2017 20:40:52 +0100 Subject: [PATCH] fix(subscribeToResult): accept array-like as result Accept array-like as a result to subscribe, so that Observable.from and operators using subscribeToResult have identical behaviour. --- spec/util/subscribeToResult-spec.ts | 11 +++++++++++ src/observable/FromObservable.ts | 3 +-- src/util/isArrayLike.ts | 1 + src/util/subscribeToResult.ts | 4 ++-- 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 src/util/isArrayLike.ts diff --git a/spec/util/subscribeToResult-spec.ts b/spec/util/subscribeToResult-spec.ts index 6101060b6c..57b4b259aa 100644 --- a/spec/util/subscribeToResult-spec.ts +++ b/spec/util/subscribeToResult-spec.ts @@ -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); diff --git a/src/observable/FromObservable.ts b/src/observable/FromObservable.ts index 03d9306639..5e9bf48953 100644 --- a/src/observable/FromObservable.ts +++ b/src/observable/FromObservable.ts @@ -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'; @@ -12,8 +13,6 @@ import { Subscriber } from '../Subscriber'; import { ObserveOnSubscriber } from '../operator/observeOn'; import { $$observable } from '../symbol/observable'; -const isArrayLike = ((x: any): x is ArrayLike => x && typeof x.length === 'number'); - /** * We need this JSDoc comment for affecting ESDoc. * @extends {Ignored} diff --git a/src/util/isArrayLike.ts b/src/util/isArrayLike.ts new file mode 100644 index 0000000000..15da7a50f4 --- /dev/null +++ b/src/util/isArrayLike.ts @@ -0,0 +1 @@ +export const isArrayLike = ((x: any): x is ArrayLike => x && typeof x.length === 'number'); \ No newline at end of file diff --git a/src/util/subscribeToResult.ts b/src/util/subscribeToResult.ts index 3c603f1d80..29af49afa5 100644 --- a/src/util/subscribeToResult.ts +++ b/src/util/subscribeToResult.ts @@ -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'; @@ -32,7 +32,7 @@ export function subscribeToResult(outerSubscriber: OuterSubscriber, } 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]); }