Skip to content

Commit

Permalink
Use getAll() when available (#5985)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian authored Feb 10, 2022
1 parent eaa5170 commit c1b9cf1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-tables-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Some database operations now use `IndexedDB.getAll()` on browsers where support is availbe.
29 changes: 22 additions & 7 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,13 +659,28 @@ export class SimpleDbStore<
indexOrRange?: string | IDBKeyRange,
range?: IDBKeyRange
): PersistencePromise<ValueType[]> {
const cursor = this.cursor(this.options(indexOrRange, range));
const results: ValueType[] = [];
return this.iterateCursor(cursor, (key, value) => {
results.push(value);
}).next(() => {
return results;
});
const iterateOptions = this.options(indexOrRange, range);
// Use `getAll()` if the browser supports IndexedDB v3, as it is roughly
// 20% faster. Unfortunately, getAll() does not support custom indices.
if (!iterateOptions.index && typeof this.store.getAll === 'function') {
const request = this.store.getAll(iterateOptions.range);
return new PersistencePromise((resolve, reject) => {
request.onerror = (event: Event) => {
reject((event.target as IDBRequest).error!);
};
request.onsuccess = (event: Event) => {
resolve((event.target as IDBRequest).result);
};
});
} else {
const cursor = this.cursor(iterateOptions);
const results: ValueType[] = [];
return this.iterateCursor(cursor, (key, value) => {
results.push(value);
}).next(() => {
return results;
});
}
}

deleteAll(): PersistencePromise<void>;
Expand Down

0 comments on commit c1b9cf1

Please sign in to comment.