55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
// ReporterSubscription class.
|
|
function ReporterSubscription(id, callback, reporter)
|
|
{
|
|
this.id = id;
|
|
this.callback = callback;
|
|
this.reporter = reporter;
|
|
}
|
|
|
|
// Reporter class.
|
|
function Reporter(name)
|
|
{
|
|
this.name =
|
|
(typeof name !== "undefined") ?
|
|
name :
|
|
"";
|
|
this.subscriptions = [];
|
|
}
|
|
Reporter.prototype.report = function()
|
|
{
|
|
for (var id in this.subscriptions)
|
|
{
|
|
var subscription = this.subscriptions[id];
|
|
subscription.callback();
|
|
}
|
|
}
|
|
Reporter.prototype.subscribe = function(callback)
|
|
{
|
|
var id = murom.uuid();
|
|
var subscription = new ReporterSubscription(id, callback, this);
|
|
this.subscriptions.push(subscription);
|
|
return subscription;
|
|
}
|
|
Reporter.prototype.subscribeMany = function(funcs)
|
|
{
|
|
for (var i = 0; i < funcs.length; ++i)
|
|
{
|
|
var func = funcs[i];
|
|
this.subscribe(func);
|
|
}
|
|
}
|
|
|
|
var Уведомитель = Reporter;
|
|
Reporter.prototype.уведомить = function()
|
|
{
|
|
this.report();
|
|
};
|
|
Reporter.prototype.подписать = function(функция)
|
|
{
|
|
this.subscribe(функция);
|
|
};
|
|
Reporter.prototype.подписатьМного = function(funcs)
|
|
{
|
|
this.subscribeMany(funcs);
|
|
};
|