1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.eventHook = eventHook;
|
4 | exports.eventMixin = eventMixin;
|
5 | const events_1 = require("events");
|
6 | const service_1 = require("./service");
|
7 | function eventHook(context, next) {
|
8 | const { events } = (0, service_1.getServiceOptions)(context.self);
|
9 | const defaultEvent = service_1.defaultEventMap[context.method] || null;
|
10 | context.event = defaultEvent;
|
11 | return next().then(() => {
|
12 |
|
13 |
|
14 | if (typeof context.event === 'string' && !events.includes(context.event)) {
|
15 | const results = Array.isArray(context.result) ? context.result : [context.result];
|
16 | results.forEach((element) => context.self.emit(context.event, element, context));
|
17 | }
|
18 | });
|
19 | }
|
20 | function eventMixin(service) {
|
21 | const isEmitter = typeof service.on === 'function' && typeof service.emit === 'function';
|
22 | if (!isEmitter) {
|
23 | Object.assign(service, events_1.EventEmitter.prototype);
|
24 | }
|
25 | return service;
|
26 | }
|
27 |
|
\ | No newline at end of file |