From b831dfbfde63610823514f51d513e2480dff74e9 Mon Sep 17 00:00:00 2001 From: Gabriel Lebec Date: Thu, 31 May 2018 12:47:42 -0400 Subject: [PATCH] fix: remove `close` listener on graceful end (#29) --- lib/volleyball.js | 12 ++++++++---- src/volleyball.js | 9 +++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/volleyball.js b/lib/volleyball.js index 2df52d2..e22441f 100644 --- a/lib/volleyball.js +++ b/lib/volleyball.js @@ -26,12 +26,16 @@ function Volleyball() { time: process.hrtime() }; logReq(req, cycle); - res.on('finish', function () { - return logRes(res, cycle); - }); - res.on('close', function () { + + var handleUnexpectedClose = function handleUnexpectedClose() { return logClose(res, cycle); + }; + + res.on('finish', function () { + logRes(res, cycle); + res.removeListener('close', handleUnexpectedClose); }); + res.on('close', handleUnexpectedClose); next(); } // factory method which does not expose any of the library internals diff --git a/src/volleyball.js b/src/volleyball.js index 8055db8..539d7b1 100644 --- a/src/volleyball.js +++ b/src/volleyball.js @@ -23,8 +23,13 @@ function Volleyball(config = {}) { } logReq(req, cycle) - res.on('finish', () => logRes(res, cycle)) - res.on('close', () => logClose(res, cycle)) + + const handleUnexpectedClose = () => logClose(res, cycle) + res.on('finish', () => { + logRes(res, cycle) + res.removeListener('close', handleUnexpectedClose) + }) + res.on('close', handleUnexpectedClose) next() }