1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 02:29:26 +02:00
OpenSTF/lib/units/device/plugins/screen/util/broadcastset.js
Gunther Brunner 711ab4a8e5 Updated npm dependencies.
ProtobufJS and Bluebird require further work.
2016-08-03 20:08:30 +09:00

48 lines
930 B
JavaScript

var util = require('util')
var EventEmitter = require('eventemitter3')
function BroadcastSet() {
this.set = Object.create(null)
this.count = 0
}
util.inherits(BroadcastSet, EventEmitter)
BroadcastSet.prototype.insert = function(id, ws) {
if (!(id in this.set)) {
this.set[id] = ws
this.count += 1
this.emit('insert', id)
if (this.count === 1) {
this.emit('nonempty')
}
}
}
BroadcastSet.prototype.remove = function(id) {
if (id in this.set) {
delete this.set[id]
this.count -= 1
this.emit('remove', id)
if (this.count === 0) {
this.emit('empty')
}
}
}
BroadcastSet.prototype.values = function() {
return Object.keys(this.set).map(function(id) {
return this.set[id]
}, this)
}
BroadcastSet.prototype.keys = function() {
return Object.keys(this.set)
}
BroadcastSet.prototype.get = function(id) {
return this.set[id]
}
module.exports = BroadcastSet