From 0ea48245cf3bb5e6ded53250a20bf2e195a00e2e Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Sun, 24 May 2020 22:54:08 +0100 Subject: [PATCH] groundwork for pubsub --- packages/server/src/api/controllers/record.js | 4 +--- packages/server/src/app.js | 3 +++ packages/server/src/events/index.js | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 packages/server/src/events/index.js diff --git a/packages/server/src/api/controllers/record.js b/packages/server/src/api/controllers/record.js index 2cd648b6c3..53261cb8ec 100644 --- a/packages/server/src/api/controllers/record.js +++ b/packages/server/src/api/controllers/record.js @@ -43,9 +43,7 @@ exports.save = async function(ctx) { record.type = "record" const response = await db.post(record) record._rev = response.rev - // await ctx.publish(events.recordApi.save.onRecordCreated, { - // record: record, - // }) + ctx.eventPublisher.emit("RECORD_CREATED", record) ctx.body = record ctx.status = 200 diff --git a/packages/server/src/app.js b/packages/server/src/app.js index bec5f8d16e..f947738567 100644 --- a/packages/server/src/app.js +++ b/packages/server/src/app.js @@ -4,6 +4,7 @@ const logger = require("koa-pino-logger") const http = require("http") const api = require("./api") const env = require("./environment") +const eventPublisher = require("./events"); const app = new Koa() @@ -19,6 +20,8 @@ app.use( }) ) +app.context.publisher = eventPublisher; + // api routes app.use(api.routes()) diff --git a/packages/server/src/events/index.js b/packages/server/src/events/index.js new file mode 100644 index 0000000000..14e4e052e1 --- /dev/null +++ b/packages/server/src/events/index.js @@ -0,0 +1,3 @@ +const EventEmitter = require("events").EventEmitter; + +module.exports = new EventEmitter(); \ No newline at end of file