1 var events
= require('events'),
2 util
= require('util'),
4 EventPublisher
= require('./plugininterface.js');
9 * The main point in which events are fired and bound to
12 // Where events are bound to
16 // Create a publisher to allow event subscribing
17 function Publisher (obj
) {
18 var EventPublisher
= require('./plugininterface.js');
19 return new EventPublisher();
23 // Register an already created Publisher() as the active instance
24 function registerPublisher (obj
) {
25 active_publisher
= obj
;
34 * Keeping track of modules
37 // Hold the loaded modules
38 var registered_modules
= [];
40 function loadModule (module_file
) {
42 full_module_filename
= global
.config
.module_dir
+ module_file
;
44 // Get an instance of the module and remove it from the cache
46 module
= require(full_module_filename
);
47 delete require
.cache
[require
.resolve(full_module_filename
)];
49 // Module was not found
57 // Find a registered collection, .dispose() of it and remove it
58 function unloadModule (module
) {
59 var found_module
= false;
61 registered_modules
= _
.reject(registered_modules
, function (registered_module
) {
62 if (module
.toLowerCase() === registered_module
.module_name
.toLowerCase()) {
65 registered_module
.dispose();
80 * To be created by modules to bind to server events
82 function Module (module_name
) {
83 registered_modules
.push(this);
84 this.module_name
= module_name
;
86 // Holder for all the bound events by this module
92 // Keep track of this modules events and bind
93 Module
.prototype.on = function (event_name
, fn
) {
94 var internal_events
= ['dispose'];
96 this._events
[event_name
] = this._events
[event_name
] || [];
97 this._events
[event_name
].push(fn
);
99 // If this is an internal event, do not propogate the event
100 if (internal_events
.indexOf(event_name
) === -1) {
101 active_publisher
.on(event_name
, fn
);
106 // Keep track of this modules events and bind once
107 Module
.prototype.once = function (event_name
, fn
) {
108 this._events
[event_name
] = this._events
[event_name
] || [];
109 this._events
[event_name
].push(fn
);
111 active_publisher
.once(event_name
, fn
);
115 // Remove any events by this module only
116 Module
.prototype.off = function (event_name
, fn
) {
119 if (typeof event_name
=== 'undefined') {
123 } else if (typeof fn
=== 'undefined') {
124 // Remove all of 1 event type
125 delete this._events
[event_name
];
128 // Remove a single event + callback
129 for (idx
in (this._events
[event_name
] || [])) {
130 if (this._events
[event_name
][idx
] === fn
) {
131 delete this._events
[event_name
][idx
];
136 active_publisher
.off(event_name
, fn
);
141 // Clean up anything used by this module
142 Module
.prototype.dispose = function () {
143 // Call any dispose callbacks
144 (this._events
['dispose'] || []).forEach(function (callback
) {
148 // Remove all bound event listeners
160 Publisher
: Publisher
,
163 registerPublisher
: registerPublisher
,
165 unload
: unloadModule
,
166 getRegisteredModules: function () { return registered_modules
; }