Skip to content

ModuleMap

do- edited this page Aug 22, 2024 · 7 revisions

ModuleMap is a class representing a collection of modules:

  • loaded from known filtered subtrees of the filesystem
  • and then recursively merged by name.

It is a Map descendant with:

  • String keys corresponding to modules' base file names
  • and Object values representing preprocessed code modules.

Overall, this class represents a registry of application code.

Static properties

Symbols

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

Error subclasses

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

Constructor

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, and watch are passed to the ModuleLoader constructor, which result is stored as the loader property;
  • merger, a new ObjectMerger by default, is stored as is.

Methods

get (key)

The standard get method is rewritten so:

  • the key must be a String;
  • if key is not found or loader.isModified (key) returns true:
    • loader.require (k) is scanned through
      • if none if found, it throws an Error;
    • each value obtained is stored by calling set (key, value) (with merging, see below);
  • finally,
    • the complete event is emitted by means of merger;
    • super.get (key) is returned.

getMethod (moduleName, methodName)

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.

load ()

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 complete event by means of merger for each existing entry.

set (key, value)

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.

Clone this wiki locally