Skip to content

Commit

Permalink
feat: support read queries for optimistic updates
Browse files Browse the repository at this point in the history
feat: support read queries for optimistic updates
  • Loading branch information
Bobby authored Dec 13, 2021
2 parents 97d2db4 + 3056242 commit 91f3fb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-firestore",
"version": "1.1.2",
"version": "1.1.5",
"description": "Redux bindings for Firestore.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
19 changes: 19 additions & 0 deletions src/reducers/cacheReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ const formatTimestamp = ({ seconds } = {}) =>
new Intl.DateTimeFormat('en-US', { dateStyle: 'short' }).format(
new Date(seconds * 1000),
);
const isDocRead = ({ doc, id } = {}) =>
typeof id === 'string' || typeof doc === 'string';

const PROCESSES = {
'<': (a, b) => a < b,
Expand Down Expand Up @@ -563,6 +565,23 @@ function translateMutationToOverrides({ payload }, db = {}, dbo = {}) {

const collection = db[path] || {};
const overrides = dbo[path] || {};

if (!isDocRead(reads[key])) {
const pathIds = processOptimistic(reads[key], {
database: db,
databaseOverrides: dbo,
});
return {
...result,
[key]: pathIds.map(([path, id]) => ({
id,
path,
...collection[id],
...(overrides[id] || {}),
})),
};
}

return {
...result,
[key]: { id, path, ...collection[id], ...(overrides[id] || {}) },
Expand Down
4 changes: 3 additions & 1 deletion src/utils/mutate.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ async function writeInTransaction(firebase, operations) {
const coll = firestoreRef(firebase, read);
const snapshot = await coll.get();
if (hasNothing(snapshot) || snapshot.docs.length === 0) return [];
const unserializedDocs = await Promise.all(snapshot.docs.map(getter));
const unserializedDocs = await Promise.all(
snapshot.docs.map(({ ref }) => getter(ref)),
);
return unserializedDocs.map(serialize);
});

Expand Down

0 comments on commit 91f3fb9

Please sign in to comment.