ruddy
Version:
Modularized state-management tools for modern front-end applications. Manage dispatched messages in a clean and predictable way for either small or large scale projects
48 lines (44 loc) • 1.59 kB
JavaScript
import _default3 from 'ramda/src/is';
import _default2 from 'ramda/src/isEmpty';
import _default from 'ramda/src/curry';
export default _default(function (throttling, debouncing, action) {
if (_default2(throttling) && _default2(debouncing)) {
return Promise.resolve(action);
}
var limiters = {};
return new Promise(function (resolve) {
if (!limiters[action.type]) {
var ms = 0;
if (throttling.some(function (_ref) {
var throttleBy = _ref[0],
milliseconds = _ref[1];
if (_default3(String, throttleBy) && throttleBy === action.type || _default3(RegExp, throttleBy) && throttleBy.test(action.type) || _default3(Function, throttleBy) && throttleBy(action)) {
ms = milliseconds;
return true;
}
return false;
})) {
resolve(action);
limiters[action.type] = setTimeout(function () {
delete limiters[action.type];
}, ms);
} else if (debouncing.some(function (_ref2) {
var debounceBy = _ref2[0],
milliseconds = _ref2[1];
if (_default3(String, debounceBy) && debounceBy === action.type || _default3(RegExp, debounceBy) && debounceBy.test(action.type) || _default3(Function, debounceBy) && debounceBy(action)) {
ms = milliseconds;
return true;
}
return false;
})) {
limiters[action.type] = setTimeout(function () {
resolve(action);
delete limiters[action.type];
}, ms);
} else {
resolve(action);
}
}
resolve(action);
});
});