@checksub_team/peaks_timeline
Version:
JavaScript UI component for displaying audio waveforms
82 lines (67 loc) • 1.83 kB
JavaScript
/**
* @file
*
* Defines the {@link invoker} class.
*
* @module invoker
*/
define([
], function() {
'use strict';
/**
* An invoker class for throttling.
*
* @class
* @alias Invoker
*/
function Invoker() {
this._throttledFunc = {};
}
Invoker.prototype.throttle = function(id, func, wait) {
var self = this;
if (this._throttledFunc[id]) {
// Already limiting
this._throttledFunc[id].func = func;
this._throttledFunc[id].continue = true;
}
else {
// Create a limit
this._throttledFunc[id] = {
func: func,
timer: null,
continue: true
};
this._throttledFunc[id].timer = setInterval(function() {
if (self._throttledFunc[id].continue) {
func();
self._throttledFunc[id].continue = false;
}
else {
clearInterval(self._throttledFunc[id].timer);
delete self._throttledFunc[id];
}
}, wait);
}
};
Invoker.prototype.debounce = function(func, wait, immediate) {
var timeout;
return function executedFunction() {
// eslint-disable-next-line consistent-this
var _self = this;
var args = arguments;
function later() {
timeout = null;
if (!immediate) {
func.apply(_self, args);
}
}
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(_self, args);
}
};
};
return Invoker;
});