Skip to content

Commit dc13ee4

Browse files
committed
Properly honor the async option (or lack thereof) in iteration to maintain back-compat for sync iteration
1 parent 6509388 commit dc13ee4

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

resources/Table.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,11 +2239,12 @@ export function makeTable(options) {
22392239
entries = transformToEntries(entries, select, context, readTxn, null);
22402240
let ordered;
22412241
// if we are doing post-ordering, we need to get records first, then sort them
2242-
results.iterate = function () {
2242+
results.iterate = function (options: { async: boolean }) {
22432243
let sortedArrayIterator: IterableIterator<any>;
2244-
const dbIterator = entries[Symbol.asyncIterator]
2245-
? entries[Symbol.asyncIterator]()
2246-
: entries[Symbol.iterator]();
2244+
const dbIterator =
2245+
options?.async && entries[Symbol.asyncIterator]
2246+
? entries[Symbol.asyncIterator]()
2247+
: entries[Symbol.iterator]();
22472248
let dbDone: boolean;
22482249
const dbOrderedAttribute = sort.dbOrderedAttribute;
22492250
let enqueuedEntryForNextGroup: any;
@@ -2357,7 +2358,10 @@ export function makeTable(options) {
23572358
};
23582359
applySortingOnSelect(sort);
23592360
} else {
2360-
results.iterate = (entries[Symbol.asyncIterator] || entries[Symbol.iterator]).bind(entries);
2361+
results.iterate = (options: { async: boolean }) => {
2362+
if (options?.async && entries[Symbol.asyncIterator]) return entries[Symbol.asyncIterator]();
2363+
else return entries[Symbol.iterator]();
2364+
};
23612365
results = results.map(function (entry) {
23622366
try {
23632367
// because this is a part of a stream of results, we will often be continuing to iterate over the results when there are errors,

unitTests/resources/query.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ describe('Querying through Resource API', () => {
199199
}
200200
assert.equal(results.length, 1);
201201
});
202+
it('Ensure that we are yielding when querying data in a table', async function () {
203+
let results = [];
204+
let yielded = false;
205+
setImmediate(() => {
206+
yielded = true;
207+
});
208+
for await (let record of QueryTable.search({
209+
conditions: [{ attribute: 'id', comparator: 'less_than_equal', value: 'id-3' }],
210+
})) {
211+
results.push(record);
212+
}
213+
assert(yielded);
214+
assert.equal(results.length, 24);
215+
});
202216
it('Query data in a table with non-indexed property', async function () {
203217
let results = [];
204218
for await (let record of QueryTable.search({
@@ -342,7 +356,8 @@ describe('Querying through Resource API', () => {
342356

343357
it('Query relational data in a table with many-to-one', async function () {
344358
let results = [];
345-
for await (let record of QueryTable.search({
359+
// do this test with sync iteration to verify it is possible
360+
for (let record of QueryTable.search({
346361
conditions: [{ attribute: 'id', value: 'id-1' }],
347362
select: ['id', 'related', 'name'],
348363
})) {

0 commit comments

Comments
 (0)