Skip to content

Commit 4b1a27f

Browse files
author
Bobby
authored
feat: returns undefined on initial read when in-memory db is empty
feat: returns undefined on initial read when in-memory db is empty
2 parents 86da9e1 + f56439b commit 4b1a27f

File tree

5 files changed

+229
-39
lines changed

5 files changed

+229
-39
lines changed

src/actions/firestore.js

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export function get(firebase, dispatch, queryOption) {
9797
payload: (snap) => ({
9898
data: dataByIdSnapshot(snap),
9999
ordered: orderedFromSnap(snap),
100+
fromCache:
101+
typeof snap.metadata?.fromCache === 'boolean'
102+
? snap.metadata.fromCache
103+
: true,
100104
}),
101105
merge: {
102106
docs: mergeOrdered && mergeOrderedDocUpdates,

src/reducers/cacheReducer.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const PROCESSES = {
105105
'>=': (a, b) => a >= b,
106106
'>': (a, b) => a > b,
107107
'array-contains': (a, b) => a.includes(b),
108-
in: (a, b) => a && a.includes(b),
108+
in: (a, b) => a && b && b.includes(a),
109109
'array-contains-any': (a, b) => b.some((b1) => a.includes(b1)),
110110
'not-in': (a, b) => !b.includes(a),
111111
'*': () => true,
@@ -369,7 +369,8 @@ function reprocessQuerires(draft, path) {
369369

370370
const docs = selectDocuments(draft, draft[key]);
371371
const ordered = docs.map(({ id, path: _path }) => [_path, id]);
372-
set(draft, [key, 'docs'], docs);
372+
const isInitialLoad = draft[key].via === 'memory' && docs.length === 0;
373+
set(draft, [key, 'docs'], isInitialLoad ? undefined : docs);
373374
set(draft, [key, 'ordered'], ordered);
374375
});
375376

@@ -557,6 +558,12 @@ const initialize = (state, { action, key, path }) =>
557558
set(draft, ['databaseOverrides'], {});
558559
}
559560

561+
const via = {
562+
undefined: 'memory',
563+
true: 'cache',
564+
false: 'server',
565+
}[action.payload.fromCache];
566+
560567
if (action.payload.data) {
561568
Object.keys(action.payload.data).forEach((id) => {
562569
setWith(draft, ['database', path, id], action.payload.data[id], Object);
@@ -569,9 +576,11 @@ const initialize = (state, { action, key, path }) =>
569576
const ordered = (
570577
action.payload.ordered || selectDocuments(draft, action.meta)
571578
).map(({ path, id }) => [path, id]);
579+
572580
set(draft, [action.meta.storeAs], {
573581
ordered,
574582
...action.meta,
583+
via,
575584
});
576585

577586
// append docs field to query
@@ -778,9 +787,9 @@ const mutation = (state, { action, key, path }) =>
778787
});
779788

780789
const HANDLERS = {
781-
[actionTypes.GET_SUCCESS]: initialize,
782-
[actionTypes.LISTENER_RESPONSE]: initialize,
783790
[actionTypes.SET_LISTENER]: initialize,
791+
[actionTypes.LISTENER_RESPONSE]: initialize,
792+
[actionTypes.GET_SUCCESS]: initialize,
784793
[actionTypes.UNSET_LISTENER]: conclude,
785794
[actionTypes.DOCUMENT_ADDED]: modify,
786795
[actionTypes.DOCUMENT_MODIFIED]: modify,

src/utils/query.js

+5
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ export function dispatchListenerResponse({
791791
mergeOrderedDocUpdates,
792792
mergeOrderedCollectionUpdates,
793793
} = firebase._.config || {};
794+
const fromCache =
795+
typeof docData.metadata?.fromCache === 'boolean'
796+
? docData.metadata.fromCache
797+
: true;
794798
const docChanges =
795799
typeof docData.docChanges === 'function'
796800
? docData.docChanges()
@@ -810,6 +814,7 @@ export function dispatchListenerResponse({
810814
payload: {
811815
data: dataByIdSnapshot(docData),
812816
ordered: orderedFromSnap(docData),
817+
fromCache,
813818
},
814819
merge: {
815820
docs: mergeOrdered && mergeOrderedDocUpdates,

test/unit/actions/firestore.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ describe('firestoreActions', () => {
463463
};
464464
const expectedAction2 = {
465465
meta: listenerConfig,
466-
payload: { data: null, ordered: [] },
466+
payload: { data: null, ordered: [], fromCache: true },
467467
merge: { collections: true, docs: true },
468468
type: actionTypes.LISTENER_RESPONSE,
469469
};
@@ -644,7 +644,7 @@ describe('firestoreActions', () => {
644644
};
645645
const expectedAction2 = {
646646
meta: listenerConfig,
647-
payload: { data: null, ordered: [] },
647+
payload: { data: null, ordered: [], fromCache: true },
648648
merge: { collections: true, docs: true },
649649
type: actionTypes.LISTENER_RESPONSE,
650650
};

0 commit comments

Comments
 (0)