EXPORTED_SYMBOLS = ["module"];
var module = function (application) {
var log = function (message) {
application.logger.debug("[VK counters] " + message);
}
;
var Counters = function () {
this.init();
}
;
Counters.prototype = {
constructor: Counters,
_cache: null,
init: function () {
var counters = this;
this._cache = new application.Cache("counters.json");
var mailman = application.mailman;
mailman.addSubscriber({
subject: "new-messages",
subscriber: {
receive: function ({body: messages}) {
counters._update("messages",messages.total);
}
}});
mailman.addSubscriber({
subject: "new-friends",
subscriber: {
receive: function ({body: requests}) {
counters._update("friends",requests.total);
}
}});
}
,
_update: function (id, value) {
log("\"" + id + "\" = \"" + value + "\".");
var mailman = application.mailman;
mailman.send({
subject: "counter-update",
body: {
id: id,
value: value}});
this._cache.put(id,String(value));
}
,
get: function (id) {
var value = 0;
var cachedValue = this._cache.get(id);
if (null !== cachedValue)
{
value = Number(cachedValue);
}

return value;
}
,
reset: function () {
this._cache.clear();
}
};
return Counters;
}
;
