Skip to content

Commit b329d69

Browse files
committed
Add [Symbol.iterator] to CachedAsyncIterable
The [Symbol.iterator] method returns a synchronous iterator over the elements cached by the CachedAsyncIterable instance.
1 parent 1ba84c6 commit b329d69

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/cached_async_iterable.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ export default class CachedAsyncIterable {
2323
this.seen = [];
2424
}
2525

26+
/**
27+
* Synchronous iterator over the cached elements.
28+
*
29+
* Return a generator object implementing the iterator protocol over the
30+
* cached elements of the original (async or sync) iterable.
31+
*/
32+
[Symbol.iterator]() {
33+
const {seen} = this;
34+
let cur = 0;
35+
36+
return {
37+
next() {
38+
if (seen.length === cur) {
39+
return {value: undefined, done: true};
40+
}
41+
return seen[cur++];
42+
}
43+
};
44+
}
45+
46+
/**
47+
* Asynchronous iterator caching the yielded elements.
48+
*
49+
* Elements yielded by the original iterable will be cached and available
50+
* synchronously. Returns an async generator object implementing the
51+
* iterator protocol over the elements of the original (async or sync)
52+
* iterable.
53+
*/
2654
[Symbol.asyncIterator]() {
2755
const { seen, iterator } = this;
2856
let cur = 0;

test/cached_async_iterable_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,75 @@ suite("CachedAsyncIterable", function() {
5555
});
5656
});
5757

58+
suite("sync iteration over cached elements", function(){
59+
let o1, o2;
60+
61+
suiteSetup(function() {
62+
o1 = Object();
63+
o2 = Object();
64+
});
65+
66+
test("sync iterable with no cached elements yet", function() {
67+
function *generate() {
68+
yield *[o1, o2];
69+
}
70+
71+
const iterable = new CachedAsyncIterable(generate());
72+
assert.deepEqual([...iterable], []);
73+
});
74+
75+
test("sync iterable with a few elements cached so far", async function() {
76+
function *generate() {
77+
yield *[o1, o2];
78+
}
79+
80+
const iterable = new CachedAsyncIterable(generate());
81+
await iterable.touchNext();
82+
assert.deepEqual([...iterable], [o1]);
83+
});
84+
85+
test("iterable with all cached elements", async function() {
86+
function *generate() {
87+
yield *[o1, o2];
88+
}
89+
90+
const iterable = new CachedAsyncIterable(generate());
91+
await iterable.touchNext();
92+
await iterable.touchNext();
93+
assert.deepEqual([...iterable], [o1, o2]);
94+
});
95+
96+
test("async iterable with no cached elements yet", async function() {
97+
async function *generate() {
98+
yield *[o1, o2];
99+
}
100+
101+
const iterable = new CachedAsyncIterable(generate());
102+
assert.deepEqual([...iterable], []);
103+
});
104+
105+
test("async iterable with a few elements cached so far", async function() {
106+
async function *generate() {
107+
yield *[o1, o2];
108+
}
109+
110+
const iterable = new CachedAsyncIterable(generate());
111+
await iterable.touchNext();
112+
assert.deepEqual([...iterable], [o1]);
113+
});
114+
115+
test("async iterable with all cached elements", async function() {
116+
async function *generate() {
117+
yield *[o1, o2];
118+
}
119+
120+
const iterable = new CachedAsyncIterable(generate());
121+
await iterable.touchNext();
122+
await iterable.touchNext();
123+
assert.deepEqual([...iterable], [o1, o2]);
124+
});
125+
});
126+
58127
suite("async iteration", function(){
59128
let o1, o2;
60129

0 commit comments

Comments
 (0)