@@ -210,7 +210,7 @@ export default function (txn, metadata, opts, callback) {
210
210
} ;
211
211
}
212
212
213
- function fetchResults ( ) {
213
+ async function fetchResults ( ) {
214
214
// There is a risk here with getting all results into memory - if they have multiple
215
215
// revs, then we risk loading loads of extra data which is then discarded. This is
216
216
// reduced by batching. This also loads unused data when include_docs is false.
@@ -219,10 +219,17 @@ export default function (txn, metadata, opts, callback) {
219
219
// result size, and (2) not so big it's likely to cause issues.
220
220
const batchSize = 100 ;
221
221
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 ( ) ;
223
230
224
- function fetchNextBatch ( ) {
225
- dbIndex . getAll ( keyRange , batchSize ) . onsuccess = ( e ) => {
231
+ function fetchNextBatch ( kr ) {
232
+ dbIndex . getAll ( kr , batchSize ) . onsuccess = ( e ) => {
226
233
const batch = e . target . result ;
227
234
for ( let i = 0 ; i < batch . length ; ++ i ) {
228
235
const doc = batch [ i ] ;
@@ -235,19 +242,14 @@ export default function (txn, metadata, opts, callback) {
235
242
236
243
if ( batch . length >= batchSize ) {
237
244
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 ;
240
247
if ( startKey [ 1 ] !== endKey [ 1 ] ) {
241
248
const incEnd = descending ? false : inclusiveEnd ;
242
249
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 ) ;
245
251
}
246
252
}
247
- if ( descending ) {
248
- results . reverse ( ) ;
249
- }
250
- return txn . txn . commit ( ) ;
251
253
} ;
252
254
}
253
255
}
0 commit comments