Skip to content

Commit b5eedbb

Browse files
committed
Make CachedIterable a subclass of Array
1 parent ea08231 commit b5eedbb

5 files changed

+32
-44
lines changed

src/cached_async_iterable.mjs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ export default class CachedAsyncIterable extends CachedIterable {
3232
* cached elements of the original (async or sync) iterable.
3333
*/
3434
[Symbol.iterator]() {
35-
const {seen} = this;
35+
const cached = this;
3636
let cur = 0;
3737

3838
return {
3939
next() {
40-
if (seen.length === cur) {
40+
if (cached.length === cur) {
4141
return {value: undefined, done: true};
4242
}
43-
return seen[cur++];
43+
return cached[cur++];
4444
}
4545
};
4646
}
@@ -54,15 +54,15 @@ export default class CachedAsyncIterable extends CachedIterable {
5454
* iterable.
5555
*/
5656
[Symbol.asyncIterator]() {
57-
const { seen, iterator } = this;
57+
const cached = this;
5858
let cur = 0;
5959

6060
return {
6161
async next() {
62-
if (seen.length <= cur) {
63-
seen.push(await iterator.next());
62+
if (cached.length <= cur) {
63+
cached.push(await cached.iterator.next());
6464
}
65-
return seen[cur++];
65+
return cached[cur++];
6666
}
6767
};
6868
}
@@ -74,15 +74,14 @@ export default class CachedAsyncIterable extends CachedIterable {
7474
* @param {number} count - number of elements to consume
7575
*/
7676
async touchNext(count = 1) {
77-
const { seen, iterator } = this;
7877
let idx = 0;
7978
while (idx++ < count) {
80-
if (seen.length === 0 || seen[seen.length - 1].done === false) {
81-
seen.push(await iterator.next());
79+
if (this.length === 0 || this[this.length - 1].done === false) {
80+
this.push(await this.iterator.next());
8281
}
8382
}
8483
// Return the last cached {value, done} object to allow the calling
8584
// code to decide if it needs to call touchNext again.
86-
return seen[seen.length - 1];
85+
return this[this.length - 1];
8786
}
8887
}

src/cached_iterable.mjs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
22
* Base CachedIterable class.
33
*/
4-
export default class CachedIterable {
5-
/**
6-
* Create an `CachedIterable` instance.
7-
*
8-
* @param {Iterable} iterable
9-
* @returns {CachedIterable}
10-
*/
11-
constructor() {
12-
this.seen = [];
13-
}
14-
4+
export default class CachedIterable extends Array {
155
/**
166
* Create a `CachedIterable` instance from an iterable or, if another
177
* instance of `CachedIterable` is passed, return it without any

src/cached_sync_iterable.mjs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ export default class CachedSyncIterable extends CachedIterable {
2424
}
2525

2626
[Symbol.iterator]() {
27-
const { seen, iterator } = this;
27+
const cached = this;
2828
let cur = 0;
2929

3030
return {
3131
next() {
32-
if (seen.length <= cur) {
33-
seen.push(iterator.next());
32+
if (cached.length <= cur) {
33+
cached.push(cached.iterator.next());
3434
}
35-
return seen[cur++];
35+
return cached[cur++];
3636
}
3737
};
3838
}
@@ -44,15 +44,14 @@ export default class CachedSyncIterable extends CachedIterable {
4444
* @param {number} count - number of elements to consume
4545
*/
4646
touchNext(count = 1) {
47-
const { seen, iterator } = this;
4847
let idx = 0;
4948
while (idx++ < count) {
50-
if (seen.length === 0 || seen[seen.length - 1].done === false) {
51-
seen.push(iterator.next());
49+
if (this.length === 0 || this[this.length - 1].done === false) {
50+
this.push(this.iterator.next());
5251
}
5352
}
5453
// Return the last cached {value, done} object to allow the calling
5554
// code to decide if it needs to call touchNext again.
56-
return seen[seen.length - 1];
55+
return this[this.length - 1];
5756
}
5857
}

test/cached_async_iterable_test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,33 +186,33 @@ suite("CachedAsyncIterable", function() {
186186

187187
test("consumes an element into the cache", async function() {
188188
const iterable = new CachedAsyncIterable(generateMessages());
189-
assert.equal(iterable.seen.length, 0);
189+
assert.equal(iterable.length, 0);
190190
await iterable.touchNext();
191-
assert.equal(iterable.seen.length, 1);
191+
assert.equal(iterable.length, 1);
192192
});
193193

194194
test("allows to consume multiple elements into the cache", async function() {
195195
const iterable = new CachedAsyncIterable(generateMessages());
196196
await iterable.touchNext();
197197
await iterable.touchNext();
198-
assert.equal(iterable.seen.length, 2);
198+
assert.equal(iterable.length, 2);
199199
});
200200

201201
test("allows to consume multiple elements at once", async function() {
202202
const iterable = new CachedAsyncIterable(generateMessages());
203203
await iterable.touchNext(2);
204-
assert.equal(iterable.seen.length, 2);
204+
assert.equal(iterable.length, 2);
205205
});
206206

207207
test("stops at the last element", async function() {
208208
const iterable = new CachedAsyncIterable(generateMessages());
209209
await iterable.touchNext();
210210
await iterable.touchNext();
211211
await iterable.touchNext();
212-
assert.equal(iterable.seen.length, 3);
212+
assert.equal(iterable.length, 3);
213213

214214
await iterable.touchNext();
215-
assert.equal(iterable.seen.length, 3);
215+
assert.equal(iterable.length, 3);
216216
});
217217

218218
test("works on an empty iterable", async function() {
@@ -223,7 +223,7 @@ suite("CachedAsyncIterable", function() {
223223
await iterable.touchNext();
224224
await iterable.touchNext();
225225
await iterable.touchNext();
226-
assert.equal(iterable.seen.length, 1);
226+
assert.equal(iterable.length, 1);
227227
});
228228

229229
test("iteration for such cache works", async function() {

test/cached_sync_iterable_test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,41 +106,41 @@ suite("CachedSyncIterable", function() {
106106

107107
test("consumes an element into the cache", function() {
108108
const iterable = new CachedSyncIterable([o1, o2]);
109-
assert.equal(iterable.seen.length, 0);
109+
assert.equal(iterable.length, 0);
110110
iterable.touchNext();
111-
assert.equal(iterable.seen.length, 1);
111+
assert.equal(iterable.length, 1);
112112
});
113113

114114
test("allows to consume multiple elements into the cache", function() {
115115
const iterable = new CachedSyncIterable([o1, o2]);
116116
iterable.touchNext();
117117
iterable.touchNext();
118-
assert.equal(iterable.seen.length, 2);
118+
assert.equal(iterable.length, 2);
119119
});
120120

121121
test("allows to consume multiple elements at once", function() {
122122
const iterable = new CachedSyncIterable([o1, o2]);
123123
iterable.touchNext(2);
124-
assert.equal(iterable.seen.length, 2);
124+
assert.equal(iterable.length, 2);
125125
});
126126

127127
test("stops at the last element", function() {
128128
const iterable = new CachedSyncIterable([o1, o2]);
129129
iterable.touchNext();
130130
iterable.touchNext();
131131
iterable.touchNext();
132-
assert.equal(iterable.seen.length, 3);
132+
assert.equal(iterable.length, 3);
133133

134134
iterable.touchNext();
135-
assert.equal(iterable.seen.length, 3);
135+
assert.equal(iterable.length, 3);
136136
});
137137

138138
test("works on an empty iterable", function() {
139139
const iterable = new CachedSyncIterable([]);
140140
iterable.touchNext();
141141
iterable.touchNext();
142142
iterable.touchNext();
143-
assert.equal(iterable.seen.length, 1);
143+
assert.equal(iterable.length, 1);
144144
});
145145

146146
test("iteration for such cache works", function() {

0 commit comments

Comments
 (0)