From f02e3f9890f7a3afab74a26ec4e81bce0cf95f89 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Wed, 6 Jul 2016 14:50:06 -0400 Subject: [PATCH 1/3] Add module to determine whether polling should be enabled --- lib/should-poll.js | 50 ++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- test/should-poll-test.js | 16 +++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 lib/should-poll.js create mode 100644 test/should-poll-test.js diff --git a/lib/should-poll.js b/lib/should-poll.js new file mode 100644 index 0000000..913abb8 --- /dev/null +++ b/lib/should-poll.js @@ -0,0 +1,50 @@ +'use strict'; + +var Promise = require('promise'); +var buildBundle = require('./build-bundle'); +var fs = require('fs'); +var tmp = require('tmp'); +var watchify = require('watchify'); + +function shouldPoll() { + + return new Promise(function(resolve, reject) { + var file = tmp.fileSync(); + + var bundle = buildBundle(file.name, {}); + watchify(bundle, { poll: false, delay: 0 }); + + var resolved = false; + + function decide(pollEnabled) { + bundle.close(); + file.removeCallback(); + if (!resolved) { + resolved = true; + resolve(pollEnabled); + } + } + + bundle.once('bundle', function(b) { + b.once('error', reject); + // end event is never fired without having a data + b.once('data', function() {}); + b.once('end', function() { + + bundle.once('update', function() { + decide(true); + }); + + setTimeout(decide, 1000, false); + + fs.appendFileSync(file.name, 'var a = 1;'); + }); + }); + + bundle.once('error', reject); + bundle.bundle(); + }); + +} + +module.exports = shouldPoll; diff --git a/package.json b/package.json index ddeb8b9..67fa519 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "ms": "^0.7.1", "prepare-response": "^1.1.2", "promise": "^7.0.3", + "tmp": "0.0.28", "uglify-js": "^2.4.23", "watchify": "^3.3.0" } -} \ No newline at end of file +} diff --git a/test/should-poll-test.js b/test/should-poll-test.js new file mode 100644 index 0000000..cf884c3 --- /dev/null +++ b/test/should-poll-test.js @@ -0,0 +1,16 @@ +var shouldPoll = require('../lib/should-poll.js'); +var assert = require('assert'); + +describe('should-poll', function() { + it('should be a function', function() { + assert.equal(typeof shouldPoll, 'function'); + }); + it('should resolve to true or false', function(done) { + shouldPoll() + .then(function(pollingEnabled) { + assert(typeof pollingEnabled === 'boolean'); + }) + .then(done) + .catch(done); + }); +}); From 5544235803320b4f251b5db18b39cb18c4f64be6 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Wed, 6 Jul 2016 15:16:48 -0400 Subject: [PATCH 2/3] Using should-poll to determine polling --- lib/build-response.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/build-response.js b/lib/build-response.js index 6e047b1..4795850 100644 --- a/lib/build-response.js +++ b/lib/build-response.js @@ -5,6 +5,7 @@ var prepare = require('prepare-response'); var uglify = require('uglify-js'); var watchify = require('watchify'); var buildBundle = require('./build-bundle'); +var shouldPoll = require('./should-poll.js'); module.exports = function send(path, options) { var bundle = buildBundle(path, options); @@ -18,21 +19,25 @@ module.exports = function send(path, options) { } else if (options.cache === 'dynamic') { var response, resolve; var updatingTimeout; - bundle = watchify(bundle, {poll: true, delay: 0}); - bundle.on('update', function () { - if (resolve) { - clearTimeout(updatingTimeout); - } else { - response = new Promise(function (_resolve) { - resolve = _resolve; - }); - } - updatingTimeout = setTimeout(function rebuild() { - resolve(getResponse(bundle, options)); - resolve = undefined; - }, 600); + response = shouldPoll() + .then(function(poll) { + bundle = watchify(bundle, {poll: poll, delay: 0}); + bundle.on('update', function () { + if (resolve) { + clearTimeout(updatingTimeout); + } else { + response = new Promise(function (_resolve) { + resolve = _resolve; + }); + } + updatingTimeout = setTimeout(function rebuild() { + resolve(getResponse(bundle, options)); + resolve = undefined; + }, 600); + }); + return getResponse(bundle, options); }); - response = Promise.resolve(getResponse(bundle, options)); + return { send: function (req, res, next) { response.done(function (response) { response.send(req, res, next); }, next); From cb2f5d139894153ffd1f5b87cb8cd1d3d81e7a4f Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Wed, 6 Jul 2016 15:18:57 -0400 Subject: [PATCH 3/3] Fix potential error on dispose() if watchify was not yet initialized --- lib/build-response.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/build-response.js b/lib/build-response.js index 4795850..27d877b 100644 --- a/lib/build-response.js +++ b/lib/build-response.js @@ -19,7 +19,8 @@ module.exports = function send(path, options) { } else if (options.cache === 'dynamic') { var response, resolve; var updatingTimeout; - response = shouldPoll() + + var watchifyBundle = shouldPoll() .then(function(poll) { bundle = watchify(bundle, {poll: poll, delay: 0}); bundle.on('update', function () { @@ -35,6 +36,10 @@ module.exports = function send(path, options) { resolve = undefined; }, 600); }); + return bundle; + }); + + response = watchifyBundle.then(function(bundle) { return getResponse(bundle, options); }); @@ -43,7 +48,9 @@ module.exports = function send(path, options) { response.done(function (response) { response.send(req, res, next); }, next); }, dispose: function () { - bundle.close(); + watchifyBundle.then(function(bundle) { + bundle.close(); + }); } }; } else {