[IMP] web: add a fix on underscore to support cancellableThrottle

This commit is contained in:
Vincent Schippefilt
2018-09-20 11:10:33 +02:00
parent ca95b39f77
commit 3092432daa
2 changed files with 53 additions and 0 deletions
@@ -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;
};
+1
View File
@@ -107,6 +107,7 @@
<script type="text/javascript" src="/web/static/src/js/libs/jquery.js"></script>
<script type="text/javascript" src="/web/static/src/js/libs/download.js"></script>
<script type="text/javascript" src="/web/static/src/js/libs/content-disposition.js"></script>
<script type="text/javascript" src="/web/static/src/js/libs/underscore.js"></script>
<script type="text/javascript" src="/web/static/src/js/services/config.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/abstract_service.js"></script>
<script type="text/javascript" src="/web/static/src/js/core/abstract_storage_service.js"></script>