-
Notifications
You must be signed in to change notification settings - Fork 0
ModuleMap
ModuleMap is a class representing a collection of modules:
It is a Map descendant with:
-
Stringkeys corresponding to modules' base file names - and
Objectvalues representing preprocessed code modules.
Overall, this class represents a registry of application code.
| Name | Description |
|---|---|
ModuleMap.MODULE_NAME |
Each module fetched with get (name) have this property set to name
|
ModuleMap.MODULE |
Each method fetched with getMethod (_, __) have this property set to the containing module |
ModuleMap.METHOD_NAME |
Each method fetched with getMethod (_, name) have this property set to name
|
| Name | Description |
|---|---|
ModuleMap.ModuleNotFoundError |
Thrown by getMethod (moduleName, _) if moduleName is not found |
ModuleMap.MethodNotFoundError |
Thrown by getMethod (moduleName, methodName) if the module loaded doesn't contain methodName
|
ModuleMap.NotAMethodError |
Thrown by getMethod (moduleName, methodName) if the property named methodName didn't happen to be a function |
The standard constructor is totally overridden, there is no way to set the initial content to ModuleMap.
const {ModuleMap} = require ('require-sliced')
const myModuleMap = new ModuleMap ({
dir: {
root: ['/opt/myProject'],
// filter: (str, arr) => arr.at (-2) === 'Model', // **/Model/*
// live: false,
},
// ext: '.js',
// watch: false,
// merger: new myObjectMerger (someOptions)
})The only parameter here is a bag of options where:
-
dir,ext, andwatchare passed to the ModuleLoader constructor, which result is stored as theloaderproperty; -
merger, a new ObjectMerger by default, is stored as is.
The standard get method is rewritten so:
- the
keymust be aString; - if
keyis not found orloader.isModified (key)returnstrue:-
loader.require (k)is scanned through- if none if found, it throws an Error;
- each
valueobtained is stored by callingset (key, value)(with merging, see below);
-
- finally,
- the
completeevent is emitted by means ofmerger; -
super.get (key)is returned.
- the
Fetches the module with get (moduleName) and returns its method called methodName with [ModuleMap.MODULE] and [ModuleMap.METHOD_NAME] properties set.
May throw ModuleMap.ModuleNotFoundError, ModuleMap.MethodNotFoundError or ModuleMap.NotAMethodError.
This method scans through loader.requireAll () result and for each [name, module] pair obtained:
- stores it by calling
set (name, module)(with merging, see below); - finally, emits the
completeevent by means ofmergerfor each existing entry.
The standard set method is rewritten so for an existing key, the new value is merged into the old one instead of overwriting it.
In this class, set is called internally by get and load and not intended for direct use.