2021-09-17 16:07:27 +03:00
|
|
|
const EventEmitter = require('events').EventEmitter;
|
2022-12-13 13:28:02 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-09-17 16:07:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @typedef {import('./').ConstructorOf<T>} ConstructorOf<T>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Data
|
|
|
|
* @typedef {object} IEvent
|
|
|
|
* @prop {Date} timestamp
|
|
|
|
* @prop {Data} data
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DomainEvents {
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @type EventEmitter
|
|
|
|
*/
|
|
|
|
static ee = new EventEmitter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Data
|
|
|
|
* @template {IEvent<Data>} EventClass
|
|
|
|
* @param {ConstructorOf<EventClass>} Event
|
2022-12-13 13:28:02 +03:00
|
|
|
* @param {(event: EventClass) => Promise<void> | void} handler
|
2021-09-17 16:07:27 +03:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
static subscribe(Event, handler) {
|
2022-12-13 13:28:02 +03:00
|
|
|
DomainEvents.ee.on(Event.name, async (event) => {
|
|
|
|
try {
|
|
|
|
await handler(event);
|
|
|
|
} catch (e) {
|
|
|
|
logging.error('Unhandled error in event handler for event: ' + Event.name);
|
|
|
|
logging.error(e);
|
|
|
|
}
|
|
|
|
});
|
2021-09-17 16:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template Data
|
|
|
|
* @param {IEvent<Data>} event
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
static dispatch(event) {
|
2022-11-29 13:15:19 +03:00
|
|
|
DomainEvents.dispatchRaw(event.constructor.name, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch an event in case you don't have an instance of the event class, but you do have the event name and event data.
|
|
|
|
* @template Data
|
|
|
|
* @param {string} name
|
|
|
|
* @param {Data} data
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
static dispatchRaw(name, data) {
|
|
|
|
DomainEvents.ee.emit(name, data);
|
2021-09-17 16:07:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DomainEvents;
|