Skip to content

Commit

Permalink
Fix save issue with multiple specified debounces
Browse files Browse the repository at this point in the history
  • Loading branch information
kilkelly committed Apr 10, 2022
1 parent 8e94444 commit 7374a3f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";var _merge=_interopRequireDefault(require("merge"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.clear=clear,exports.combineLoads=combineLoads,exports.load=load,exports.save=save;function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _iterableToArrayLimit(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f=[],g=!0,h=!1;try{for(c=c.call(a);!(g=(d=c.next()).done)&&(f.push(d.value),!(b&&f.length===b));g=!0);}catch(a){h=!0,e=a}finally{try{g||null==c["return"]||c["return"]()}finally{if(h)throw e}}return f}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var MODULE_NAME="[Redux-LocalStorage-Simple]",NAMESPACE_DEFAULT="redux_localstorage_simple",NAMESPACE_SEPARATOR_DEFAULT="_",STATES_DEFAULT=[],IGNORE_STATES_DEFAULT=[],DEBOUNCE_DEFAULT=0,IMMUTABLEJS_DEFAULT=!1,DISABLE_WARNINGS_DEFAULT=!1,debounceTimeout=null;// ---------------------------------------------------
"use strict";var _merge=_interopRequireDefault(require("merge"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.clear=clear,exports.combineLoads=combineLoads,exports.load=load,exports.save=save;function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _slicedToArray(a,b){return _arrayWithHoles(a)||_iterableToArrayLimit(a,b)||_unsupportedIterableToArray(a,b)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _iterableToArrayLimit(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f=[],g=!0,h=!1;try{for(c=c.call(a);!(g=(d=c.next()).done)&&(f.push(d.value),!(b&&f.length===b));g=!0);}catch(a){h=!0,e=a}finally{try{g||null==c["return"]||c["return"]()}finally{if(h)throw e}}return f}}function _arrayWithHoles(a){if(Array.isArray(a))return a}function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var MODULE_NAME="[Redux-LocalStorage-Simple]",NAMESPACE_DEFAULT="redux_localstorage_simple",NAMESPACE_SEPARATOR_DEFAULT="_",STATES_DEFAULT=[],IGNORE_STATES_DEFAULT=[],DEBOUNCE_DEFAULT=0,IMMUTABLEJS_DEFAULT=!1,DISABLE_WARNINGS_DEFAULT=!1,debounceTimeouts=new Map;// ---------------------------------------------------
/* warn
DESCRIPTION
Expand Down Expand Up @@ -112,7 +112,7 @@ function SafeLocalStorage(a){this.warnFn=a||warnConsole}Object.defineProperty(Sa
function f(a,b){return 1<a.split(".").length?lensPath(a.split("."),b):lensPath([a],b)}// Local function to avoid duplication of code above
function h(){0===c.length?o.setItem(g,j):c.forEach(function(a){var b=g+i+a,c=f(a,j);c?o.setItem(b,c):o.removeItem(b)})}// Bake disableWarnings into the warn function
var j,l=warn(m),n=b(d);isArray(c)||(console.error(MODULE_NAME,"'states' parameter in 'save()' method was passed a non-array value. Setting default value instead. Check your 'save()' method."),c=STATES_DEFAULT),isArray(e)||(console.error(MODULE_NAME,"'ignoreStates' parameter in 'save()' method was passed a non-array value. Setting default value instead. Check your 'save()' method."),e=IGNORE_STATES_DEFAULT),0<e.length&&(e=e.filter(function(a){return isString(a)?a:void console.error(MODULE_NAME,"'ignoreStates' array contains a non-string value. Ignoring this value. Check your 'ignoreStates' array.")})),isString(g)||(console.error(MODULE_NAME,"'namespace' parameter in 'save()' method was passed a non-string value. Setting default value instead. Check your 'save()' method."),g=NAMESPACE_DEFAULT),isString(i)||(console.error(MODULE_NAME,"'namespaceSeparator' parameter in 'save()' method was passed a non-string value. Setting default value instead. Check your 'save()' method."),i=NAMESPACE_SEPARATOR_DEFAULT),isInteger(k)||(console.error(MODULE_NAME,"'debounce' parameter in 'save()' method was passed a non-integer value. Setting default value instead. Check your 'save()' method."),k=DEBOUNCE_DEFAULT),j=0<e.length?handleIgnoreStates(e,a.getState()):a.getState();var o=new SafeLocalStorage(l);// Check to see whether to debounce LocalStorage saving
return k?(debounceTimeout&&clearTimeout(debounceTimeout),debounceTimeout=setTimeout(function(){h(c,g)},k)):h(c,g),n}}}}/**
return k?(debounceTimeouts.get(c+g)&&clearTimeout(debounceTimeouts.get(c+g)),debounceTimeouts.set(c+g,setTimeout(function(){h(c,g)},k))):h(c,g),n}}}}/**
Loads specified states from localstorage into the Redux state tree.
PARAMETERS
Expand Down
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const IGNORE_STATES_DEFAULT = []
const DEBOUNCE_DEFAULT = 0
const IMMUTABLEJS_DEFAULT = false
const DISABLE_WARNINGS_DEFAULT = false
let debounceTimeout = null
let debounceTimeouts = new Map()

// ---------------------------------------------------
/* warn
Expand Down Expand Up @@ -281,14 +281,17 @@ export function save ({
// Check to see whether to debounce LocalStorage saving
if (debounce) {
// Clear the debounce timeout if it was previously set
if (debounceTimeout) {
clearTimeout(debounceTimeout)
if (debounceTimeouts.get(states + namespace)) {
clearTimeout(debounceTimeouts.get(states + namespace))
}

// Save to LocalStorage after the debounce period has elapsed
debounceTimeout = setTimeout(function () {
_save(states, namespace)
}, debounce)
debounceTimeouts.set(
states + namespace,
setTimeout(function () {
_save(states, namespace)
}, debounce)
)
// No debouncing necessary so save to LocalStorage right now
} else {
_save(states, namespace)
Expand Down
2 changes: 1 addition & 1 deletion test/test.js

Large diffs are not rendered by default.

0 comments on commit 7374a3f

Please sign in to comment.