Skip to content

Commit

Permalink
Merge branch 'master' into ws-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh authored Oct 19, 2016
2 parents 2775e06 + 8ebee66 commit c0e365b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ By contributing or commenting on issues in this repository, whether you've read
### ES6 via npm

```sh
npm install rxjs-es
npm install rxjs
```

To import the entire core set of functionality:
Expand Down
63 changes: 63 additions & 0 deletions spec/observables/IteratorObservable-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
import {queue} from '../../dist/cjs/scheduler/queue';
import {IteratorObservable} from '../../dist/cjs/observable/IteratorObservable';

declare const expectObservable;
Expand Down Expand Up @@ -46,6 +47,68 @@ describe('IteratorObservable', () => {
);
});

it('should finalize generators if the subscription ends', () => {
const iterator = {
finalized: false,
next() {
return { value: 'duck', done: false };
},
return() {
this.finalized = true;
}
};

const iterable = {
[Rx.Symbol.iterator]() {
return iterator;
}
};

const results = [];

IteratorObservable.create(iterable)
.take(3)
.subscribe(
x => results.push(x),
null,
() => results.push('GOOSE!')
);

expect(results).to.deep.equal(['duck', 'duck', 'duck', 'GOOSE!']);
expect(iterator.finalized).to.be.true;
});

it('should finalize generators if the subscription and it is scheduled', () => {
const iterator = {
finalized: false,
next() {
return { value: 'duck', done: false };
},
return() {
this.finalized = true;
}
};

const iterable = {
[Rx.Symbol.iterator]() {
return iterator;
}
};

const results = [];

IteratorObservable.create(iterable, queue)
.take(3)
.subscribe(
x => results.push(x),
null,
() => results.push('GOOSE!')
);

expect(results).to.deep.equal(['duck', 'duck', 'duck', 'GOOSE!']);
expect(iterator.finalized).to.be.true;
});

it('should emit members of an array iterator on a particular scheduler', () => {
const source = IteratorObservable.create(
[10, 20, 30, 40],
Expand Down
6 changes: 6 additions & 0 deletions src/observable/IteratorObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class IteratorObservable<T> extends Observable<T> {
state.index = index + 1;

if (subscriber.closed) {
if (typeof iterator.return === 'function') {
iterator.return();
}
return;
}

Expand Down Expand Up @@ -71,6 +74,9 @@ export class IteratorObservable<T> extends Observable<T> {
subscriber.next(result.value);
}
if (subscriber.closed) {
if (typeof iterator.return === 'function') {
iterator.return();
}
break;
}
} while (true);
Expand Down
2 changes: 1 addition & 1 deletion src/operator/do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { TeardownLogic } from '../Subscription';
* @owner Observable
*/
/* tslint:disable:max-line-length */
export function _do<T>(next: (x: T) => void, error?: (e: any) => void, complete?: (this: Observable<T>) => void): Observable<T>;
export function _do<T>(this: Observable<T>, next: (x: T) => void, error?: (e: any) => void, complete?: () => void): Observable<T>;
export function _do<T>(this: Observable<T>, observer: PartialObserver<T>): Observable<T>;
/* tslint:disable:max-line-length */
export function _do<T>(this: Observable<T>, nextOrObserver?: PartialObserver<T> | ((x: T) => void),
Expand Down

0 comments on commit c0e365b

Please sign in to comment.