Skip to content

Commit c1b9cf1

Browse files
Use getAll() when available (#5985)
1 parent eaa5170 commit c1b9cf1

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

.changeset/late-tables-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Some database operations now use `IndexedDB.getAll()` on browsers where support is availbe.

packages/firestore/src/local/simple_db.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,28 @@ export class SimpleDbStore<
659659
indexOrRange?: string | IDBKeyRange,
660660
range?: IDBKeyRange
661661
): PersistencePromise<ValueType[]> {
662-
const cursor = this.cursor(this.options(indexOrRange, range));
663-
const results: ValueType[] = [];
664-
return this.iterateCursor(cursor, (key, value) => {
665-
results.push(value);
666-
}).next(() => {
667-
return results;
668-
});
662+
const iterateOptions = this.options(indexOrRange, range);
663+
// Use `getAll()` if the browser supports IndexedDB v3, as it is roughly
664+
// 20% faster. Unfortunately, getAll() does not support custom indices.
665+
if (!iterateOptions.index && typeof this.store.getAll === 'function') {
666+
const request = this.store.getAll(iterateOptions.range);
667+
return new PersistencePromise((resolve, reject) => {
668+
request.onerror = (event: Event) => {
669+
reject((event.target as IDBRequest).error!);
670+
};
671+
request.onsuccess = (event: Event) => {
672+
resolve((event.target as IDBRequest).result);
673+
};
674+
});
675+
} else {
676+
const cursor = this.cursor(iterateOptions);
677+
const results: ValueType[] = [];
678+
return this.iterateCursor(cursor, (key, value) => {
679+
results.push(value);
680+
}).next(() => {
681+
return results;
682+
});
683+
}
669684
}
670685

671686
deleteAll(): PersistencePromise<void>;

0 commit comments

Comments
 (0)