@codescouts/events
Advanced tools
Comparing version 1.0.2 to 1.0.3
import { DomainEvent } from "./DomainEvent"; | ||
type Handler<E extends DomainEvent> = (event: E) => void; | ||
export declare class DomainEvents { | ||
@@ -6,5 +7,7 @@ private constructor(); | ||
static clear(): void; | ||
static register<T extends DomainEvent>(callback: (event: T) => void, eventClassName: string): void; | ||
static dispatch(event: DomainEvent): void; | ||
static remove<E extends DomainEvent>(handler: Handler<E>): void; | ||
static register<E extends DomainEvent>(handler: Handler<E>, eventClassName: string): void; | ||
static dispatch<E extends DomainEvent>(event: E): void; | ||
} | ||
export {}; | ||
//# sourceMappingURL=DomainEvents.d.ts.map |
@@ -13,2 +13,18 @@ "use strict"; | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -22,25 +38,42 @@ exports.DomainEvents = void 0; | ||
}; | ||
DomainEvents.register = function (callback, eventClassName) { | ||
DomainEvents.remove = function (handler) { | ||
var e_1, _a; | ||
try { | ||
for (var _b = __values(Object.entries(this.handlersMap)), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = __read(_c.value, 2), key = _d[0], value = _d[1]; | ||
if (value.includes(handler)) { | ||
this.handlersMap[key] = this.handlersMap[key].filter(function (h) { return h !== handler; }); | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}; | ||
DomainEvents.register = function (handler, eventClassName) { | ||
if (!this.handlersMap.hasOwnProperty(eventClassName)) { | ||
this.handlersMap[eventClassName] = []; | ||
} | ||
this.handlersMap[eventClassName].push(callback); | ||
this.handlersMap[eventClassName].push(handler); | ||
}; | ||
DomainEvents.dispatch = function (event) { | ||
var e_1, _a; | ||
var e_2, _a; | ||
var eventClassName = event.constructor.name; | ||
if (this.handlersMap.hasOwnProperty(eventClassName)) { | ||
var handlers = this.handlersMap[eventClassName]; | ||
try { | ||
for (var handlers_1 = __values(handlers), handlers_1_1 = handlers_1.next(); !handlers_1_1.done; handlers_1_1 = handlers_1.next()) { | ||
var handler = handlers_1_1.value; | ||
for (var _b = __values(this.handlersMap[eventClassName]), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var handler = _c.value; | ||
handler(event); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (handlers_1_1 && !handlers_1_1.done && (_a = handlers_1.return)) _a.call(handlers_1); | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
@@ -47,0 +80,0 @@ } |
import { DomainEvent } from "./DomainEvent"; | ||
import { IEventDispatcher } from "./IEventDispatcher"; | ||
export declare abstract class Handler<T extends DomainEvent> { | ||
import { Newly } from "./types"; | ||
export declare abstract class Handler<E extends DomainEvent> { | ||
protected readonly dispatcher: IEventDispatcher; | ||
constructor(event: new (...args: any[]) => T); | ||
protected abstract handle(event: T): void | Promise<any>; | ||
constructor(eventClassReference: Newly<E>); | ||
protected abstract handle(event: E): void | Promise<void>; | ||
} | ||
//# sourceMappingURL=Handler.d.ts.map |
@@ -6,7 +6,7 @@ "use strict"; | ||
var Handler = (function () { | ||
function Handler(event) { | ||
function Handler(eventClassReference) { | ||
this.dispatcher = { | ||
dispatch: function (event) { return DomainEvents_1.DomainEvents.dispatch(event); }, | ||
}; | ||
DomainEvents_1.DomainEvents.register(this.handle.bind(this), event.name); | ||
DomainEvents_1.DomainEvents.register(this.handle.bind(this), eventClassReference.name); | ||
} | ||
@@ -13,0 +13,0 @@ return Handler; |
import { DomainEvent } from "./DomainEvent"; | ||
export interface IEventDispatcher { | ||
dispatch(event: DomainEvent): void; | ||
dispatch<E extends DomainEvent>(event: E): void; | ||
} | ||
//# sourceMappingURL=IEventDispatcher.d.ts.map |
{ | ||
"name": "@codescouts/events", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./lib/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14933
29
179