Skip to content

Foundation42/module-system

Repository files navigation

Module System

A modular application framework with dynamic module loading and inter-module communication built on the tuple-store state management library.

Features

  • Dynamic module loading and lifecycle management
  • Module dependency resolution
  • Inter-module communication through shared services
  • Isolated state management for each module
  • Observable state changes through tuple-store
  • File system or in-memory module repositories

Installation

bun add github:Foundation42/module-system

Basic Usage

import { createModuleSystem } from 'module-system';

// Create a module system
const moduleSystem = createModuleSystem({
  type: "file-system",      // Load modules from the file system
  modulesDir: "./modules",  // Directory containing modules
  enableJournaling: true    // Enable state change journaling
});

// Initialize the module system
await moduleSystem.repository.refresh();

// List available modules
const modules = moduleSystem.repository.listModules();
console.log(`Found ${modules.length} modules`);

// Load modules (handles dependencies automatically)
await moduleSystem.manager.loadModules(['core-module', 'ui-module']);

Creating Modules

Each module should export a createModule function that returns a Module instance:

import { Module, ModuleContext, ModuleManifest, ModuleState } from 'module-system';

export function createModule(manifest: ModuleManifest): Module {
  return {
    // Store the module state
    state: ModuleState.REGISTERED,
    
    // Initialize the module
    async initialize(context: ModuleContext): Promise<boolean> {
      // Access module-specific store
      await context.store.set('initialized', true);
      
      // Register a service
      context.services.registerService({
        id: 'myService',
        implementation: {
          doSomething: () => 'result'
        },
        version: '1.0.0'
      });
      
      return true;
    },
    
    // Stop the module
    async stop(): Promise<boolean> {
      // Cleanup
      return true;
    },
    
    // Get module state
    getState(): ModuleState {
      return this.state;
    },
    
    // Get module manifest
    getManifest(): ModuleManifest {
      return manifest;
    },
    
    // Expose public API
    getAPI(): Record<string, any> {
      return {
        getSomething: () => 'something'
      };
    }
  };
}

Module Manifest

Each module should have a manifest.json file:

{
  "id": "my-module",
  "name": "My Module",
  "description": "A sample module",
  "version": "1.0.0",
  "entryPoint": "index.js",
  "capabilities": ["feature1", "feature2"],
  "slots": ["main-panel"],
  "dependencies": {
    "core-module": "^1.0.0"
  }
}

License

MIT

About

A modular application framework with dynamic module loading and inter-module communication

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published