1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 02:09:32 +02:00

Move some screen utilities to the global util directory so that they can be used for other things too.

This commit is contained in:
Simo Kinnunen 2015-04-30 13:41:32 +09:00
parent ae9f2b5ab0
commit 5bb05f52f6
4 changed files with 3 additions and 3 deletions

32
lib/util/failcounter.js Normal file
View file

@ -0,0 +1,32 @@
var util = require('util')
var EventEmitter = require('eventemitter3').EventEmitter
function FailCounter(threshold, time) {
this.threshold = threshold
this.time = time
this.values = []
}
util.inherits(FailCounter, EventEmitter)
FailCounter.prototype.inc = function() {
var now = Date.now()
while (this.values.length) {
if (now - this.values[0] >= this.time) {
this.values.shift()
}
else {
break
}
}
this.values.push(now)
if (this.values.length > this.threshold) {
this.emit('exceedLimit', this.threshold, this.time)
}
}
module.exports = FailCounter