node-mediator
node-mediator implements the mediator design pattern with events. It supports creating decoupled components(called colleagues) which all interact through one component(called mediator) through events. If you have large projects with a lot of dependencies between components, this is the library for you.
Why should you use this library?
- When you have many components communicating with one another, using a mediator with events will make your development times quicker and building new fetures easier and faster as everything is decoupled.
- Easier to follow the S.O.L.I.D principles as you have fewer intercomponent dependencies and a better view of the hierarchy of your components.
- Simpler to test your components.
- Ensures strictness between inter-component communication.
- Uses eventemitter2 library by default(it is overridable if you implement the Emitter interface and pass it as a 2nd argument to the mediator initialization)
Installation
npm install node-mediator -Soryarn add node-mediator
Examples
you can find an example of usage in the examples folder found here
relations.ts
used by the mediator to restrict inter-component commumnication
// tester, uploader and logger are our class that extended the Colleague abstract class// each colleague implementation has on and emit objects which accept strings of events names as keys and booleans as values.// in our case it means that tester is allowed to fire a test event and receive a test event, etc.
Logger.ts
// our logger extends Colleague, therefore has .on, .emit, .emitAsync and .register on its prototype.// we define our methods here and we will register them later. If you want to emit an event to some other component you simply use the this.emit or this.emitAsync if you need to await the answer.
Main.ts
new Main.run
License
MIT