Skip to content

Commit 6338152

Browse files
author
Bobby
committed
fix: removed dupe sometimes from an add_doc action
1 parent 8d3c159 commit 6338152

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

src/reducers/cacheReducer.js

+34-29
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ function selectDocuments(reducerState, query) {
340340
*/
341341
function reprocessQuerires(draft, path) {
342342
const done = mark(`reprocess-${path}`);
343+
const queries = [];
343344

344-
info(`reprocess queries for ${path}`);
345345
const paths = Array.isArray(path) ? path : [path];
346346
Object.keys(draft).forEach((key) => {
347347
if (['database', 'databaseOverrides'].includes(key)) return;
@@ -351,12 +351,19 @@ function reprocessQuerires(draft, path) {
351351
if (!collections.some((coll) => paths.includes(coll))) {
352352
return;
353353
}
354+
queries.push(key);
355+
354356
const docs = selectDocuments(draft, draft[key]);
355357
const ordered = docs.map(({ id, path: _path }) => [_path, id]);
356358
set(draft, [key, 'docs'], docs);
357359
set(draft, [key, 'ordered'], ordered);
358360
});
359361

362+
if (info.enabled) {
363+
const overrides = JSON.parse(JSON.stringify(draft.databaseOverrides || {}));
364+
info(`reprocess ${path} (${q.length} queries) with overrides`, overrides);
365+
}
366+
360367
done();
361368
}
362369

@@ -553,7 +560,7 @@ const initialize = (state, { action, key, path }) =>
553560

554561
const conclude = (state, { action, key, path }) =>
555562
produce(state, (draft) => {
556-
const unsetDone = mark(`cache.UNSET_LISTENER`);
563+
const done = mark(`cache.UNSET_LISTENER`);
557564
if (draft[key]) {
558565
// all ids for the collection type, except query to be unset
559566
const activeIds = Object.keys(draft).reduce((inUse, dbKey) => {
@@ -578,13 +585,13 @@ const conclude = (state, { action, key, path }) =>
578585
reprocessQuerires(draft, path);
579586
}
580587

581-
unsetDone();
588+
done();
582589
return draft;
583590
});
584591

585592
const modify = (state, { action, key, path }) =>
586593
produce(state, (draft) => {
587-
const addDone = mark(`cache.DOCUMENT_MODIFIED`);
594+
const done = mark(`cache.DOCUMENT_MODIFIED`);
588595
setWith(
589596
draft,
590597
['database', path, action.meta.doc],
@@ -594,34 +601,24 @@ const modify = (state, { action, key, path }) =>
594601

595602
const remove = shouldRemoveOverride(action, draft.databaseOverrides);
596603
if (remove) {
597-
unset(draft, ['databaseOverrides', path, action.meta.doc]);
598-
}
599-
600-
const { payload } = action;
601-
const { oldIndex = 0, newIndex = 0 } = payload.ordered || {};
602-
if (newIndex !== oldIndex) {
603-
const tuple =
604-
(payload.data && [payload.data.path, payload.data.id]) ||
605-
draft[key].ordered[oldIndex];
606-
607-
const { ordered } = draft[key] || { ordered: [] };
608-
if (oldIndex !== -1) {
609-
ordered.splice(oldIndex, 0);
604+
if (Object.keys(draft.databaseOverrides[path]).length > 1) {
605+
unset(draft, ['databaseOverrides', path, action.meta.doc]);
606+
} else {
607+
unset(draft, ['databaseOverrides', path]);
610608
}
611-
ordered.splice(newIndex, 0, tuple);
612-
613-
set(draft, [key, 'ordered'], ordered);
614609
}
615610

611+
// reprocessing unifies any order changes from firestore
612+
616613
reprocessQuerires(draft, path);
617614

618-
addDone();
615+
done();
619616
return draft;
620617
});
621618

622619
const failure = (state, { action, key, path }) =>
623620
produce(state, (draft) => {
624-
const failureDone = mark(`cache.MUTATE_FAILURE`);
621+
const done = mark(`cache.MUTATE_FAILURE`);
625622
// All failures remove overrides
626623
if (action.payload.data || action.payload.args) {
627624
const write = action.payload.data
@@ -649,7 +646,7 @@ const failure = (state, { action, key, path }) =>
649646
}
650647
}
651648

652-
failureDone();
649+
done();
653650
return draft;
654651
});
655652

@@ -661,7 +658,11 @@ const deletion = (state, { action, key, path }) =>
661658
}
662659

663660
if (draft.databaseOverrides && draft.databaseOverrides[path]) {
664-
unset(draft, ['databaseOverrides', path, action.meta.doc]);
661+
if (Object.keys(draft.databaseOverrides[path]).length > 1) {
662+
unset(draft, ['databaseOverrides', path, action.meta.doc]);
663+
} else {
664+
unset(draft, ['databaseOverrides', path]);
665+
}
665666
}
666667

667668
// remove document id from ordered index
@@ -679,7 +680,7 @@ const deletion = (state, { action, key, path }) =>
679680

680681
const remove = (state, { action, key, path }) =>
681682
produce(state, (draft) => {
682-
const removeDone = mark(`cache.DOCUMENT_REMOVED`);
683+
const done = mark(`cache.DOCUMENT_REMOVED`);
683684
if (draft.databaseOverrides && draft.databaseOverrides[path]) {
684685
unset(draft, ['databaseOverrides', path, action.meta.doc]);
685686
}
@@ -696,7 +697,7 @@ const remove = (state, { action, key, path }) =>
696697
// reprocess
697698
reprocessQuerires(draft, path);
698699

699-
removeDone();
700+
done();
700701
return draft;
701702
});
702703

@@ -715,14 +716,18 @@ const optimistic = (state, { action, key, path }) =>
715716

716717
const reset = (state, { action, key, path }) =>
717718
produce(state, (draft) => {
718-
unset(draft, ['databaseOverrides', path, action.meta.doc]);
719+
if (Object.keys(draft.databaseOverrides[path]).length > 1) {
720+
unset(draft, ['databaseOverrides', path, action.meta.doc]);
721+
} else {
722+
unset(draft, ['databaseOverrides', path]);
723+
}
719724
reprocessQuerires(draft, path);
720725
return draft;
721726
});
722727

723728
const mutation = (state, { action, key, path }) =>
724729
produce(state, (draft) => {
725-
const startDone = mark(`cache.MUTATE_START`);
730+
const done = mark(`cache.MUTATE_START`);
726731
if (action.payload && action.payload.data) {
727732
const optimisiticUpdates =
728733
translateMutationToOverrides(action, draft.database) || [];
@@ -740,7 +745,7 @@ const mutation = (state, { action, key, path }) =>
740745
});
741746
}
742747

743-
startDone();
748+
done();
744749
return draft;
745750
});
746751

src/utils/profiling.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const perf = win && win.performance;
2222
* @returns {Function}
2323
*/
2424
export default function mark(marker) {
25-
if (!debug.enabled('rrf') || !perf) return noop;
25+
if (!debug.enabled('rrf:*') || !debug.enabled('rrf:profile') || !perf) {
26+
return noop;
27+
}
2628

2729
try {
2830
const now = perf.now();

test/unit/reducers/cacheReducer.spec.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe('cacheReducer', () => {
213213
describe('LISTENER_RESPONSE', () => {
214214
it('override a document modification synchronously', () => {
215215
const doc1 = { key1: 'value1', id: 'testDocId1', path }; // initial doc
216-
const doc2 = { key2: 'value2', id: 'testDocId1', path }; // added doc
216+
const doc2 = { key2: null, id: 'testDocId1', path }; // added doc
217217

218218
// Initial seed
219219
const action1 = {
@@ -237,7 +237,8 @@ describe('cacheReducer', () => {
237237

238238
const pass1 = reducer(initialState, action1);
239239
const pass2 = reducer(pass1, action2);
240-
240+
console.log(pass2.cache.databaseOverrides);
241+
console.log(pass2.cache.testStoreAs.docs[0]);
241242
expect(pass1.cache.testStoreAs.docs[0]).to.eql(doc1);
242243
expect(pass2.cache.testStoreAs.docs[0]).to.eql({ ...doc1, ...doc2 });
243244
});
@@ -635,7 +636,7 @@ describe('cacheReducer', () => {
635636
expect(pass2.cache.databaseOverrides[collection]).to.eql({
636637
[change.id]: change,
637638
});
638-
expect(pass3.cache.databaseOverrides[collection]).to.eql({});
639+
expect(pass3.cache.databaseOverrides).to.eql({});
639640
});
640641
});
641642

0 commit comments

Comments
 (0)