Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
45 changes: 27 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
var constants = require('redux-persist/constants')
var keyPrefix = constants.keyPrefix
var constants = require('redux-persist/constants');
var keyPrefix = constants.keyPrefix;

module.exports = function(persistor, config){
var config = config || {}
var blacklist = config.blacklist || false
var whitelist = config.whitelist || false
module.exports = function(persistor, config) {
var newConfig = config || {};
var blacklist = newConfig.blacklist || false;
var whitelist = newConfig.whitelist || false;
var isFocused = (document.hasFocus) ? document.hasFocus() : true;

window.addEventListener('storage', handleStorageEvent, false)
window.addEventListener('focus', function () {
isFocused = true;
}, false);

function handleStorageEvent(e){
if(e.key.indexOf(keyPrefix) === 0){
var keyspace = e.key.substr(keyPrefix.length)
if(whitelist && whitelist.indexOf(keyspace) === -1){ return }
if(blacklist && blacklist.indexOf(keyspace) !== -1){ return }

persistor.rehydrate(keyspace, e.newValue, function(){
//@TODO handle errors?
})
window.addEventListener('blur', function () {
isFocused = false;
}, false);

window.addEventListener('storage', function (event) {
if (isFocused) return;

if (event.key.indexOf(keyPrefix) === 0) {
var keyspace = event.key.substr(keyPrefix.length);
if (whitelist && whitelist.indexOf(keyspace) === -1) { return; }
if (blacklist && blacklist.indexOf(keyspace) !== -1) { return; }
// rehydrate storage with the new value
persistor.rehydrate(keyspace, event.newValue, function (key, state) {
// @TODO handle errors?
});
}
}
}
}, false);
};