From dab0d4686a6a1f4f34b1f4124d64934d4466b8c8 Mon Sep 17 00:00:00 2001 From: HerrHase Date: Sun, 16 Jan 2022 23:13:04 +0100 Subject: [PATCH] change eventEmitter to decorate --- packages/server/bootstrap.js | 9 +++++---- packages/server/http/api/webhook.js | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/server/bootstrap.js b/packages/server/bootstrap.js index 5e66333..ae1621f 100644 --- a/packages/server/bootstrap.js +++ b/packages/server/bootstrap.js @@ -18,7 +18,8 @@ import { EventEmitter } from 'events' // create eventemitter for sending messages // @TODO find a better solution, was only to use it with online event, but not working as expected -const eventEmitter = new EventEmitter() + +server.decorate('eventEmitter', new EventEmitter()) const xmpp = client({ service: process.env.XMPP_SERVICE, @@ -35,7 +36,7 @@ xmpp.on('online', (address) => { console.log('connected to ' + address) - eventEmitter.on('send-message', async (data) => + server.eventEmitter.on('send-message', async (data) => { // Sends a chat message to itself const message = xml( @@ -57,6 +58,7 @@ xmpp.on('offline', (error) => { xmpp.start().catch(console.error) + /** * add routes * @@ -66,8 +68,7 @@ import webhookHttp from './http/api/webhook.js' server .register(webhookHttp, { - 'prefix': '/api/webhook', - 'eventEmitter': eventEmitter // @TODO shift to a more fastify-way + 'prefix': '/api/webhook' }) export default server \ No newline at end of file diff --git a/packages/server/http/api/webhook.js b/packages/server/http/api/webhook.js index e17f5a3..298255b 100644 --- a/packages/server/http/api/webhook.js +++ b/packages/server/http/api/webhook.js @@ -40,7 +40,7 @@ export default async function(fastify, opts) const result = parser.run() // send event for send xmpp - opts.eventEmitter.emit('send-message', { + fastify.eventEmitter.emit('send-message', { 'message': result })