# modapp

> A thin framework for building completely modular web applications.

Latest version **2.7.0** (published 2024-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install modapp
pnpm add modapp
yarn add modapp
bun add modapp
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.7.0 |
| Published | 2024-02-06 |
| First published | 2017-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 286.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Samuel Jirénius |
| Maintainers | jirenius |

## Links

- npm: https://www.npmjs.com/package/modapp
- Repository: https://github.com/jirenius/modapp
- Issues: https://github.com/jirenius/modapp/issues
- npm.io page: https://npm.io/package/modapp

## Dependencies (2)

- [modapp-utils](https://npm.io/package/modapp-utils.md) ^1.8.0
- [modapp-eventbus](https://npm.io/package/modapp-eventbus.md) ^1.8.2

## Recent versions

- 2.7.0 (latest) — 2024-02-06
- 2.6.2 — 2023-11-26
- 2.6.1 — 2023-07-25
- 2.6.0 — 2022-10-31
- 2.5.0 — 2020-11-13
- 2.4.0 — 2020-06-08
- 2.3.3 — 2018-07-27
- 2.3.2 — 2018-06-24
- 2.3.1 — 2018-06-24
- 2.3.0 — 2018-06-24
- 2.2.0 — 2018-03-30
- 2.1.3 — 2018-03-04
- 2.1.2 — 2017-10-04
- 2.1.1 — 2017-06-15
- 2.1.0 — 2017-05-04
- … 5 more at https://npm.io/package/modapp/versions

## README

[![view on npm](http://img.shields.io/npm/v/modapp.svg)](https://www.npmjs.org/package/modapp)

# ModApp
A thin framework for building modular web applications.

## Installation

With npm:
```sh
npm install modapp
```

With yarn:
```sh
yarn add modapp
```

## Example

```javascript
import { App } from 'modapp';

// A module keeping track of greetings
class Greetings {
	constructor(app, params) {
		this.app = app;
		this.greetings = {};
	}

	addGreeting(lang, greeting) {
		this.greetings[lang] = greeting;
	}

	removeGreeting(lang) {
		delete this.greetlings[lang];
	}

	greet(lang) {
		let greeting = this.greetings[lang];
		console.log(greeting || "Unknown language");
	}

	dispose() {} // Nothing to dispose
}

// A module for swedish greetings
class EnglishGreeting {
	constructor(app, params) {
		this.app = app;
		this.greeting = params.greeting || "Hello";
		this.app.require(['greetings'], this._init.bind(this));
	}

	_init(module)  {
		this.module = module;
		this.module.greetings.addGreeting('english', this.greeting);
	}

	dispose() {
		this.module.greetings.removeGreeting('english');
	}
}

// Creating the app with some module configuration. These parameters may be overwritten using url queries
let app = new App({ englishGreeting: { greeting: "Hi" }});

// Bundles are usually created dynamically using tools like webpacks require context.
let bundle = {
	greetings: Greetings,
	englishGreeting: EnglishGreeting
};

app.loadBundle(bundle)
	.then(result => {
		console.log("Modules loaded: ", result);
		app.getModule('greetings').greet('english');
	});
```

## Using modapp

Modules can be loaded, deactivate, and reactivated while the application is running, allowing hotloading of new features and functionality to the application. New modules will simply hook into the application using the register/unregister methods provided (`addGreeting` and `removeGreeting` in the example).

Try [resgate-test-app](https://github.com/jirenius/resgate-test-app) to see modapp in use.

## AppModule interface

For a class to be loaded by the app, it must follow the AppModule interface.  
A module may expose any number of public methods, using camelCase as naming convention.  
All properties should be considered private, as well as any methods starting with an underscore (`_`).  
Any exposed method that allows other modules to register objects or callbacks, must come paired
with a method for unregistering.

### appModule.constructor(app, params)
The app module constructor.  
It is only in the constructor that the module may call 
*app.require(...)*.
If *app.require* is called, the module must postpone registering any objects or callbacks until
the require callback is called to prevent memory leaks in case the App cannot fulfill the requirements
and discards the app module.
(#AppModule)  

| Param | Type | Description |
| --- | --- | --- |
| app | [<code>App</code>](#App) | The app instance |
| params | <code>Object.&lt;string, \*&gt;</code> | Module parameters from the app config |

### appModule.dispose()
Disposes the app module, removing any items or callbacks previously registered within the constructor or
in the *app.require* callback.  
Once disposed, no more calls will be done to the module instance.

## Documentation

[Markdown documentation](https://github.com/jirenius/modapp/blob/master/docs/docs.md)

---
_Source: https://npm.io/package/modapp · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
