Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Fix bug where mappers were run before batch was flushed #26

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/__tests__/__node__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems strange that we want to pass in a falsey ctx here?

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});
Expand All @@ -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);
}
Expand Down