@arpinum/messaging
Version:
Simple message bus
56 lines (55 loc) • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuickBus = void 0;
class QuickBus {
constructor() {
this.handlerMap = new Map();
}
post(message) {
try {
validateArgs();
}
catch (e) {
return Promise.reject(e);
}
const handlers = this.handlerMap.get(message.type) || [];
return Promise.all(handlers.map((h) => h(message)));
function validateArgs() {
if (!message) {
throw new Error("Missing message");
}
if (!message.type) {
throw new Error("Missing message type");
}
}
}
register(type, handler) {
validateArgs();
const handlers = (this.handlerMap.get(type) || []).concat(handler);
this.handlerMap.set(type, handlers);
return () => {
return;
};
function validateArgs() {
if (!type) {
throw new Error("Missing type");
}
if (!handler) {
throw new Error("Missing handler");
}
if (handler.constructor.name !== "Function") {
throw new Error("Handler must be a function");
}
}
}
handlerCount() {
return 0;
}
postAll() {
return Promise.resolve([]);
}
unregisterAll() {
return;
}
}
exports.QuickBus = QuickBus;