BotanSS/package.js

50 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-04-18 14:14:36 +00:00
// The Package Loader
var fs = require( "fs" );
var rootNS = {
botanss: "./"
};
var Package = function() { };
Package.prototype.rootNS = function( name, path )
{
if( rootNS[ name ] ) return;
rootNS[ name ] = fs.realpathSync( path ) + "/";
};
2022-04-03 12:52:44 +00:00
var _reload = function( e, filename )
{
2022-04-04 13:28:39 +00:00
if( this._lock == global.X_SERVER_CLUSTER.worker.id )
2022-04-03 12:52:44 +00:00
return;
2022-04-04 13:28:39 +00:00
this._lock = global.X_SERVER_CLUSTER.worker.id;
2022-04-03 12:52:44 +00:00
setTimeout( () =>
{
global.Dragonfly.Info( `Change detected: ${this.src}, reloading` );
global.X_SERVER_CLUSTER.worker.destroy();
} , 200 );
};
Package.prototype.load = function( _class )
2015-04-18 14:14:36 +00:00
{
var fSep = _class.indexOf( "." );
var nsdomain = _class.substr( 0, fSep );
_class = _class.substr( fSep + 1 ).replace( /\./g, "/" );
var file = rootNS[ nsdomain ] + _class;
2022-04-04 13:28:39 +00:00
if( global.debug && global.X_SERVER_CLUSTER && global.X_SERVER_CLUSTER.worker )
2015-06-03 06:45:45 +00:00
{
2022-04-03 12:52:44 +00:00
var src = require.resolve( file );
if(!( src in require.cache ))
{
fs.watch( src, _reload.bind({ "src": src }) );
}
2015-06-03 06:45:45 +00:00
}
2022-04-03 12:52:44 +00:00
return require( file );
2015-04-18 14:14:36 +00:00
};
global.botanLoader = new Package();