Skip to content

Use getAll() when available #5985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -645,13 +645,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