Skip to content

Commit b91c7fe

Browse files
author
Bobby
committed
chore: PR changes
1 parent 8c07fa9 commit b91c7fe

File tree

9 files changed

+67
-114
lines changed

9 files changed

+67
-114
lines changed

src/reducers/cacheReducer.js

+4-35
Original file line numberDiff line numberDiff line change
@@ -403,46 +403,15 @@ const nestedMap = (obj, key, val) => {
403403
return obj;
404404
};
405405

406-
/**
407-
* Mutate ArrayUnion
408-
* @param {string} key - mutate tuple key
409-
* @param {*} val - mutate tuple value
410-
* @param {Function} cached - function that returns in-memory cached instance
411-
* @returns Null | Array<*>
412-
*/
413-
function arrayUnion(key, val, cached) {
414-
if (key !== '::arrayUnion') return null;
415-
return (cached() || []).concat([val]);
416-
}
406+
const arrayUnion = (key, val, cached) =>
407+
key !== '::arrayUnion' ? null : (cached() || []).concat([val]);
417408

418-
/**
419-
* Mutate arrayRemove
420-
* @param {string} key - mutate tuple key
421-
* @param {*} val - mutate tuple value
422-
* @param {Function} cached - function that returns in-memory cached instance
423-
* @returns Null | Array<*>
424-
*/
425-
function arrayRemove(key, val, cached) {
426-
return (
427-
key === '::arrayRemove' && (cached() || []).filter((item) => item !== val)
428-
);
429-
}
409+
const arrayRemove = (key, val, cached) =>
410+
key === '::arrayRemove' && (cached() || []).filter((item) => item !== val);
430411

431-
/**
432-
* Mutate increment
433-
* @param {string} key - mutate tuple key
434-
* @param {*} val - mutate tuple value
435-
* @param {Function} cached - function that returns in-memory cached instance
436-
* @returns Null | number
437-
*/
438412
const increment = (key, val, cached) =>
439413
key === '::increment' && typeof val === 'number' && (cached() || 0) + val;
440414

441-
/**
442-
* Mutate timestamp
443-
* @param {*} key
444-
* @returns
445-
*/
446415
const serverTimestamp = (key) => key === '::serverTimestamp' && new Date();
447416

