siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
101 lines (73 loc) • 3.02 kB
HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js">/*
Siesta 5.6.1
Copyright(c) 2009-2022 Bryntum AB
https://bryntum.com/contact
https://bryntum.com/products/siesta/license
*/
Class('Siesta.Util.Throttler', {
has : {
lastActivationTime : null,
func : { required : true },
scope : { required : true },
// activate only after this amount of time after last tick (ignoring any preceding ticks)
buffer : null,
// activate at least every `throttle` time
throttle : null,
bufferTimeoutId : null,
args : null
},
methods : {
tick : function () {
var now = new Date() - 0
this.args = arguments
if (this.throttle != null) {
if (!this.lastActivationTime) this.lastActivationTime = now
var timeAfterLastActivation = now - this.lastActivationTime
if (timeAfterLastActivation > this.throttle) this.activate()
}
if (this.buffer != null) {
var me = this
if (this.bufferTimeoutId) clearTimeout(this.bufferTimeoutId)
this.bufferTimeoutId = setTimeout(function () {
me.activate()
}, this.buffer)
}
},
activate : function () {
this.lastActivationTime = new Date() - 0
if (this.bufferTimeoutId) clearTimeout(this.bufferTimeoutId)
this.bufferTimeoutId = null
var args = this.args
this.args = null
this.func.apply(this.scope, args)
},
cancel : function () {
if (this.bufferTimeoutId) clearTimeout(this.bufferTimeoutId)
this.bufferTimeoutId = null
this.args = null
},
flush : function () {
if (this.bufferTimeoutId != null) this.activate()
}
}
})
</pre>
</body>
</html>