http-delayed-response
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,4 +0,5 @@ | ||
0.0.3 / March 4 2014 | ||
0.0.4 / March 4 2014 | ||
======================== | ||
* minor update to "stop", making sure it always get called | ||
* handle response events to make sure "stop" always get called | ||
* improved handling of timers | ||
@@ -5,0 +6,0 @@ 0.0.2 / February 20 2014 |
36
index.js
@@ -29,2 +29,3 @@ var stream = require('stream'); | ||
this.next = next; | ||
this.timers = {}; | ||
@@ -35,5 +36,7 @@ // if request is aborted, end the response immediately | ||
}); | ||
// make sure timers stop if response is closed | ||
// make sure timers stop if response is ended or closed | ||
res.on('close', function () { | ||
delayed.stop(); | ||
}).on('finish', function () { | ||
delayed.stop(); | ||
}); | ||
@@ -67,3 +70,3 @@ | ||
if (timeout) { | ||
this.timeout = setTimeout(function () { | ||
this.timers.timeout = setTimeout(function () { | ||
// timeout implies status is unknown, set HTTP Accepted status | ||
@@ -101,5 +104,5 @@ delayed.res.statusCode = 202; | ||
// start the polling timer | ||
setTimeout(function () { | ||
delayed.pollingTimer = setInterval(heartbeat.bind(delayed), interval); | ||
// start the polling and initial delay timers | ||
this.timers.initialDelay = setTimeout(function () { | ||
delayed.timers.poll = setInterval(heartbeat.bind(delayed), interval); | ||
}, initialDelay); | ||
@@ -110,3 +113,3 @@ this.started = true; | ||
if (timeout) { | ||
this.timeout = setTimeout(function () { | ||
this.timers.timeout = setTimeout(function () { | ||
delayed.end(new TimeoutError('timeout occurred')); | ||
@@ -148,8 +151,2 @@ }, timeout); | ||
// prevent double processing | ||
if (this.stopped) { | ||
console.warn('DelayedResponse.end has been called twice!'); | ||
return; | ||
} | ||
// detect a promise-like object | ||
@@ -168,3 +165,5 @@ if (err && 'then' in err && typeof err.then === 'function') { | ||
this.stop(); | ||
// prevent double processing | ||
if (this.ended) return console.warn('DelayedResponse.end has been called twice!'); | ||
this.ended = true; | ||
@@ -207,10 +206,13 @@ // restore socket buffering | ||
DelayedResponse.prototype.stop = function () { | ||
// stop initial delay | ||
clearTimeout(this.timers.initialDelay); | ||
this.timers.initialDelay = null; | ||
// stop polling | ||
clearInterval(this.pollingTimer); | ||
this.pollingTimer = null; | ||
clearInterval(this.timers.poll); | ||
this.timers.poll = null; | ||
// stop timeout | ||
clearTimeout(this.timeout); | ||
this.timeout = null; | ||
clearTimeout(this.timers.timeout); | ||
this.timers.timeout = null; | ||
}; | ||
module.exports = DelayedResponse; |
{ | ||
"name": "http-delayed-response", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Nicolas Mercier", |
@@ -106,7 +106,6 @@ var express = require('express'); | ||
delayed.on('cancel', function () { | ||
done(); | ||
res.end(); | ||
}).start(50, 0, 100); | ||
res.end(); | ||
}); | ||
request(app).get('/').expect(202).end(function () {}); | ||
request(app).get('/').expect(202, done); | ||
}); | ||
@@ -129,6 +128,11 @@ it('should throw when started twice', function (done) { | ||
var app = express(); | ||
var stopping = false; | ||
app.use(function (req, res) { | ||
var delayed = new DelayedResponse(req, res); | ||
delayed.start(10); | ||
delayed.on('poll', function () { | ||
if (stopping) throw new Error('should have stopped polling'); | ||
}); | ||
delayed.start(20); | ||
setTimeout(function () { | ||
stopping = true; | ||
delayed.stop(); | ||
@@ -142,5 +146,8 @@ res.end(); | ||
var app = express(); | ||
var aborting = false; | ||
app.use(function (req, res) { | ||
var delayed = new DelayedResponse(req, res); | ||
delayed.on('abort', function () { | ||
delayed.on('poll', function () { | ||
if (aborting) throw new Error('should have stopped polling'); | ||
}).on('abort', function () { | ||
res.end(); | ||
@@ -152,5 +159,22 @@ done(); | ||
setTimeout(function () { | ||
aborting = true; | ||
req.abort(); | ||
}, 100); | ||
}); | ||
it('should stop when response is ended', function (done) { | ||
var app = express(); | ||
var ending = false; | ||
app.use(function (req, res) { | ||
var delayed = new DelayedResponse(req, res); | ||
delayed.on('poll', function () { | ||
if (ending) throw new Error('should have stopped polling'); | ||
}).start(); | ||
setTimeout(function () { | ||
ending = true; | ||
res.end(); | ||
}, 100); | ||
}); | ||
request(app).get('/').expect(202, done); | ||
}); | ||
}); | ||
@@ -323,6 +347,3 @@ describe('.end(err, data)', function () { | ||
}); | ||
describe('with promises', function () { | ||
}); | ||
}); | ||
}); |
34902
522