-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add map actions for multiple items #6
Closed
bsonntag
wants to merge
1
commit into
adrienjt:master
from
bsonntag:feature/map-actions-for-multiple-items
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,20 @@ const empty = { | |
export default ({ | ||
initialState = empty, | ||
addActionTypes = [], | ||
addManyActionTypes = [], | ||
changeActionTypes = [], | ||
changeManyActionTypes = [], | ||
removeActionTypes = [], | ||
removeManyActionTypes = [], | ||
keyGetter = action => action.payload.id, | ||
itemGetter = action => ({...action.payload}), | ||
itemModifier = (item, action) => ({...item, ...action.payload}), | ||
addChangeItemsGetter = action => action.payload.reduce((result, item) => ({ | ||
...result, | ||
[item.id]: item | ||
}), {}), | ||
itemsModifier = (item, newItem) => ({...item, ...newItem}), | ||
removeKeysGetter = action => action.payload, | ||
resetActionTypes = [], | ||
emptyActionTypes = [], | ||
}) => ( | ||
|
@@ -20,10 +29,16 @@ export default ({ | |
const {type} = action; | ||
if (addActionTypes.includes(type)) { | ||
return add(state, action, keyGetter, itemGetter); | ||
} else if (addManyActionTypes.includes(type)) { | ||
return addMany(state, action, addChangeItemsGetter); | ||
} else if (changeActionTypes.includes(type)) { | ||
return change(state, action, keyGetter, itemModifier); | ||
} else if (changeManyActionTypes.includes(type)) { | ||
return changeMany(state, action, addChangeItemsGetter, itemsModifier); | ||
} else if (removeActionTypes.includes(type)) { | ||
return remove(state, action, keyGetter); | ||
} else if (removeManyActionTypes.includes(type)) { | ||
return removeMany(state, action, removeKeysGetter); | ||
} else if (resetActionTypes.includes(type)) { | ||
return initialState; | ||
} else if (emptyActionTypes.includes(type)) { | ||
|
@@ -33,14 +48,24 @@ export default ({ | |
} | ||
}; | ||
|
||
const add = (state, action, keyGetter, itemGetter) => { | ||
const key = keyGetter(action); | ||
const addItem = (state, key, item) => { | ||
return { | ||
byId: {...state.byId, [key]: itemGetter(action)}, | ||
byId: {...state.byId, [key]: item}, | ||
allIds: state.allIds.includes(key) ? state.allIds : state.allIds.concat(key), | ||
}; | ||
}; | ||
|
||
const add = (state, action, keyGetter, itemGetter) => { | ||
return addItem(state, keyGetter(action), itemGetter(action)); | ||
}; | ||
|
||
const addMany = (state, action, addChangeItemsGetter) => { | ||
const items = addChangeItemsGetter(action) | ||
return Object.keys(items).reduce((result, key) => { | ||
return addItem(result, key, items[key]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like your attempt at factorizing some code here. But the performance impact is O(n). Now that addChangeItemsGetter returns the items as a map-Object, you can merge them all at once: const addMany = (state, action, addChangeItemsGetter) => {
const items = addChangeItemsGetter(action);
const keys = Object.keys(items);
return {
byId: {...state.byId, ...items},
allIds: [...new Set(state.allIds.concat(keys))],
};
}
|
||
}, state); | ||
}; | ||
|
||
const change = (state, action, keyGetter, itemModifier) => { | ||
const key = keyGetter(action); | ||
const item = state.byId[key]; | ||
|
@@ -50,8 +75,18 @@ const change = (state, action, keyGetter, itemModifier) => { | |
}; | ||
}; | ||
|
||
const remove = (state, action, keyGetter) => { | ||
const key = keyGetter(action); | ||
const changeMany = (state, action, addChangeItemsGetter, itemsModifier) => { | ||
const items = addChangeItemsGetter(action); | ||
return Object.keys(items).reduce((result, key) => { | ||
const item = result.byId[key]; | ||
return item === undefined ? result : { | ||
byId: {...result.byId, [key]: itemsModifier(item, items[key])}, | ||
allIds: result.allIds, | ||
}; | ||
}, state); | ||
}; | ||
|
||
const removeItem = (state, key) => { | ||
const item = state.byId[key]; | ||
if (item === undefined) return state; | ||
const byId = {...state.byId}; | ||
|
@@ -61,3 +96,11 @@ const remove = (state, action, keyGetter) => { | |
allIds: state.allIds.filter(x => x !== key), | ||
}; | ||
}; | ||
|
||
const remove = (state, action, keyGetter) => { | ||
return removeItem(state, keyGetter(action)); | ||
}; | ||
|
||
const removeMany = (state, action, removeKeysGetter) => { | ||
return removeKeysGetter(action).reduce(removeItem, state); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unnecessary use of immutability: the
accumulator
's scope insideArray.prototype.reduce
'scallback
is well defined. We can safely mutate it. The immutable solution is actually very slow compared to the mutable solution I suggested: https://jsperf.com/mutable-vs-immutable-reduceI know that redux-data-structures doesn't focus on performance (https://github.com/adrienjt/redux-data-structures#performance), but this is a low hanging fruit.
If you're interested in the details, I suggest running the two snippets with
node --prof
. You'll see that the immutable solution spends most of its time copying arrays: