From b427cadbdb9e1dbd475d7385cf7eaf6a6ebb00c5 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Mon, 27 Mar 2017 10:14:10 -0700 Subject: [PATCH] fix(IteratorObservable): get new iterator for each subscription - closes #2496 --- spec/observables/IteratorObservable-spec.ts | 18 ++++++++++++++++++ src/observable/IteratorObservable.ts | 17 ++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/spec/observables/IteratorObservable-spec.ts b/spec/observables/IteratorObservable-spec.ts index 63b1d73761..6b0397be4e 100644 --- a/spec/observables/IteratorObservable-spec.ts +++ b/spec/observables/IteratorObservable-spec.ts @@ -47,6 +47,24 @@ describe('IteratorObservable', () => { ); }); + it('should get new iterator for each subscription', () => { + const expected = [ + Rx.Notification.createNext(10), + Rx.Notification.createNext(20), + Rx.Notification.createComplete() + ]; + + const e1 = IteratorObservable.create(new Int32Array([10, 20])).observeOn(rxTestScheduler); + + let v1, v2: Array>; + e1.materialize().toArray().subscribe((x) => v1 = x); + e1.materialize().toArray().subscribe((x) => v2 = x); + + rxTestScheduler.flush(); + expect(v1).to.deep.equal(expected); + expect(v2).to.deep.equal(expected); + }); + it('should finalize generators if the subscription ends', () => { const iterator = { finalized: false, diff --git a/src/observable/IteratorObservable.ts b/src/observable/IteratorObservable.ts index 5bc3f8897c..b905128124 100644 --- a/src/observable/IteratorObservable.ts +++ b/src/observable/IteratorObservable.ts @@ -11,8 +11,6 @@ import { Subscriber } from '../Subscriber'; * @hide true */ export class IteratorObservable extends Observable { - private iterator: any; - static create(iterator: any, scheduler?: IScheduler): IteratorObservable { return new IteratorObservable(iterator, scheduler); } @@ -45,20 +43,20 @@ export class IteratorObservable extends Observable { ( this).schedule(state); } - constructor(iterator: any, private scheduler?: IScheduler) { + constructor(private readonly iteratorObject: any, private scheduler?: IScheduler) { super(); - if (iterator == null) { + if (iteratorObject == null) { throw new Error('iterator cannot be null.'); + } else if (!iteratorObject[$$iterator]) { + throw new TypeError('object is not iterable'); } - - this.iterator = getIterator(iterator); } protected _subscribe(subscriber: Subscriber): TeardownLogic { - let index = 0; - const { iterator, scheduler } = this; + const { scheduler } = this; + const iterator = getIterator(this.iteratorObject); if (scheduler) { return scheduler.schedule(IteratorObservable.dispatch, 0, { @@ -126,9 +124,6 @@ function getIterator(obj: any) { if (!i && obj.length !== undefined) { return new ArrayIterator(obj); } - if (!i) { - throw new TypeError('object is not iterable'); - } return obj[Symbol_iterator](); }