UNPKG

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

61 lines (50 loc) 1.96 kB
'use strict'; exports.__esModule = true; var _is = require('ramda/src/is'); var _is2 = _interopRequireDefault(_is); var _isEmpty = require('ramda/src/isEmpty'); var _isEmpty2 = _interopRequireDefault(_isEmpty); var _curry = require('ramda/src/curry'); var _curry2 = _interopRequireDefault(_curry); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } exports.default = (0, _curry2.default)(function (throttling, debouncing, action) { if ((0, _isEmpty2.default)(throttling) && (0, _isEmpty2.default)(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 ((0, _is2.default)(String, throttleBy) && throttleBy === action.type || (0, _is2.default)(RegExp, throttleBy) && throttleBy.test(action.type) || (0, _is2.default)(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 ((0, _is2.default)(String, debounceBy) && debounceBy === action.type || (0, _is2.default)(RegExp, debounceBy) && debounceBy.test(action.type) || (0, _is2.default)(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); }); });