wazuh-kibana-app/public/kibana-integrations/debounce.js

74 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-07-12 10:26:18 +00:00
/*
2019-09-02 15:48:56 +00:00
* Author: Elasticsearch B.V.
* Updated by Wazuh, Inc.
2019-07-12 10:26:18 +00:00
*
2019-09-02 15:48:56 +00:00
* Copyright (C) 2015-2019 Wazuh, Inc.
2019-07-12 10:26:18 +00:00
*
2019-09-02 15:48:56 +00:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
2019-07-12 10:26:18 +00:00
*/
import _ from 'lodash';
import { uiModules } from 'ui/modules';
// Debounce service, angularized version of lodash debounce
// borrowed heavily from https://github.com/shahata/angular-debounce
const module = uiModules.get('app/wazuh');
2019-07-22 14:13:52 +00:00
module.service('debounce', [
'$timeout',
function($timeout) {
return function(func, wait, options) {
let timeout;
let args;
let self;
let result;
options = _.defaults(options || {}, {
leading: false,
trailing: true,
invokeApply: true
});
2019-07-12 10:26:18 +00:00
2019-07-22 14:13:52 +00:00
function debounce() {
self = this;
args = arguments;
2019-07-12 10:26:18 +00:00
2019-07-22 14:13:52 +00:00
const later = function() {
timeout = null;
if (!options.leading || options.trailing) {
result = func.apply(self, args);
}
};
2019-07-12 10:26:18 +00:00
2019-07-22 14:13:52 +00:00
const callNow = options.leading && !timeout;
2019-07-12 10:26:18 +00:00
2019-07-22 14:13:52 +00:00
if (timeout) {
$timeout.cancel(timeout);
}
timeout = $timeout(later, wait, options.invokeApply);
2019-07-12 10:26:18 +00:00
2019-07-22 14:13:52 +00:00
if (callNow) {
result = func.apply(self, args);
}
return result;
2019-07-12 10:26:18 +00:00
}
2019-07-22 14:13:52 +00:00
debounce.cancel = function() {
$timeout.cancel(timeout);
timeout = null;
};
2019-07-12 10:26:18 +00:00
2019-07-22 14:13:52 +00:00
return debounce;
2019-07-12 10:26:18 +00:00
};
2019-07-22 14:13:52 +00:00
}
]);
2019-07-12 10:26:18 +00:00
export function DebounceProvider(debounce) {
return debounce;
}