From f24629a8e1f2e4148286c1455e40725301053e3e Mon Sep 17 00:00:00 2001 From: Giancarlo Anemone Date: Wed, 13 Dec 2017 17:19:57 -0800 Subject: [PATCH] Fix bug where mappers were run before batch was flushed --- src/__tests__/__node__/index.js | 15 ++++++++++++--- src/server.js | 5 +++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/__tests__/__node__/index.js b/src/__tests__/__node__/index.js index bb5c81e..fbc834a 100644 --- a/src/__tests__/__node__/index.js +++ b/src/__tests__/__node__/index.js @@ -152,20 +152,28 @@ test('Server EventEmitter batching', async t => { app.plugin(() => async (ctx, next) => { const emitter = Emitter.of(ctx); - emitter.on('test-post-await', ({x}) => { + emitter.on('test-post-await', ({x, lol}) => { t.equals(x, 1, 'payload is correct'); + t.ok(lol, 'runs mappers'); flags.postawait = true; }); await next(); emitter.emit('test-post-await', {x: 1}); + emitter.map(payload => { + return { + ...payload, + lol: true, + }; + }); t.notOk(emitter.flushed, 'waits to flush'); t.notOk(flags.postawait, 'batches post await next events'); }); app.plugin(() => async (ctx, next) => { const emitter = Emitter.of(ctx); - emitter.on('test-post-end', ({x}) => { + emitter.on('test-post-end', ({x, lol}) => { t.equals(x, 1, 'payload is correct'); + t.ok(lol, 'runs mappers'); flags.postend = true; }); ctx.timing.end.then(() => { @@ -178,8 +186,9 @@ test('Server EventEmitter batching', async t => { app.plugin(() => async (ctx, next) => { const emitter = Emitter.of(ctx); - emitter.on('test-timeout', ({x}) => { + emitter.on('test-timeout', ({x, lol}) => { t.equals(x, 1, 'payload is correct'); + t.ok(lol, 'runs mappers'); flags.timeout = true; }); setTimeout(() => { diff --git a/src/server.js b/src/server.js index 1fbb6d4..29d7965 100644 --- a/src/server.js +++ b/src/server.js @@ -40,11 +40,10 @@ export default function() { } } emit(type, payload, ctx) { - payload = super.mapEvent(type, payload, this.ctx); if (!this.ctx) { + payload = super.mapEvent(type, payload, this.ctx); super.handleEvent(type, payload, ctx); } else { - payload = this.parent.mapEvent(type, payload, this.ctx); // this logic exists to manage ensuring we send events after the batch if (this.flushed) { this.handleBatchedEvent({type, payload}); @@ -54,6 +53,8 @@ export default function() { } } handleBatchedEvent({type, payload}) { + payload = super.mapEvent(type, payload, this.ctx); + payload = this.parent.mapEvent(type, payload, this.ctx); super.handleEvent(type, payload, this.ctx); this.parent.handleEvent(type, payload, this.ctx); }