From b487bbd41ec4cb3b4e99190dcd42e35ae8b858b6 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Wed, 3 Apr 2019 10:10:49 -0700 Subject: [PATCH] feat: Allow to skip sending service events (#1270) --- packages/feathers/lib/events.js | 2 +- packages/feathers/test/events.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/feathers/lib/events.js b/packages/feathers/lib/events.js index 08a1350ea7..96e9e75b3c 100644 --- a/packages/feathers/lib/events.js +++ b/packages/feathers/lib/events.js @@ -6,7 +6,7 @@ const Proto = require('uberproto'); const eventHook = exports.eventHook = function eventHook () { return function (hook) { const { app, service } = hook; - const eventName = app.eventMappings[hook.method]; + const eventName = hook.event === null ? hook.event : app.eventMappings[hook.method]; const isHookEvent = service._hookEvents && service._hookEvents.indexOf(eventName) !== -1; // If this event is not being sent yet and we are not in an error hook diff --git a/packages/feathers/test/events.test.js b/packages/feathers/test/events.test.js index 957e86f9dc..652529a1de 100644 --- a/packages/feathers/test/events.test.js +++ b/packages/feathers/test/events.test.js @@ -56,6 +56,32 @@ describe('Service events', () => { service.create({ message: 'Hello' }); }); + it('allows to skip event emitting', done => { + const app = feathers().use('/creator', { + create (data) { + return Promise.resolve(data); + } + }); + + const service = app.service('creator'); + + service.hooks({ + before: { + create (context) { + context.event = null; + + return context; + } + } + }); + + service.on('created', data => { + done(new Error('Should never get here')); + }); + + service.create({ message: 'Hello' }).then(() => done()); + }); + it('.update and updated', done => { const app = feathers().use('/creator', { update (id, data) {