Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

78 lines (68 sloc) 2.372 kB
namespace Rosella.Event
{
const string DEFAULT_SUBSCRIBER_FACTORY = "Rosella.Event.default_subscriber_factory";
const string DEFAULT_PAYLOAD_FACTORY = "Rosella.Event.payload_factory";
function default_subscriber_factory()
{
var temp = Rosella.Globals.get_global(DEFAULT_SUBSCRIBER_FACTORY);
if (temp == null) {
temp = new Rosella.Event.Subscriber.Factory();
Rosella.Globals.register_global(DEFAULT_SUBSCRIBER_FACTORY, temp);
}
return temp;
}
function payload_factory()
{
var temp = Rosella.Globals.get_global(DEFAULT_PAYLOAD_FACTORY);
if (temp == null) {
temp = new Rosella.ObjectFactory(class Rosella.Event.Payload);
Rosella.Globals.register_global(DEFAULT_PAYLOAD_FACTORY, temp);
}
return temp;
}
}
/* Event Class.
An Event is basically a data object that holds information from the
publisher to transmit to various subscribers. When an event is raised,
the event and all its data are passed to Queues and, ultimately, to
subscribers.
See the Queue and Manager class documentation for more details.
*/
class Rosella.Event
{
var subscribers;
var subscriber_factory;
var payloads;
// Constructor
function Event()
{
self.subscribers = {};
self.subscriber_factory = Rosella.Event.default_subscriber_factory();
}
function subscribe(string name, var action, string dispatch)
{
var subscriber = self.subscriber_factory.create(action, dispatch);
self.subscribers[name] = subscriber;
}
function unsubscribe(event_name)
{
if (exists self.subscribers[event_name])
delete self.subscribers[event_name];
}
/* Event Raise Routines
*/
function publish(var pos [slurpy], var named [slurpy,named])
{
var payloads = {};
var payload_factory = Rosella.Event.payload_factory();
for (string subscriber_name in self.subscribers) {
var subscriber = self.subscribers[subscriber_name];
if (subscriber != null) {
var payload = payload_factory.create(self, subscriber_name, pos, named);
payloads[subscriber_name] = payload;
subscriber.execute(payload);
}
}
return payloads;
}
}
Jump to Line
Something went wrong with that request. Please try again.