diff --git a/lib/processMultipart.js b/lib/processMultipart.js index 990639b..5f3d24c 100644 --- a/lib/processMultipart.js +++ b/lib/processMultipart.js @@ -111,7 +111,7 @@ module.exports = (options, req, res, next) => { }); file.on('data', (data) => { - uploadTimer.set(); // Refresh upload timer each time new data chunk came. + uploadTimer.refresh(); // Refresh upload timer each time new data chunk came. dataHandler(data); // Handle new piece of data. }); diff --git a/lib/uploadtimer.js b/lib/uploadtimer.js index d29ab46..007e517 100644 --- a/lib/uploadtimer.js +++ b/lib/uploadtimer.js @@ -1,24 +1,45 @@ class UploadTimer { /** * @constructor - * @param {number} timeout - timer timeout in msecs. + * @param {number} timeout - timer timeout in msecs. * @param {Function} callback - callback to run when timeout reached. */ - constructor(timeout = 0, callback = () => {}) { - this.timeout = timeout; - this.callback = callback; + constructor(timeout, callback) { + this.timeout = timeout || 0; + this.callback = callback || (() => {}); this.timer = null; } + /** + * Sets the timer. + * Initializes & starts the timer. + * @returns {boolean} True if timer has been set. + */ + set() { + if (this.timer || !this.timeout) return false; + this.timer = setTimeout(() => { + this.clear(); + this.callback(); + }, this.timeout); + return true; + } + + /** + * Clears the timer. + * If timer cleared, it has to be re-initialized again with set method. + */ clear() { clearTimeout(this.timer); } - set() { - // Do not start a timer if zero timeout or it hasn't been set. - if (!this.timeout) return false; - this.clear(); - this.timer = setTimeout(this.callback, this.timeout); + /** + * Refreshes timer. + * @returns {boolean} True if timer has been refreshed. + */ + refresh() { + // Do nothing if zero/empty timeout or timer hasn't been initialized. + if (!this.timer) return false; + this.timer.refresh(); return true; } } diff --git a/test/uploadtimer.spec.js b/test/uploadtimer.spec.js index 9dfd158..9b91ce6 100644 --- a/test/uploadtimer.spec.js +++ b/test/uploadtimer.spec.js @@ -6,13 +6,14 @@ const UploadTimer = require('../lib/uploadtimer'); describe('uploadTimer: Test UploadTimer class', () => { it('It runs a callback function after specified timeout.', (done) => { - const uploadTimer = new UploadTimer(1000, done); + const uploadTimer = new UploadTimer(500, done); uploadTimer.set(); }); it('set method returns true if timeout specified.', () => { - const uploadTimer = new UploadTimer(1000); + const uploadTimer = new UploadTimer(500); assert.equal(uploadTimer.set(), true); + uploadTimer.clear(); }); it('set method returns false if timeout has not specified.', () => { @@ -25,4 +26,16 @@ describe('uploadTimer: Test UploadTimer class', () => { assert.equal(uploadTimer.set(), false); }); + it('set method returns false if timer allready set.', () => { + const uploadTimer = new UploadTimer(500); + uploadTimer.set(); + assert.equal(uploadTimer.set(), false); + uploadTimer.clear(); + }); + + it('refresh method returns false if timer has not been set/initialized.', () => { + const uploadTimer = new UploadTimer(500); + assert.equal(uploadTimer.refresh(), false); + }); + });