Skip to content

Commit

Permalink
Ensure that iterables go through return step when map function return…
Browse files Browse the repository at this point in the history
…s done indicator
  • Loading branch information
kriszyp committed Jan 30, 2024
1 parent e3753ea commit 7e7d584
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions util/RangeIterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export class RangeIterable {
if (!async) {
throw new Error('Can not synchronously iterate with asynchronous values');
}
return iteratorResult.then(iteratorResult => this.next(iteratorResult), onError);
return iteratorResult.then(iteratorResult => this.next(iteratorResult), (error) => {
this.return();
throw error;
});
}
}
if (iteratorResult.done === true) {
Expand All @@ -52,34 +55,39 @@ export class RangeIterable {
this.next() :
{
value: result
}, onError);
}, (error) => {
this.return();
throw error;
});
}
} while(result === SKIP);
if (result === DONE) {
if (iterable.onDone) iterable.onDone();
return result;
return this.return();
}
return {
value: result
};
} catch(error) {
onError(error);
this.return();
throw error;
}
},
return() {
if (iterable.onDone) iterable.onDone();
return iterator.return();
if (!this.done) {
this.done = true;
if (iterable.onDone) iterable.onDone();
return iterator.return();
}
},
throw() {
if (iterable.onDone) iterable.onDone();
return iterator.throw();
if (!this.done) {
this.done = true;
if (iterable.onDone) iterable.onDone();
return iterator.throw();
}
}
};
};
function onError(error) {
if (iterable.onDone) iterable.onDone();
throw error;
}
return iterable;
}
[Symbol.asyncIterator]() {
Expand Down Expand Up @@ -120,11 +128,15 @@ export class RangeIterable {
if (!async) throw new Error('Can not synchronously iterate with asynchronous values');
result.then((result) => {
if (result.done()) concatIterable.onDone();
}, onError);
}, (error) => {
this.return();
throw error;
});
} else if (result.done) concatIterable.onDone();
}
} catch (error) {
onError(error);
this.return();
throw error;
}
} else {
if (concatIterable.onDone) concatIterable.onDone();
Expand All @@ -145,7 +157,8 @@ export class RangeIterable {
if (result.done) return iteratorDone(result);
return result;
} catch (error) {
onError(error);
this.return();
throw error;
}
},
return() {
Expand All @@ -158,11 +171,6 @@ export class RangeIterable {
}
};
};
function onError(error) {
if (iterable.onDone) iterable.onDone();
throw error;
}

return concatIterable;
}

Expand Down Expand Up @@ -221,7 +229,8 @@ export class RangeIterable {
}
} while(true);
} catch (error) {
onError(error);
this.return();
throw error;
}
},
return() {
Expand All @@ -238,10 +247,6 @@ export class RangeIterable {
}
};
};
function onError(error) {
if (iterable.onDone) iterable.onDone();
throw error;
}
return mappedIterable;
}

Expand Down

0 comments on commit 7e7d584

Please sign in to comment.