Reaper decides if a device is present or not. Allows devices to "come back to life" if they start beating again.

This commit is contained in:
Simo Kinnunen
2015-06-03 18:49:42 +09:00
parent 2b84476c42
commit 736b6f769e
8 changed files with 149 additions and 26 deletions

View File

@@ -20,8 +20,10 @@ function TtlSet(ttl) {
util.inherits(TtlSet, EventEmitter)
TtlSet.prototype.bump = function(value, time) {
var item = this._remove(this.mapping[value]) || this._create(value)
TtlSet.SILENT = 1
TtlSet.prototype.bump = function(value, time, flags) {
var item = this._remove(this.mapping[value]) || this._create(value, flags)
item.time = time || Date.now()
item.prev = this.tail
@@ -37,8 +39,8 @@ TtlSet.prototype.bump = function(value, time) {
}
}
TtlSet.prototype.drop = function(value) {
this._drop(this.mapping[value])
TtlSet.prototype.drop = function(value, flags) {
this._drop(this.mapping[value], flags)
}
TtlSet.prototype.stop = function() {
@@ -59,8 +61,7 @@ TtlSet.prototype._check = function() {
var item
while ((item = this.head)) {
if (now - item.time > this.ttl) {
this._drop(item)
this.emit('timeout', item.value)
this._drop(item, 0)
}
else {
break
@@ -70,16 +71,27 @@ TtlSet.prototype._check = function() {
this._scheduleCheck()
}
TtlSet.prototype._create = function(value) {
TtlSet.prototype._create = function(value, flags) {
var item = new TtlItem(value)
this.mapping[value] = item
if ((flags & TtlSet.SILENT) !== TtlSet.SILENT) {
this.emit('insert', value)
}
return item
}
TtlSet.prototype._drop = function(item) {
TtlSet.prototype._drop = function(item, flags) {
if (item) {
this._remove(item)
delete this.mapping[item.value]
if ((flags & TtlSet.SILENT) !== TtlSet.SILENT) {
this.emit('drop', item.value)
}
}
}