UNPKG

1.23 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.eventHook = eventHook;
4exports.eventMixin = eventMixin;
5const events_1 = require("events");
6const service_1 = require("./service");
7function 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 // Send the event only if the service does not do so already (indicated in the `events` option)
13 // This is used for custom events and for client services receiving event from the server
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}
20function 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//# sourceMappingURL=events.js.map
\No newline at end of file