EXPORTED_SYMBOLS = ["module"];
var module = function (application) {
var log = function (message) {
application.logger.trace("[Repeater] " + message);
}
;
var Cache = application.Cache;
var Repeater = function (subjectsList) {
this.init(subjectsList);
}
;
Repeater.prototype = {
constructor: Repeater,
init: function (subjectsList) {
var repeater = this;
this.cache = new Cache("repeater.json");
var mailman = application.mailman;
const REQUEST_NAME = "please-repeat";
mailman.addSubscriber({
subject: REQUEST_NAME,
subscriber: {
receive: function ({body: id}) {
log("Received request to repeat \"" + id + "\".");
var string = repeater.cache.get(id);
if (null === string)
{
log("There is no data in cache for \"" + id + "\".");
return;
}

var data = JSON.parse(string);
mailman.send({
subject: id,
body: data});
log("Message sent.");
}
}});
var mailman = application.mailman;
var subscriber = {
receive: function ({subject: subject, body: data}) {
repeater.cache.put(subject,JSON.stringify(data));
}
};
subjectsList.forEach(function (subject) {
mailman.addSubscriber({
subject: subject,
subscriber: subscriber});
}
);
}
,
clear: function () {
this.cache.clear();
}
};
return Repeater;
}
;
