Skip to content

Commit c44013e

Browse files
author
alxndrsn
committed
don't recurse
1 parent 56a17f4 commit c44013e

File tree

1 file changed

+14
-12
lines changed
  • packages/node_modules/pouchdb-adapter-indexeddb/src

1 file changed

+14
-12
lines changed

packages/node_modules/pouchdb-adapter-indexeddb/src/allDocs.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export default function (txn, metadata, opts, callback) {
210210
};
211211
}
212212

213-
function fetchResults() {
213+
async function fetchResults() {
214214
// There is a risk here with getting all results into memory - if they have multiple
215215
// revs, then we risk loading loads of extra data which is then discarded. This is
216216
// reduced by batching. This also loads unused data when include_docs is false.
@@ -219,10 +219,17 @@ export default function (txn, metadata, opts, callback) {
219219
// result size, and (2) not so big it's likely to cause issues.
220220
const batchSize = 100;
221221

222-
fetchNextBatch();
222+
let kr = keyRange;
223+
while (kr) {
224+
kr = await fetchNextBatch(keyRange);
225+
}
226+
if (descending) {
227+
results.reverse();
228+
}
229+
return txn.txn.commit();
223230

224-
function fetchNextBatch() {
225-
dbIndex.getAll(keyRange, batchSize).onsuccess = (e) => {
231+
function fetchNextBatch(kr) {
232+
dbIndex.getAll(kr, batchSize).onsuccess = (e) => {
226233
const batch = e.target.result;
227234
for (let i=0; i<batch.length; ++i) {
228235
const doc = batch[i];
@@ -235,19 +242,14 @@ export default function (txn, metadata, opts, callback) {
235242

236243
if (batch.length >= batchSize) {
237244
const lastSeenKey = [ 0, batch[batch.length-1].id ];
238-
const startKey = descending ? keyRange.upper : lastSeenKey;
239-
const endKey = descending ? lastSeenKey : keyRange.upper;
245+
const startKey = descending ? kr.upper : lastSeenKey;
246+
const endKey = descending ? lastSeenKey : kr.upper;
240247
if (startKey[1] !== endKey[1]) {
241248
const incEnd = descending ? false : inclusiveEnd;
242249
const incStart = descending ? true : false;
243-
keyRange = createKeyRange(startKey, endKey, incStart, incEnd, key, descending);
244-
return fetchNextBatch();
250+
return createKeyRange(startKey, endKey, incStart, incEnd, key, descending);
245251
}
246252
}
247-
if (descending) {
248-
results.reverse();
249-
}
250-
return txn.txn.commit();
251253
};
252254
}
253255
}

0 commit comments

Comments
 (0)