entangle.ts
Version:
A declarative, event-driven framework for orchestrating business logic in TypeScript & Node.js applications.
40 lines (39 loc) • 1.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryAether = void 0;
const events_1 = __importDefault(require("events"));
const Aether_1 = require("../Aether");
/**
* An in-memory implementation of the Aether using Node.js's native EventEmitter.
*
* This implementation is perfect for development, testing, and simple single-process applications.
*
* **Warning:** It is not recommended for horizontally-scaled production environments
* (e.g., using Node.js 'cluster' module, PM2 cluster mode, or multiple servers).
* Events are bound to a single process's memory and cannot be shared across different
* processes or machines. Be aware that using it on a production environment cal also lead to an increase in memory consumption.
*/
class InMemoryAether extends Aether_1.Aether {
constructor() {
super(...arguments);
this._emitter = new events_1.default();
}
on(event, callback) {
this._emitter.on(event, callback);
}
once(event, callback) {
this._emitter.once(event, callback);
}
emit(event, entanglement, ...args) {
const boson = {
payload: args,
entanglement,
timestamp: Date.now(),
};
this._emitter.emit(event, boson);
}
}
exports.InMemoryAether = InMemoryAether;