Skip to content

Commit

Permalink
v0.7.0
Browse files Browse the repository at this point in the history
* fix(orderedReducer): updating arrays within a document properly - #103, #140, #141 - @pdyxs
* fix(query): oneListenerPerPath takes into account storeAs - #144 - @compojoom
* fix(orderedReducer): deleting a sub-collection no longer causes TypeError - #161
  • Loading branch information
prescottprue committed Feb 19, 2019
2 parents 5c34897 + 3ccacf1 commit fbf717d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "redux-firestore",
"version": "0.6.4",
"version": "0.7.0",
"description": "Redux bindings for Firestore.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
20 changes: 20 additions & 0 deletions src/actions/firestore.js
Expand Up @@ -266,6 +266,26 @@ export function setListeners(firebase, dispatch, listeners) {
}

const { config } = firebase._;

// Only attach one listener (count of matching listener path calls is tracked)
if (config.oneListenerPerPath) {
return listeners.forEach(listener => {
const path =
getQueryName(listener) +
(listener.storeAs ? `-${listener.storeAs}` : '');

const oldListenerCount = pathListenerCounts[path] || 0;
pathListenerCounts[path] = oldListenerCount + 1;

// If we already have an attached listener exit here
if (oldListenerCount > 0) {
return;
}

setListener(firebase, dispatch, listener);
});
}

const { allowMultipleListeners } = config;

return listeners.forEach(listener => {
Expand Down
4 changes: 2 additions & 2 deletions src/reducers/orderedReducer.js
@@ -1,5 +1,5 @@
import { size, get, unionBy, reject, omit, map, keyBy, isEqual } from 'lodash';
import { merge as mergeObjects } from 'lodash/fp';
import { merge as mergeObjects, assign as assignObjects } from 'lodash/fp';
import { actionTypes } from '../constants';
import {
updateItemInArray,
Expand Down Expand Up @@ -30,7 +30,7 @@ function modifyDoc(collectionState, action) {
if (!action.meta.subcollections || action.meta.storeAs) {
return updateItemInArray(collectionState, action.meta.doc, item =>
// Merge is used to prevent the removal of existing subcollections
mergeObjects(item, action.payload.data),
assignObjects(item, action.payload.data),
);
}

Expand Down
22 changes: 12 additions & 10 deletions src/utils/reducers.js
Expand Up @@ -117,16 +117,18 @@ export function pathFromMeta(meta) {
*/
export function updateItemInArray(array, itemId, updateItemCallback) {
let matchFound = false;
const modified = array.map(item => {
// Preserve items that do not have matching ids
if (!item || item.id !== itemId) {
return item;
}
matchFound = true;
// Use the provided callback to create an updated item
const updatedItem = updateItemCallback(item);
return updatedItem;
});
const modified = Array.isArray(array)
? array.map(item => {
// Preserve items that do not have matching ids
if (!item || item.id !== itemId) {
return item;
}
matchFound = true;
// Use the provided callback to create an updated item
const updatedItem = updateItemCallback(item);
return updatedItem;
})
: [];
if (!matchFound) {
modified.push(updateItemCallback({ id: itemId }));
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/reducers/orderedReducer.spec.js
Expand Up @@ -393,6 +393,24 @@ describe('orderedReducer', () => {
});
});

it('removes values from internal arrays', () => {
const id = 'doc';
const someField = ['single'];
const orderedData = [{ id, someField }];
action = {
meta: { collection: 'testing', doc: 'doc' },
type: actionTypes.LISTENER_RESPONSE,
payload: { ordered: orderedData, data: orderedData[0] },
};
state = {
testing: [{ id, someField: ['a thing', 'with multiple', 'values'] }],
};
expect(orderedReducer(state, action)).to.have.nested.property(
'testing.0.someField.length',
1,
);
});

describe('subcollections', () => {
it('adds a new doc within state with subcollection', () => {
const orderedData = { id: 'subDoc' };
Expand Down

0 comments on commit fbf717d

Please sign in to comment.