2014-12-25 02:32:18 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Bus = {};
|
|
|
|
|
|
|
|
/** @constructor */
|
|
|
|
Bus.Connector = function()
|
|
|
|
{
|
|
|
|
this.listeners = {};
|
|
|
|
this.pair = undefined;
|
|
|
|
};
|
|
|
|
|
2015-01-09 04:21:01 +01:00
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {function()} fn
|
|
|
|
* @param {Object} thisValue
|
|
|
|
*/
|
2014-12-25 02:32:18 +01:00
|
|
|
Bus.Connector.prototype.register = function(name, fn, thisValue)
|
|
|
|
{
|
|
|
|
var listeners = this.listeners[name];
|
|
|
|
|
|
|
|
if(listeners === undefined)
|
|
|
|
{
|
|
|
|
listeners = this.listeners[name] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners.push({
|
|
|
|
fn: fn,
|
|
|
|
thisValue: thisValue,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-01-09 04:21:01 +01:00
|
|
|
/**
|
|
|
|
* Unregister one message with the given name and callback
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @param {function()} fn
|
|
|
|
*/
|
|
|
|
Bus.Connector.prototype.unregister = function(name, fn)
|
|
|
|
{
|
|
|
|
var listeners = this.listeners[name];
|
|
|
|
|
|
|
|
if(listeners === undefined)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.listeners[name] = listeners.filter(function(l)
|
|
|
|
{
|
|
|
|
return l.fn !== fn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-12-26 20:36:58 +01:00
|
|
|
/**
|
2015-01-06 20:22:42 +01:00
|
|
|
* Send ("emit") a message
|
|
|
|
*
|
2014-12-26 20:36:58 +01:00
|
|
|
* @param {string} name
|
|
|
|
* @param {Object=} value
|
|
|
|
*/
|
2014-12-25 02:32:18 +01:00
|
|
|
Bus.Connector.prototype.send = function(name, value)
|
|
|
|
{
|
2014-12-26 20:36:58 +01:00
|
|
|
dbg_assert(arguments.length === 1 || arguments.length === 2);
|
2014-12-25 02:32:18 +01:00
|
|
|
|
|
|
|
if(!this.pair)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var listeners = this.pair.listeners[name];
|
|
|
|
|
|
|
|
if(listeners === undefined)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var i = 0; i < listeners.length; i++)
|
|
|
|
{
|
|
|
|
var listener = listeners[i];
|
|
|
|
listener.fn.call(listener.thisValue, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-06 20:22:42 +01:00
|
|
|
/**
|
|
|
|
* Send a message, guaranteeing that it is received asynchronously
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @param {Object=} value
|
|
|
|
*/
|
|
|
|
Bus.Connector.prototype.send_async = function(name, value)
|
|
|
|
{
|
|
|
|
dbg_assert(arguments.length === 1 || arguments.length === 2);
|
|
|
|
|
|
|
|
setTimeout(this.send.bind(this, name, value), 0);
|
|
|
|
};
|
|
|
|
|
2015-01-09 04:01:49 +01:00
|
|
|
/**
|
|
|
|
* Return true if a message with the given name should be sent. That is, if
|
|
|
|
* there are any listeners for this message
|
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
Bus.Connector.prototype.should_send = function(name)
|
|
|
|
{
|
|
|
|
if(!this.pair)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var listeners = this.pair.listeners[name];
|
|
|
|
|
|
|
|
return listeners !== undefined && listeners.length > 0;
|
|
|
|
};
|
2014-12-25 02:32:18 +01:00
|
|
|
|
|
|
|
Bus.create = function()
|
|
|
|
{
|
|
|
|
var c0 = new Bus.Connector();
|
|
|
|
var c1 = new Bus.Connector();
|
|
|
|
|
|
|
|
c0.pair = c1;
|
|
|
|
c1.pair = c0;
|
|
|
|
|
|
|
|
return [c0, c1];
|
|
|
|
};
|
|
|
|
|