Skip to content

Commit b4f52d9

Browse files
author
Bobby
authored
fix: pagination and optimistic write creation (#22)
fix: pagination and optimistic write creation
2 parents 932959b + 30c3b43 commit b4f52d9

File tree

7 files changed

+231
-123
lines changed

7 files changed

+231
-123
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-firestore",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Redux bindings for Firestore.",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/actions/firestore.js

+4
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,12 @@ export function runTransaction(firebase, dispatch, transactionPromise) {
416416
* @returns {Promise} Resolves with results of update call
417417
*/
418418
export function mutate(firebase, dispatch, writes) {
419+
const timestamp = `${+new Date()}`;
420+
419421
return wrapInDispatch(dispatch, {
420422
ref: firebase,
421423
method: 'mutate',
424+
meta: { timestamp },
422425
args: [writes],
423426
types: [
424427
{
@@ -428,6 +431,7 @@ export function mutate(firebase, dispatch, writes) {
428431
actionTypes.MUTATE_SUCCESS,
429432
{
430433
type: actionTypes.MUTATE_FAILURE,
434+
meta: { timestamp },
431435
payload: { data: writes },
432436
},
433437
],

src/createFirestoreInstance.js

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ export default function createFirestoreInstance(firebase, configs, dispatch) {
102102
* dispatch({ type: 'SOME_ACTION' })
103103
* })
104104
* };
105-
*
106105
*/
107106
export function getFirestore() {
108107
/* istanbul ignore next: Firestore instance always exists during tests */

src/reducers/cacheReducer.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ const xfAllIds = ({ collection: path }) =>
164164

165165
/**
166166
* @name xfWhere
167+
* @param getDoc.where
168+
* @param getDoc
167169
* @param {Array.<Array.<string>>} where - Firestore where clauses
168170
* @property {object.<FirestorePath, object<FirestoreDocumentId, Doc>>} db
169171
* @property {object.<FirestorePath, object<FirestoreDocumentId, ParitalDoc>>} dbo
@@ -206,6 +208,8 @@ const xfWhere = ({ where }, getDoc) => {
206208

207209
/**
208210
* @name xfOrder
211+
* @param getDoc.orderBy
212+
* @param getDoc
209213
* @param {Array.<string>} order - Firestore order property
210214
* @property {object.<FirestorePath, object<FirestoreDocumentId, Doc>>} db
211215
* @property {object.<FirestorePath, object<FirestoreDocumentId, ParitalDoc>>} dbo
@@ -234,11 +238,9 @@ const xfOrder = ({ orderBy: order }, getDoc) => {
234238
// TODO: refactor to manually lookup and compare
235239
const docs = tuples.map(([path, id]) => getDoc(path, id));
236240

237-
const result = orderBy(docs, fields, direction).map(
241+
return orderBy(docs, fields, direction).map(
238242
({ id, path } = {}) => path && id && [path, id],
239243
);
240-
241-
return result;
242244
});
243245
};
244246

@@ -262,7 +264,8 @@ const xfLimit = ({ limit, endAt, endBefore }) => {
262264
* @param {?CacheState.database} db -
263265
* @param {?CacheState.databaseOverrides} dbo -
264266
* @param {RRFQuery} query - Firestore query
265-
* @param {Boolean} isOptimisticWrite - includes optimistic data
267+
* @param getDoc
268+
* @param {boolean} isOptimisticWrite - includes optimistic data
266269
* @typedef {Function} xFormFilter - in optimistic reads and overrides
267270
* the reducer needs to take all documents and make a best effort to
268271
* filter down the document based on a cursor.
@@ -276,8 +279,9 @@ const xfPaginate = (query, getDoc) => {
276279
const end = endAt || endBefore;
277280
const isAfter = startAfter !== undefined;
278281
const isBefore = endBefore !== undefined;
282+
const needsPagination = start || end || false;
279283

280-
if (!order || !isOptimisticRead || !!start || !!end) return identity;
284+
if (!needsPagination || !order || !isOptimisticRead) return identity;
281285

282286
const isFlat = typeof order[0] === 'string';
283287
const orders = isFlat ? [order] : order;
@@ -329,6 +333,7 @@ const xfPaginate = (query, getDoc) => {
329333
* @name processOptimistic
330334
* Convert the query to a transducer for the query results
331335
* @param {?CacheState.database} database -
336+
* @param state
332337
* @param {?CacheState.databaseOverrides} overrides -
333338
* @param {RRFQuery} query - query used to get data from firestore
334339
* @returns {Function} - Transducer will return a modifed array of documents
@@ -340,9 +345,9 @@ function processOptimistic(query, state) {
340345
const dbo = databaseOverrides && databaseOverrides[collection];
341346

342347
const getDoc = (path, id) => {
343-
if (path !== collection) console.log('-----', path, collection);
344348
const data = db[id] || {};
345349
const override = dbo?.[id];
350+
346351
return override ? { ...data, ...override } : data;
347352
};
348353

@@ -530,7 +535,7 @@ function translateMutationToOverrides({ payload }, db = {}, dbo = {}) {
530535
const overrides = dbo[path] || {};
531536
return {
532537
...result,
533-
[key]: { ...collection[id], ...(overrides[id] || {}) },
538+
[key]: { id, path, ...collection[id], ...(overrides[id] || {}) },
534539
};
535540
}, {});
536541
}
@@ -830,13 +835,21 @@ const mutation = (state, { action, key, path }) => {
830835
try {
831836
const result = produce(state, (draft) => {
832837
const done = mark(`cache.MUTATE_START`, key);
838+
const {
839+
meta: { timestamp },
840+
} = action;
833841
if (action.payload && action.payload.data) {
834842
const optimisiticUpdates =
835843
translateMutationToOverrides(action, draft.database) || [];
836844

837-
optimisiticUpdates.forEach(({ path: _path, id, ...data }) => {
838-
info('overriding', `${_path}/${id}`, data);
839-
setWith(draft, ['databaseOverrides', _path, id], data, Object);
845+
optimisiticUpdates.forEach((data) => {
846+
info('overriding', `${data.path}/${data.id}`, data);
847+
setWith(
848+
draft,
849+
['databaseOverrides', data.path, data.id],
850+
data,
851+
Object,
852+
);
840853
});
841854

842855
const updatePaths = [

src/reducers/statusReducer.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ import { actionTypes } from '../constants';
22
import { getSlashStrPath, combineReducers } from '../utils/reducers';
33
import { getQueryName } from '../utils/query';
44

5-
const {
6-
SET_LISTENER,
7-
UNSET_LISTENER,
8-
LISTENER_ERROR,
9-
LISTENER_RESPONSE,
10-
} = actionTypes;
5+
const { SET_LISTENER, UNSET_LISTENER, LISTENER_ERROR, LISTENER_RESPONSE } =
6+
actionTypes;
117

128
/**
139
* Reducer for requesting state.Changed by `START`, `NO_VALUE`, and `SET` actions.

0 commit comments

Comments
 (0)