Skip to content

Commit

Permalink
feat(Iterator): add iterator return to catch operators
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpodwysocki committed Aug 17, 2017
1 parent 336aa07 commit a7898ca
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/asynciterable/catch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AsyncIterableX } from '../asynciterable';
import { returnAsyncIterator } from '../internal/returniterator';

class CatchAllAsyncIterable<TSource> extends AsyncIterableX<TSource> {
private _source: Iterable<AsyncIterable<TSource>>;
Expand All @@ -22,11 +23,15 @@ class CatchAllAsyncIterable<TSource> extends AsyncIterableX<TSource> {

try {
const { done, value } = await it.next();
if (done) { break; }
if (done) {
await returnAsyncIterator(it);
break;
}
c = value;
} catch (e) {
error = e;
hasError = true;
await returnAsyncIterator(it);
break;
}

Expand Down
7 changes: 6 additions & 1 deletion src/asynciterable/catchwith.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AsyncIterableX } from '../asynciterable';
import { returnAsyncIterator } from '../internal/returniterator';

class CatchWithAsyncIterable<TSource> extends AsyncIterableX<TSource> {
private _source: AsyncIterable<TSource>;
Expand All @@ -19,10 +20,14 @@ class CatchWithAsyncIterable<TSource> extends AsyncIterableX<TSource> {

try {
c = await it.next();
if (c.done) { break; }
if (c.done) {
await returnAsyncIterator(it);
break;
}
} catch (e) {
err = await this._handler(e);
hasError = true;
await returnAsyncIterator(it);
break;
}

Expand Down
17 changes: 17 additions & 0 deletions src/internal/returniterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @ignore
*/
export function returnIterator<T>(it: Iterator<T>) {
if (typeof it.return === 'function') {
it.return();
}
}

/**
* @ignore
*/
export async function returnAsyncIterator<T>(it: AsyncIterator<T>): Promise<void> {
if (typeof it.return === 'function') {
await it.return();
}
}
7 changes: 6 additions & 1 deletion src/iterable/catch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IterableX } from '../iterable';
import { returnIterator } from '../internal/returniterator';

class CatchIterable<TSource> extends IterableX<TSource> {
private _source: Iterable<Iterable<TSource>>;
Expand All @@ -22,11 +23,15 @@ class CatchIterable<TSource> extends IterableX<TSource> {

try {
const { done, value } = it.next();
if (done) { break; }
if (done) {
returnIterator(it);
break;
}
c = value;
} catch (e) {
error = e;
hasError = true;
returnIterator(it);
break;
}

Expand Down
7 changes: 6 additions & 1 deletion src/iterable/catchwith.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IterableX } from '../iterable';
import { returnIterator } from '../internal/returniterator';

class CatchWithIterable<TSource> extends IterableX<TSource> {
private _source: Iterable<TSource>;
Expand All @@ -17,10 +18,14 @@ class CatchWithIterable<TSource> extends IterableX<TSource> {

try {
c = it.next();
if (c.done) { break; }
if (c.done) {
returnIterator(it);
break;
}
} catch (e) {
err = this._handler(e);
hasError = true;
returnIterator(it);
break;
}

Expand Down

0 comments on commit a7898ca

Please sign in to comment.