Skip to content

Commit 1e49f57

Browse files
Use getAll()
1 parent bf93a38 commit 1e49f57

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

packages/firestore/src/local/simple_db.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,28 @@ export class SimpleDbStore<
645645
indexOrRange?: string | IDBKeyRange,
646646
range?: IDBKeyRange
647647
): PersistencePromise<ValueType[]> {
648-
const cursor = this.cursor(this.options(indexOrRange, range));
649-
const results: ValueType[] = [];
650-
return this.iterateCursor(cursor, (key, value) => {
651-
results.push(value);
652-
}).next(() => {
653-
return results;
654-
});
648+
const iterateOptions = this.options(indexOrRange, range);
649+
// Use `getAll()` if the browser supports IndexedDB v3, as it is roughly
650+
// 20% faster. Unfortunately, getAll() does not support custom indices.
651+
if (!iterateOptions.index && typeof this.store.getAll === 'function') {
652+
const request = this.store.getAll(iterateOptions.range);
653+
return new PersistencePromise((resolve, reject) => {
654+
request.onerror = (event: Event) => {
655+
reject((event.target as IDBRequest).error!);
656+
};
657+
request.onsuccess = (event: Event) => {
658+
resolve((event.target as IDBRequest).result);
659+
};
660+
});
661+
} else {
662+
const cursor = this.cursor(iterateOptions);
663+
const results: ValueType[] = [];
664+
return this.iterateCursor(cursor, (key, value) => {
665+
results.push(value);
666+
}).next(() => {
667+
return results;
668+
});
669+
}
655670
}
656671

657672
deleteAll(): PersistencePromise<void>;

0 commit comments

Comments
 (0)