standard-redux-shape
Advanced tools
Comparing version 0.5.2 to 0.6.0
{ | ||
"name": "standard-redux-shape", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "A library to help standardize your redux state shape", | ||
@@ -8,3 +8,3 @@ "main": "dist/index.js", | ||
"lint": "eslint src", | ||
"prepare": "npm run lint && npm run build", | ||
"prepublishOnly": "npm run lint && npm run build", | ||
"start": "webpack-dev-server --config=webpack.demo.config.js", | ||
@@ -11,0 +11,0 @@ "build": "webpack --config=webpack.build.config.js" |
@@ -78,4 +78,8 @@ /** | ||
* @param {Function} nextReducer 后续处理的reducer | ||
* @param {Function} customMerger 用户自定义的合并 table 的方法 | ||
*/ | ||
export const createTableUpdateReducer = (nextReducer = s => s) => (state = {}, action) => { | ||
export const createTableUpdateReducer = ( | ||
nextReducer = s => s, | ||
customMerger = (tableName, table, entities, defaultMerger) => defaultMerger() | ||
) => (state = {}, action) => { | ||
if (action.type !== UPDATE_ENTITY_TABLE) { | ||
@@ -98,10 +102,17 @@ return nextReducer(state, action); | ||
// 如果之前有一个比较完整的实体已经在`state`中,后来又来了一个不完整的实体,直接覆盖会导致字段丢失, | ||
// 因此针对每个实体,使用的是浅合并的策略,保证第一级的字段是不会丢的 | ||
// 因此默认针对每个实体,使用的是浅合并的策略,保证第一级的字段是不会丢的;更复杂场景下,由用户在 customMerger 中自行处理 | ||
// | ||
// 由于`key`中可能存在`"."`这个符号,而`san-update`具备属性访问路径的解析,会直接认为这是一个属性访问, | ||
// 因此这里搞成`[key]`强制表达这个属性就是一个带点的字符串 | ||
const merging = reduce(entities, (chain, value, key) => chain.merge([key], value), immutable(table)); | ||
const [mergedTable, diff] = merging.withDiff(); | ||
const newState = isEmpty(diff) ? state : {...state, [tableName]: mergedTable}; | ||
const defaultMerger = () => { | ||
const merging = reduce(entities, (chain, value, key) => chain.merge([key], value), immutable(table)); | ||
const[mergedTable, diff] = merging.withDiff(); | ||
return isEmpty(diff) ? table : mergedTable; | ||
}; | ||
const mergedTable = customMerger(tableName, table, entities, defaultMerger); | ||
const newState = mergedTable === table ? state : {...state, [tableName]: mergedTable}; | ||
return nextReducer(newState, action); | ||
}; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
861541
712