1 /*jslint node: true, sloppy: true, forin: true, maxerr: 50, indent: 4 */
5 * To run module events:
6 * kiwi_mod.run(event_name, obj);
8 * - Each module call must return obj, with or without changes.
9 * - If a module call returns null, the event is considered cancelled
10 * and null is passed back to the caller to take action.
11 * For example, if null is returned for onmsg, kiwi stops sending
12 * the message to any clients.
15 var kiwi
= require('../kiwi.js');
16 var fs
= require('fs');
17 this.loaded_modules
= {};
21 * Load any unloaded modules as set in config
23 exports
.loadModules = function (kiwi_root
, config
) {
25 // Warn each module it is about to be unloaded
27 //this.loaded_modules = {};
29 // Load each module and run the onload event
30 for (i
in kiwi
.config
.modules
) {
31 mod_name
= kiwi
.config
.modules
[i
];
32 if (typeof this.loaded_modules
[mod_name
] !== 'undefined') continue;
34 this.loaded_modules
[mod_name
] = require(kiwi
.kiwi_root
+ '/' + kiwi
.config
.module_dir
+ mod_name
);
41 * Unload and reload a specific module
43 exports
.reloadModule = function (mod_name
) {
44 fs
.realpath(kiwi
.kiwi_root
+ '/' + kiwi
.config
.module_dir
+ mod_name
+ '.js', function(err
, resolvedPath
){
46 var mod_path
= resolvedPath
;
48 if (typeof kiwi
.kiwi_mod
.loaded_modules
[mod_name
] !== 'undefined') {
49 delete kiwi
.kiwi_mod
.loaded_modules
[mod_name
];
51 if (typeof require
.cache
[mod_path
] !== 'undefined') {
52 delete require
.cache
[mod_path
];
55 kiwi
.kiwi_mod
.loaded_modules
[mod_name
] = null;
56 kiwi
.kiwi_mod
.loaded_modules
[mod_name
] = require(mod_path
);
58 kiwi
.log('Module ' + mod_name
+ ' reloaded.');
60 kiwi
.log('reloadModule error!');
66 //return this.loaded_modules[mod_name] ? true : false;
71 * Run an event against all loaded modules
73 exports
.run = function (event_name
, event_data
, opts
) {
77 event_data
= (typeof event_data
=== 'undefined') ? {} : event_data
;
78 opts
= (typeof opts
=== 'undefined') ? {} : opts
;
80 for (mod_name
in this.loaded_modules
) {
81 if (typeof this.loaded_modules
[mod_name
]['on' + event_name
] === 'function') {
83 ret_tmp
= this.loaded_modules
[mod_name
]['on' + event_name
](ret
, opts
);
84 if (ret_tmp
=== null) {
96 exports
.printMods = function () {
98 kiwi
.log('Loaded Kiwi modules:');
99 for (mod_name
in this.loaded_modules
) {
100 kiwi
.log(' - ' + mod_name
);