1 var events
= require('events'),
2 util
= require('util');
5 // Where events are bound to
9 // Create a publisher to allow event subscribing
10 function Publisher (obj
) {
11 var EventPublisher
= function modulePublisher() {};
12 util
.inherits(EventPublisher
, events
.EventEmitter
);
14 return new EventPublisher();
18 // Register an already created Publisher() as the active instance
19 function registerPublisher (obj
) {
20 active_publisher
= obj
;
28 * To be created by modules to bind to server events
30 function Module (module_name
) {}
33 // Holder for all the bound events by this module
34 Module
.prototype._events
= {};
37 // Keep track of this modules events and bind
38 Module
.prototype.subscribe = function (event_name
, fn
) {
39 this._events
[event_name
] = this._events
[event_name
] || [];
40 this._events
[event_name
].push(fn
);
42 active_publisher
.on(event_name
, fn
);
46 // Keep track of this modules events and bind once
47 Module
.prototype.once = function (event_name
, fn
) {
48 this._events
[event_name
] = this._events
[event_name
] || [];
49 this._events
[event_name
].push(fn
);
51 active_publisher
.once(event_name
, fn
);
55 // Remove any events by this module only
56 Module
.prototype.unsubscribe = function (event_name
, fn
) {
59 if (typeof event_name
=== 'undefined') {
63 } else if (typeof fn
=== 'undefined') {
64 // Remove all of 1 event type
65 delete this._events
[event_name
];
68 // Remove a single event + callback
69 for (idx
in (this._events
[event_name
] || [])) {
70 if (this._events
[event_name
][idx
] === fn
) {
71 delete this._events
[event_name
][idx
];
76 active_publisher
.removeListener(event_name
, fn
);
80 // Clean up anything used by this module
81 Module
.prototype.dispose = function () {
96 registerPublisher
: registerPublisher