@wider/registry

WIDER modules make significant use of each other. They are published individually as many are of use to the community on a stand alone basis.

The way npm works, it is possible for the same module to be loaded multiple times from different installed packages within a project and thus if they have what is intended to be working data that spans all activity within the project then the integrity required of a single instance is lost.

Any module that wants to ensure that it is instantiated only once (per thread/cluster) may invoke this tool which does the following

  • ensures that this tool itself is not loaded multiple times
  • throws an Error.code === "INVALID_MODULE_NAME" if the request does not deliver a valid module id
  • detects if the named module has already been loaded and throws an Error.code === "DUPLICATE_CALL" if this is a duplicate.

Errors are logged to the console before being thrown. You can choose to handle the error or let it bubble so that the app exits. All errors have a property called .code which is a programming oriented code if your app wishes to analyse problems.

Additionally you can publish your registered modules so that you can refer to them directly without needing install or require elsewhere in your application.

Install

npm install @wider/utils_require

Basic Use

//ES
import register from "@wider/registry";
const $moduleName = "my-module-name";
register($moduleName);


// commonJS
const register = require("@wider/registry");
const $moduleName = "my-module-name";
register($moduleName);

Advanced use

With advanced use, you also provide your object that is being registered and may choose to influence where how is cached and accessed. We illustrate creating the object using require but any means may be used to create the value to be processed.

const $moduleName = "somethingOrOther";
const sOO = require($moduleName);
const $wider = register($moduleName, sOO);

The dictionary $wider will contain at least $wider.register and $wider.somethingOrOther. The $wider object will acquire new members as new objects are registerd.

If you use @wider/utils_bundler, those objects will also be registered and appear within $wider. In this case, the objects can be configured to be loaded just in time if they are actually used - thus allowing you to create a large registry but without the overheads of all the entries being loaded into memory.

You may choose to save your object in a specific section in the repository so as to maintain an orderly structure.

const $wider = register($moduleName, sOO, "sOOname"); // gives you $wider.sOOname;
const $wider = register($moduleName, sOO, "sOOname", "mySection"); // gives you $wider.mySection.sOOname;

Supplementary Properties

  • register.modulesLoaded() a markdown paragraphs like string documenting the registered modules
  • register.published() an markdown paragraphs like string documenting the top level structure of $wider