448417
/**

src/reducers/listenersReducer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { combineReducers } from '../utils/reducers';
88
* @param {object} [state={}] - Current listenersById redux state
99
* @param {object} action - Object containing the action that was dispatched
1010
* @param {string} action.type - Type of action that was dispatched
11-
* @param action.path
12-
* @param action.payload
11+
* @param {String} action.path - Path of action
12+
* @param {object} action.payload - THe payload
1313
* @returns {object} listenersById state after reduction (used in listeners)
1414
* @private
1515
*/
@@ -35,7 +35,7 @@ function listenersById(state = {}, { type, path, payload }) {
3535
* @param {object} [state=[]] - Current authError redux state
3636
* @param {object} action - Object containing the action that was dispatched
3737
* @param {string} action.type - Type of action that was dispatched
38-
* @param action.payload
38+
* @param {object} action.payload - THe payload
3939
* @returns {object} allListeners state after reduction (used in listeners)
4040
* @private
4141
*/
@@ -44,7 +44,7 @@ function allListeners(state = [], { type, payload }) {
4444
case actionTypes.SET_LISTENER:
4545
return [...state, payload.name];
4646
case actionTypes.UNSET_LISTENER:
47-
return state.filter(name => name !== payload.name);
47+
return state.filter((name) => name !== payload.name);
4848
default:
4949
return state;
5050
}

src/reducers/statusReducer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function requestedReducer(state = {}, { type, meta }) {
7171
* @param {object} action - Object containing the action that was dispatched
7272
* @param {string} action.type - Type of action that was dispatched
7373
* @param {string} action.path - Path of action that was dispatched
74-
* @param action.meta
74+
* @param {object} action.meta - Metadata for action
7575
* @returns {object} Profile state after reduction
7676
*/
7777
export function timestampsReducer(state = {}, { type, meta }) {

src/utils/actions.js

+33-39
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,40 @@ export function wrapInDispatch(
3535
meta,
3636
payload: isObject(requestingType) ? requestingType.payload : { args },
3737
});
38-
const success = (result) => {
39-
const successIsObject = isObject(successType);
40-
// Built action object handling function for custom payload
41-
const actionObj = {
42-
type: successIsObject ? successType.type : successType,
43-
meta,
44-
payload:
45-
successIsObject && successType.payload
46-
? makePayload(successType, result)
47-
: { args },
48-
};
49-
// Attach preserve to action if it is passed
50-
if (successIsObject && successType.preserve) {
51-
actionObj.preserve = successType.preserve;
52-
}
53-
// Attach merge to action if it is passed
54-
if (successIsObject && successType.merge) {
55-
actionObj.merge = successType.merge;
56-
}
57-
dispatch(actionObj);
58-
return result;
59-
};
60-
const failure = (err) => {
61-
dispatch({
62-
type: errorType,
63-
meta,
64-
payload: err,
65-
});
66-
return Promise.reject(err);
67-
};
6838

69-
if (method === 'mutate') {
70-
return mutate(ref, ...args)
71-
.then(success)
72-
.catch(failure);
73-
}
74-
75-
return ref[method](...args)
76-
.then(success)
77-
.catch(failure);
39+
const actionPromise =
40+
method === 'mutate' ? mutate(ref, ...args) : ref[method](...args);
41+
return actionPromise
42+
.then((result) => {
43+
const successIsObject = isObject(successType);
44+
// Built action object handling function for custom payload
45+
const actionObj = {
46+
type: successIsObject ? successType.type : successType,
47+
meta,
48+
payload:
49+
successIsObject && successType.payload
50+
? makePayload(successType, result)
51+
: { args },
52+
};
53+
// Attach preserve to action if it is passed
54+
if (successIsObject && successType.preserve) {
55+
actionObj.preserve = successType.preserve;
56+
}
57+
// Attach merge to action if it is passed
58+
if (successIsObject && successType.merge) {
59+
actionObj.merge = successType.merge;
60+
}
61+
dispatch(actionObj);
62+
return result;
63+
})
64+
.catch((err) => {
65+
dispatch({
66+
type: errorType,
67+
meta,
68+
payload: err,
69+
});
70+
return Promise.reject(err);
71+
});
7872
}
7973

8074
/**

src/utils/mutate.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chunk, cloneDeep, flatten, isFunction, mapValues } from 'lodash';
1+
import { chunk, cloneDeep, flatten, mapValues } from 'lodash';
22
import debug from 'debug';
33
import { firestoreRef } from './query';
44
import mark from './profiling';
@@ -12,7 +12,7 @@ const info = debug('rrf:mutate');
1212
* @returns Boolean
1313
*/
1414
const docRef = (firestore, collection, doc) =>
15-
firestore.collection(collection).doc(doc);
15+
firestore.doc(`${collection}/${doc}`);
1616

1717
/**
1818
* @param object
@@ -123,20 +123,20 @@ function atomize(firebase, operation) {
123123
function write(firebase, operation = {}, writer = null) {
124124
const { collection, path, doc, id, data, ...rest } = operation;
125125
const ref = docRef(firebase.firestore(), path || collection, id || doc);
126-
const [changes, useUpdate = false] = atomize(firebase, data || rest);
126+
const [changes, requiresUpdate = false] = atomize(firebase, data || rest);
127127

128128
if (writer) {
129129
const writeType = writer.commit ? 'Batching' : 'Transaction.set';
130130
info(writeType, { id: ref.id, path: ref.parent.path, ...changes });
131-
if (useUpdate) {
131+
if (requiresUpdate) {
132132
writer.update(ref, changes);
133133
} else {
134134
writer.set(ref, changes, { merge: true });
135135
}
136136
return { id: ref.id, path: ref.parent.path, ...changes };
137137
}
138138
info('Writing', { id: ref.id, path: ref.parent.path, ...changes });
139-
if (useUpdate) {
139+
if (requiresUpdate) {
140140
return ref.update(changes);
141141
}
142142

@@ -196,23 +196,19 @@ async function writeInTransaction(firebase, operations) {
196196
};
197197

198198
const done = mark('mutate.writeInTransaction:reads');
199-
const readsPromised = mapValues(operations.reads, (read) => {
199+
const readsPromised = mapValues(operations.reads, async (read) => {
200200
if (isDocRead(read)) {
201201
const doc = firestoreRef(firebase, read);
202-
return getter(doc)
203-
.then((snapshot) => (snapshot.exsits === false ? null : snapshot))
204-
.then(serialize);
202+
const snapshot = await getter(doc);
203+
return serialize(snapshot.exsits === false ? null : snapshot);
205204
}
206205

207-
// NOTE: Firestore Transaction don't support collection inside
206+
// NOTE: Queries are not supported in Firestore Transactions (client-side)
208207
const coll = firestoreRef(firebase, read);
209-
return coll
210-
.get()
211-
.then((snapshot) => {
212-
if (snapshot.docs.length === 0) return Promise.resolve([]);
213-
return Promise.all(snapshot.docs.map(getter));
214-
})
215-
.then((docs) => docs.map(serialize));
208+
const snapshot = await coll.get();
209+
if (snapshot.docs.length === 0) return [];
210+
const unserializedDocs = await Promise.all(snapshot.docs.map(getter));
211+
return unserializedDocs.map(serialize);
216212
});
217213

218214
done();
@@ -222,7 +218,8 @@ async function writeInTransaction(firebase, operations) {
222218

223219
operations.writes.forEach((writeFnc) => {
224220
const complete = mark('mutate.writeInTransaction:writes');
225-
const operation = isFunction(writeFnc) ? writeFnc(reads) : writeFnc;
221+
const operation =
222+
typeof writeFnc === 'function' ? writeFnc(reads) : writeFnc;
226223

227224
if (Array.isArray(operation)) {
228225
operation.map((op) => write(firebase, op, transaction));

src/utils/query.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,9 @@ export function firestoreRef(firebase, meta) {
172172
const { globalDataConvertor } =
173173
(firebase && firebase._ && firebase._.config) || {};
174174

175-
if (path) ref = ref.collection(path);
176-
if (collection) ref = ref.collection(collection);
175+
if (path || collection) ref = ref.collection(path || collection);
177176
if (collectionGroup) ref = ref.collectionGroup(collectionGroup);
178-
if (id) ref = ref.doc(id);
179-
if (doc) ref = ref.doc(doc);
177+
if (id || doc) ref = ref.doc(id || doc);
180178
ref = handleSubcollections(ref, subcollections);
181179
if (where) ref = addWhereToRef(ref, where);
182180
if (orderBy) ref = addOrderByToRef(ref, orderBy);

test/unit/actions/firestore.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ describe('firestoreActions', () => {
909909
parent: { path: 'path' },
910910
}));
911911
const collection = sinon.spy(() => ({ doc }));
912-
const firestore = sinon.spy(() => ({ collection }));
912+
const firestore = sinon.spy(() => ({ collection, doc }));
913913

914914
const instance = createFirestoreInstance(
915915
{ firestore },

test/unit/utils/actions.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ describe('actions utils', () => {
8282
parent: { path: 'path' },
8383
}));
8484
const collection = sinon.spy(() => ({ doc }));
85-
const firestore = sinon.spy(() => ({ collection }));
85+
const firestore = sinon.spy(() => ({ collection, doc }));
8686
wrapInDispatch(dispatchSpy, {
8787
ref: { firestore },
8888
types: ['mutate', 'mutate', 'mutate'],
8989
args: [{ collection: '/collection/path', doc: 'doc', data: { a: 1 } }],
9090
method: 'mutate',
9191
});
92-
expect(collection).to.have.been.calledOnceWith('/collection/path');
93-
expect(doc).to.have.been.calledOnceWith('doc');
92+
expect(doc).to.have.been.calledOnceWith('/collection/path/doc');
9493
expect(set).to.have.been.calledOnceWith({ a: 1 });
9594
});
9695
});

test/unit/utils/mutate.spec.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ describe('firestore.mutate()', () => {
1010
id: 'id',
1111
parent: { path: 'path' },
1212
}));
13-
const collection = sinon.spy(() => ({ doc }));
14-
const firestore = sinon.spy(() => ({ collection }));
13+
const firestore = sinon.spy(() => ({ doc }));
1514

1615
await mutate(
1716
{ firestore },
@@ -24,8 +23,7 @@ describe('firestore.mutate()', () => {
2423
},
2524
);
2625

27-
expect(collection.calledWith('orgs/tara-ai/teams'));
28-
expect(doc.calledWith('team-bravo'));
26+
expect(doc.calledWith('orgs/tara-ai/teams/team-bravo'));
2927
expect(set.calledWith({ name: 'Bravo Team 🎄' }, { merge: true }));
3028
});
3129

@@ -37,10 +35,9 @@ describe('firestore.mutate()', () => {
3735
id: 'id',
3836
parent: { path: 'path' },
3937
}));
40-
const collection = sinon.spy(() => ({ doc }));
4138

4239
const batch = sinon.spy(() => ({ set, commit }));
43-
const firestore = sinon.spy(() => ({ batch, collection }));
40+
const firestore = sinon.spy(() => ({ batch, doc }));
4441

4542
await mutate({ firestore }, [
4643
{
@@ -88,10 +85,9 @@ describe('firestore.mutate()', () => {
8885
id: 'id',
8986
parent: { path: 'path' },
9087
}));
91-
const collection = sinon.spy(() => ({ doc }));
9288

9389
const batch = sinon.spy(() => ({ set, commit }));
94-
const firestore = sinon.spy(() => ({ batch, collection }));
90+
const firestore = sinon.spy(() => ({ batch, doc }));
9591

9692
const writes = Array.from(Array(501), (_el, index) => ({
9793
collection: 'orgs/tara-ai/teams',
@@ -288,7 +284,7 @@ describe('firestore.mutate()', () => {
288284
parent: { path: 'path' },
289285
}));
290286
const collection = sinon.spy(() => ({ doc }));
291-
const firestore = sinon.spy(() => ({ collection }));
287+
const firestore = sinon.spy(() => ({ collection, doc }));
292288
firestore.FieldValue = {
293289
serverTimestamp: sinon.spy(() => 'time'),
294290
increment: sinon.spy(() => '++'),

0 commit comments

Comments
 (0)