Skip to content

Commit f40565f

Browse files
Use getAll()
1 parent bf93a38 commit f40565f

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

packages/firestore/src/local/simple_db.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,29 @@ 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+
) {
653+
const request = this.store.getAll(iterateOptions.range);
654+
return new PersistencePromise((resolve, reject) => {
655+
request.onerror = (event: Event) => {
656+
reject((event.target as IDBRequest).error!);
657+
};
658+
request.onsuccess = (event: Event) => {
659+
resolve((event.target as IDBRequest).result);
660+
};
661+
});
662+
} else {
663+
const cursor = this.cursor(iterateOptions);
664+
const results: ValueType[] = [];
665+
return this.iterateCursor(cursor, (key, value) => {
666+
results.push(value);
667+
}).next(() => {
668+
return results;
669+
});
670+
}
655671
}
656672

657673
deleteAll(): PersistencePromise<void>;

0 commit comments

Comments
 (0)