Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

mods

Module management system for Node.js that provides dynamic loading, unloading, and hot-reloading capabilities for your application modules.

Features

  • 🔄 Dynamic module loading and unloading
  • 🔍 Automatic dependency tracking
  • 🔥 Hot-reloading support
  • 📦 Module lifecycle management
  • 🧩 Context-aware module initialization
  • 📡 Event emitter capabilities for inter-module communication
  • ⏱️ Automatic timeout and interval management
  • 🧹 Automatic cleanup on process exit and error handling

Installation

npm install mods

Usage

Basic Setup

constmods=require('mods');// Initialize with contextconstctx={// Your application context};// Autoload all modules from the mods directorymods.autoload(ctx);

Loading Modules

// Load a single moduleconstmodule=awaitmods.load('moduleName',ctx);// Load multiple modulesconstmodules=awaitmods.load(['module1','module2'],ctx);

Unloading Modules

// Unload a single moduleawaitmods.unload('moduleName');// Unload multiple modulesawaitmods.unload(['module1','module2']);// Unload all modulesawaitmods.unload();

Reloading Modules

// Reload a single moduleawaitmods.reload('moduleName',ctx);// Reload multiple modulesawaitmods.reload(['module1','module2'],ctx);// Reload all loaded modulesawaitmods.reload(null,ctx);

Module Communication

Each module is an EventEmitter instance, so you call use it for exchanging events between modules. All event listeners will be removed automatically when module unloads.

// Inside a modulethis.emit('eventName',data);// Listen for eventsthis.on('eventName',(data)=>{// Handle event});// Broadcast events to all modulesthis.broadcast('eventName',data);

Alternatively, you can directly call methods on other modules:

this.mods.loaded.otherModule.someMethod(data);

The only thing to remember here: you should never store a reference to modules (this.mods.loaded.otherModule in this example), because they will become stale after reload.

Timeout and Interval Management

There are also wrappers for setTimeout and setInterval functions which are automatically cleared on module unload.

// Set a timeout that will be automatically cleared on unloadthis.setTimeout(()=>{// Your code},1000);// Set an interval that will be automatically cleared on unloadthis.setInterval(()=>{// Your code},1000);

Module Structure

Modules should be placed in the mods directory and follow this structure:

// mods/example.jsmodule.exports=asyncfunction(ctx){// Module initialization code, can be asyncthis.someMethod=()=>{// You can declare methods here to make them available from other modules}// Return cleanup functionreturnasync()=>{// Cleanup code};};

How It Works

  1. Module Loading: When a module is loaded, it receives a context object and returns a cleanup function.
  2. Dependency Tracking: The system automatically tracks module dependencies through a graph system.
  3. Event System: Modules can communicate through events using the built-in event emitter.
  4. Resource Management: Timeouts and intervals are automatically managed and cleaned up.
  5. Hot Reloading: Modules can be reloaded without restarting the application.
  6. Cleanup: When unloading, the cleanup function is called to properly dispose of resources.
  7. Process Management: Automatic cleanup on process exit and error handling.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages