Last active
August 29, 2015 13:57
-
-
Save darkone23/9571308 to your computer and use it in GitHub Desktop.
count the chatters & lurkers in meatspace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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