diff options
| author | Vincent Schippefilt <vsc@odoo.com> | 2018-09-14 13:46:07 +0200 |
|---|---|---|
| committer | Vincent Schippefilt <vsc@odoo.com> | 2018-09-20 11:10:33 +0200 |
| commit | 3092432daa95b8c5b5582fb2e4a6bb44ef064055 (patch) | |
| tree | 8698931baec2dcee01e5321c4664a683a16810b6 /addons/web/static/src/js/libs | |
| parent | ca95b39f775dcd5489e81f7b6c2e589819e024bc (diff) | |
[IMP] web: add a fix on underscore to support cancellableThrottle
Diffstat (limited to 'addons/web/static/src/js/libs')
| -rw-r--r-- | addons/web/static/src/js/libs/underscore.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/addons/web/static/src/js/libs/underscore.js b/addons/web/static/src/js/libs/underscore.js new file mode 100644 index 00000000000..083ee22f4d7 --- /dev/null +++ b/addons/web/static/src/js/libs/underscore.js @@ -0,0 +1,52 @@ +/** + * The _throttle in underscore has the feature to cancel the throttled function + * only starting version 1.9.0 + * @todo remove this in master and update underscorejs to 1.9.1 + */ + +// Returns a function, that, when invoked, will only be triggered at most once +// during a given window of time. Normally, the throttled function will run +// as much as it can, without ever going more than once per `wait` duration; +// but if you'd like to disable the execution on the leading edge, pass +// `{leading: false}`. To disable execution on the trailing edge, ditto. + +_.cancellableThrottleRemoveMeSoon = function (func, wait, options) { + var timeout, context, args, result; + var previous = 0; + if (!options) options = {}; + + var later = function () { + previous = options.leading === false ? 0 : _.now(); + timeout = null; + result = func.apply(context, args); + if (!timeout) context = args = null; + }; + + var throttled = function () { + var now = _.now(); + if (!previous && options.leading === false) previous = now; + var remaining = wait - (now - previous); + context = this; + args = arguments; + if (remaining <= 0 || remaining > wait) { + if (timeout) { + clearTimeout(timeout); + timeout = null; + } + previous = now; + result = func.apply(context, args); + if (!timeout) context = args = null; + } else if (!timeout && options.trailing !== false) { + timeout = setTimeout(later, remaining); + } + return result; + }; + + throttled.cancel = function () { + clearTimeout(timeout); + previous = 0; + timeout = context = args = null; + }; + + return throttled; +}; |
