First of all I need to apologize because this is going to have alot of code in it, which will bloat this question. However I think this will help to understand my problem a little better.
Let's say I have this given MainModule
:
'use strict';
/**
* Loads Module from given name.
* @class {LoadModules}
*/
class LoadModules {
constructor(moduleName) {
// define Modulename.
this.moduleName = moduleName;
}
/**
* Initialize module.
* @method init
* @return {void} [nothing]
*/
init() {
// Path Module.
const path = require('path');
// Require Module from given Name.
let ModuleToLoad = require(path.join(__dirname, 'Modules', this.moduleName, this.moduleName + '.js'));
// Instatiate Module.
ModuleToLoad = new ModuleToLoad();
// Start module.
ModuleToLoad.init();
}
}
And an other module which can be loaded into the MainModule
:
/**
* This is a Module which can be loaded by the MainModule.
* @class {ModuleToBeLoaded}
*/
module.exports = class ModuleToBeLoaded {
constructor() {
/**
* Empty
*/
}
/**
* Initialize newly loaded Module.
* @method init
* @return {void} [nothing]
*/
init() {
console.log('module Loaded');
}
};
As you can see this is used to load modules dynamically, which works perfectly fine.
My problem is that my MainModule
which loads other modules dynamically can't share its own global scope between the modules, or at least I didn't figure out how. I am well aware that this is complicated since my MainModule
and ModuleToBeLoaded
are in different files.
For example I have a LoggerClass
in my global scope of my MainModule
:
// This is in the global scope.
const LoggerClass = new Logger();
/**
* LoadModule class stuff comes after this.
*/
I want all Modules to access the LoggerClass as if it were in their own global scope without defining it again and again in each and every module. As an example I would change console.log
in the ModuleToBeLoaded
class into this:
/**
* Initialize newly loaded Module.
* @method init
* @return {void} [nothing]
*/
init() {
LoggerClass.log('module Loaded');
}
So basically I define Globals in the MainModule
and I want to access these Globals in the Code of the ModuleToBeLoaded
. A possible solution could be to change the constructor
in the ModuleToBeLaoded
. Like this:
constructor(LoggerClass) {
// Here I can set LoggerClass internally.
this.LoggerClass = LoggerClass;
}
/**
* Initialize newly loaded Module.
* @method init
* @return {void} [nothing]
*/
init() {
this.LoggerClass.log('module Loaded');
}
Which could allow me to instantiate the class like this:
// Instatiate Module.
ModuleToLoad = new ModuleToLoad(LoggerClass);
Is this the right way, or is there any other solution more preferably?
Sidenote: I'm in a NodeJS 7.10.0
environment without any 3rd party libraries.
Regards, Megajin
via Megajin
No comments:
Post a Comment