lazy loading for modules.

This commit is contained in:
Christopher Jeffrey 2014-01-01 22:43:17 -06:00
parent c73a6c47b9
commit 636bb45965

View File

@ -9,32 +9,122 @@
* Modules
*/
var program = require('./program')
, tput = require('./tput')
, widget = require('./widget')
, colors = require('./colors');
function define(names, callback) {
if (!Array.isArray(names)) names = [names];
names.forEach(function(name) {
blessed.__defineGetter__(name, callback);
});
}
function load(name) {
load.modules = load.modules || {};
if (load.modules[name]) return load.modules[name];
return load.modules[name] = require(name);
}
function unload(names) {
if (!Array.isArray(names)) names = [names];
names.forEach(function(name) {
delete load.modules[name];
delete require.cache[require.resolve(name)];
});
}
/**
* Blessed
*/
function blessed() {
return program.apply(null, arguments);
return blessed.program.apply(null, arguments);
}
blessed.program = blessed.Program = program;
blessed.tput = blessed.Tput = tput;
blessed.colors = colors;
blessed.widget = widget;
Object.keys(blessed.widget).forEach(function(name) {
blessed[name] = blessed.widget[name];
define(['program', 'Program'], function() {
return load('./program');
});
define(['tput', 'Tput'], function() {
return load('./tput');
});
define('colors', function() {
return load('./colors');
});
define('widget', function() {
return load('./widget');
});
var widgets = [
'Node',
'Screen',
'Element',
'Box',
'Text',
'Line',
'ScrollableBox',
'List',
'ScrollableText',
'Form',
'Input',
'Textbox',
'Textarea',
'Button',
'ProgressBar',
'FileManager',
'Checkbox',
'RadioSet',
'RadioButton',
'Prompt',
'Question',
'Message',
'Info',
'Loading',
'PickList',
'Listbar',
'DirManager',
'Passbox'
];
widgets = widgets.concat(widgets.map(function(name) {
return name.toLowerCase();
}));
widgets.forEach(function(name) {
define(name, function() {
return blessed.widget[name];
});
});
// Maybe just:
// define(['screen', 'Screen'], function() {
// Object.keys(blessed.widget).forEach(function(name) {
// blessed.__defineGetter__(name, function() {
// return blessed.widget[name];
// });
// });
// return blessed.widget.screen;
// });
// Or:
// Object.keys(blessed.widget).forEach(function(name) {
// blessed.__defineGetter__(name, function() {
// return blessed.widget[name];
// });
// });
// unload(['./alias', './colors', './events', './program', './tput', './widget']);
blessed.helpers = {
sprintf: blessed.tput.sprintf,
merge: blessed.tput.merge,
tryRead: blessed.tput.tryRead
get sprintf() {
return blessed.tput.sprintf;
},
get merge() {
return blessed.tput.merge;
},
get tryRead() {
return blessed.tput.tryRead;
}
};
/**