Skip to content

Commit

Permalink
Switch touchNext to also store promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibi Braniecki committed Jul 30, 2018
1 parent 6e383e2 commit 1826f04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/cached_async_iterable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ export default class CachedAsyncIterable extends CachedIterable {
let idx = 0;
while (idx++ < count) {
const last = this[this.length - 1];
if (last && last.done) {
if (last && (await last).done) {
break;
}
this.push(await this.iterator.next());
this.push(this.iterator.next());
}
// Return the last cached {value, done} object to allow the calling
// code to decide if it needs to call touchNext again.
return this[this.length - 1];
return await this[this.length - 1];
}
}
40 changes: 33 additions & 7 deletions test/cached_async_iterable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ suite("CachedAsyncIterable", function() {

const iterable = new CachedAsyncIterable(generate());
await iterable.touchNext();
assert.deepEqual([...iterable], [o1]);
let x = [...iterable];
assert.deepEqual([...iterable], [o1])
});

test("async iterable with all cached elements", async function() {
Expand Down Expand Up @@ -175,19 +176,21 @@ suite("CachedAsyncIterable", function() {
let counter = 0;

async function *generate() {
while (true) {
counter++;
yield await Promise.resolve();
}
while (true) {
counter++;
yield await Promise.resolve();
}
}

// We're testing that if the first call to asyncIterator has been
// made, but the value of it has not been returned yet,
// the consequitive call returns the same Promise rather than,
// attempting to fetch the next item from the iterator.
const iterable = new CachedAsyncIterable(generate());
iterable[Symbol.asyncIterator]().next();
await iterable[Symbol.asyncIterator]().next();
await Promise.all([
iterable[Symbol.asyncIterator]().next(),
await iterable[Symbol.asyncIterator]().next(),
]);
assert.equal(counter, 1);
});
});
Expand Down Expand Up @@ -293,5 +296,28 @@ suite("CachedAsyncIterable", function() {
await iterable.touchNext(),
{value: undefined, done: true});
});

test("touchNext can be called multiple times in parallel", async function() {
let counter = 0;

async function *generate() {
let value = 5;
while (value-- > 0) {
counter++;
yield await Promise.resolve(value);
}
}

// We're testing that if the first call to asyncIterator has been
// made, but the value of it has not been returned yet,
// the consequitive call returns the same Promise rather than,
// attempting to fetch the next item from the iterator.
const iterable = new CachedAsyncIterable(generate());
await Promise.all([
iterable.touchNext(2),
iterable[Symbol.asyncIterator]().next(),
]);
assert.equal(counter, 2);
});
});
});

0 comments on commit 1826f04

Please sign in to comment.