Skip to content

Instantly share code, notes, and snippets.

@darkone23
Last active August 29, 2015 13:57
Show Gist options
  • Save darkone23/9571308 to your computer and use it in GitHub Desktop.
Save darkone23/9571308 to your computer and use it in GitHub Desktop.
count the chatters & lurkers in meatspace
var UserCounter = function(activeUsers) {
var ttl = nconf.get('ttl');
function expiredBefore(now) {
return function(message) {
return message['timestamp'] + ttl > now;
}
}
function update() {
var now = new Date().getTime();
this.activeUsers = this.activeUsers.filter(expiredBefore(now))
}
this.intervalId = setInterval(update.bind(this), 1000)
this.activeUsers = activeUsers
}
UserCounter.prototype.message = function(userId) {
var now = new Date().getTime(),
message = {"id": userId, "timestamp": now};
this.activeUsers = this.activeUsers.filter(function(msg) {
return msg['id'] !== userId;
}).concat([message]);
}
UserCounter.prototype.count = function() {
return this.activeUsers.length;
}
var userCounter = new UserCounter([]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment