berkeleys-redux-utils
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -0,1 +1,17 @@ | ||
<a name="3.0.0"></a> | ||
# [3.0.0](https://github.com/BerkeleyTrue/berkeleys-redux-utils/compare/v2.1.0...v3.0.0) (2017-09-05) | ||
### Features | ||
* **combineReducers:** Filter out duplicates ([bec9b26](https://github.com/BerkeleyTrue/berkeleys-redux-utils/commit/bec9b26)) | ||
### BREAKING CHANGES | ||
* **combineReducers:** combinedReducer will no longer throw for duplicated | ||
combined reducer | ||
<a name="2.1.0"></a> | ||
@@ -2,0 +18,0 @@ # [2.1.0](https://github.com/BerkeleyTrue/berkeleys-redux-utils/compare/v2.0.0...v2.1.0) (2017-09-05) |
@@ -19,3 +19,2 @@ 'use strict'; | ||
var isCombinedReducer = Symbol('@@isCombinedReducer'); | ||
var isEmpty = Symbol('@@isEmpty'); | ||
@@ -28,2 +27,5 @@ function combineReducers() { | ||
var cache = new Map(); | ||
var seen = new Set(); | ||
// get cached reducers | ||
reducers = reducers.reduce(function (reducers, reducer) { | ||
@@ -33,5 +35,3 @@ var _reducer = reducer; | ||
// if is an empty combinedReducer, filter out | ||
!!reducer[isEmpty] ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, '\nfound a combined reducer that has already been cleared.\ncheck the reducer with the name ' + reducer[isEmpty] + '\n') : (0, _invariant2.default)(false) : void 0; | ||
_reducer = reducer.getCached(); | ||
reducer.clearCache(); | ||
} | ||
@@ -41,16 +41,24 @@ return reducers.concat(_reducer); | ||
// check the reducers | ||
reducers.forEach(function (reducer) { | ||
!(typeof reducer === 'function') ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, 'reducers should be functions but found ' + reducer) : (0, _invariant2.default)(false) : void 0; | ||
!(reducer.toString !== Function.prototype.toString) ? process.env.NODE_ENV !== 'production' ? (0, _invariant2.default)(false, 'reducers must have a user defined toString function.\n check the reducer ' + reducer) : (0, _invariant2.default)(false) : void 0; | ||
cache.set(reducer.toString(), reducer); | ||
}); | ||
// filter out duplicates | ||
reducers = reducers.filter(function (r) { | ||
return seen.has(r.toString()) ? false : seen.add(r.toString()); | ||
}); | ||
// save reducers in cache | ||
reducers.forEach(function (r) { | ||
return cache.set(r.toString(), r); | ||
}); | ||
// create final reducer | ||
var finalReducer = (0, _reduceReducers2.default)(true, reducers); | ||
// mark as a combinedReducer | ||
finalReducer[isCombinedReducer] = true; | ||
finalReducer.clearCache = function () { | ||
var names = Array.from(cache.keys()).join('|'); | ||
cache.clear(); | ||
cache = null; | ||
finalReducer[isEmpty] = names; | ||
}; | ||
finalReducer.getCached = function () { | ||
@@ -57,0 +65,0 @@ return Array.from(cache.values()); |
{ | ||
"name": "berkeleys-redux-utils", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"main": "lib/index.js", | ||
@@ -58,2 +58,3 @@ "scripts": { | ||
"prettier": "^1.5.2", | ||
"sinon": "^3.2.1", | ||
"validate-commit-msg": "^2.14.0" | ||
@@ -60,0 +61,0 @@ }, |
@@ -6,6 +6,8 @@ import invariant from 'invariant'; | ||
const isCombinedReducer = Symbol('@@isCombinedReducer'); | ||
const isEmpty = Symbol('@@isEmpty'); | ||
export default function combineReducers(...reducers) { | ||
let cache = new Map(); | ||
let seen = new Set(); | ||
// get cached reducers | ||
reducers = reducers.reduce((reducers, reducer) => { | ||
@@ -15,11 +17,3 @@ let _reducer = reducer; | ||
// if is an empty combinedReducer, filter out | ||
invariant( | ||
!reducer[isEmpty], | ||
` | ||
found a combined reducer that has already been cleared. | ||
check the reducer with the name ${reducer[isEmpty]} | ||
`, | ||
); | ||
_reducer = reducer.getCached(); | ||
reducer.clearCache(); | ||
} | ||
@@ -29,2 +23,3 @@ return reducers.concat(_reducer); | ||
// check the reducers | ||
reducers.forEach(reducer => { | ||
@@ -40,13 +35,18 @@ invariant( | ||
); | ||
cache.set(reducer.toString(), reducer); | ||
}); | ||
// filter out duplicates | ||
reducers = reducers.filter( | ||
r => seen.has(r.toString()) ? false : seen.add(r.toString()), | ||
); | ||
// save reducers in cache | ||
reducers.forEach(r => cache.set(r.toString(), r)); | ||
// create final reducer | ||
const finalReducer = createFinalReducer(true, reducers); | ||
// mark as a combinedReducer | ||
finalReducer[isCombinedReducer] = true; | ||
finalReducer.clearCache = () => { | ||
const names = Array.from(cache.keys()).join('|'); | ||
cache.clear(); | ||
cache = null; | ||
finalReducer[isEmpty] = names; | ||
}; | ||
finalReducer.getCached = () => Array.from(cache.values()); | ||
@@ -53,0 +53,0 @@ |
38970
16
22
596