UNPKG

dino-express

Version:

DinO enabled REST framework based on express

47 lines 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObservableCommand = void 0; const dino_core_1 = require("dino-core"); const ApplicationEvent_1 = require("./ApplicationEvent"); /** * Extends Dino Command pattern definition adding observability, that's it before the command * is executed an event is generated with type `commandExecutionStarted`, when the command execution * is completed another event is generated with type `commandExecutionCompleted`, in case of error a * command is generated with type `commandExecutionFailed`. * * Custom logic is to be provided via the `doExecute` method. * The event payload can be customised using the `augmentEventPayload` method */ class ObservableCommand extends dino_core_1.Command { eventProducer; constructor({ environment, applicationContext, eventProducer }) { super({ environment, applicationContext }); this.eventProducer = eventProducer; } async execute(input) { try { this.eventProducer.send(ApplicationEvent_1.ApplicationEvent.create('commandExecutionStarted', this.augmentEventPayload('commandExecutionStarted', {}))); return await this.doExecute(input); } catch (e) { this.eventProducer.send(ApplicationEvent_1.ApplicationEvent.create('commandExecutionFailed', this.augmentEventPayload('commandExecutionFailed', {}))); throw e; } finally { this.eventProducer.send(ApplicationEvent_1.ApplicationEvent.create('commandExecutionCompleted', this.augmentEventPayload('commandExecutionCompleted', {}))); } } augmentEventPayload(_eventType, payload) { return payload; } /** * Enable lazy loading as the command will be used later in time, this prevent * loading during startup improving performances. * @returns boolean flag indicating if this Injectable should be lazy loaded */ lazy() { return true; } } exports.ObservableCommand = ObservableCommand; //# sourceMappingURL=ObservableCommand.js.map