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(IteratorObservable): get new iterator for each subscription #2497

Merged
merged 1 commit into from
Oct 30, 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
18 changes: 18 additions & 0 deletions spec/observables/IteratorObservable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(new Int32Array([10, 20])).observeOn(rxTestScheduler);

let v1, v2: Array<Rx.Notification<any>>;
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,
Expand Down
14 changes: 6 additions & 8 deletions src/observable/IteratorObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { Subscriber } from '../Subscriber';
* @hide true
*/
export class IteratorObservable<T> extends Observable<T> {
private iterator: any;

static create<T>(iterator: any, scheduler?: IScheduler): IteratorObservable<T> {
return new IteratorObservable(iterator, scheduler);
}
Expand Down Expand Up @@ -45,20 +43,20 @@ export class IteratorObservable<T> extends Observable<T> {
(<any> 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[Symbol_iterator]) {
throw new TypeError('object is not iterable');
}

this.iterator = getIterator(iterator);
}

protected _subscribe(subscriber: Subscriber<T>): TeardownLogic {

let index = 0;
const { iterator, scheduler } = this;
const { scheduler } = this;
const iterator = getIterator(this.iteratorObject);

if (scheduler) {
return scheduler.schedule(IteratorObservable.dispatch, 0, {
Expand Down