From 12ee4ac6b79ffd14e6a626724db427530eee5147 Mon Sep 17 00:00:00 2001 From: jseto Date: Fri, 9 Jan 2015 22:18:41 +0100 Subject: [PATCH 1/3] create add method --- lib/index.js | 19 +++++++++++++------ test/fixture2/index2.html | 11 +++++++++++ test/fixture2/main.css | 3 +++ test/fixture2/main.js | 1 + test/index.js | 9 +++++++++ 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 test/fixture2/index2.html create mode 100644 test/fixture2/main.css create mode 100644 test/fixture2/main.js diff --git a/lib/index.js b/lib/index.js index 81c0966..5115465 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,5 @@ +'use strict'; + var sendevent = require('sendevent') , inject = require('./inject') , serve = require('./serve') @@ -18,12 +20,14 @@ module.exports = function(root, opts) { var fn = stack() fn.reload = function() {} + var watcher = filewatcher() // bypass in production var bypass = opts.bypass if (bypass === undefined) bypass = process.env.NODE_ENV == 'production' if (!bypass) { + // the prefix under which the eventstream and the client is exposed var prefix = opts.prefix || '/instant' @@ -42,10 +46,11 @@ module.exports = function(root, opts) { if (typeof ev == 'string') ev = { url: ev } if (ev) events.broadcast(ev) } + } - if (root && opts.watch !== false) { + fn.add = function( new_root ) { + if (new_root && opts.watch !== false) { var urlsByFile = {} - , watcher = filewatcher() // when a file is modifed tell all clients to reload it watcher.on('change', function(file) { @@ -66,11 +71,13 @@ module.exports = function(root, opts) { }} }) } - } - if (root) { - fn.use(serve(root, opts)) + if (new_root) { + fn.use(serve(new_root, opts)) + } + return fn; } - return fn + return fn.add(root); + } diff --git a/test/fixture2/index2.html b/test/fixture2/index2.html new file mode 100644 index 0000000..f92c3e4 --- /dev/null +++ b/test/fixture2/index2.html @@ -0,0 +1,11 @@ + + + + + + + + +

Hello

+ + \ No newline at end of file diff --git a/test/fixture2/main.css b/test/fixture2/main.css new file mode 100644 index 0000000..937a3c0 --- /dev/null +++ b/test/fixture2/main.css @@ -0,0 +1,3 @@ +html { + background: silver +} \ No newline at end of file diff --git a/test/fixture2/main.js b/test/fixture2/main.js new file mode 100644 index 0000000..57d2a94 --- /dev/null +++ b/test/fixture2/main.js @@ -0,0 +1 @@ +console.log('hello') \ No newline at end of file diff --git a/test/index.js b/test/index.js index ffde149..ab421d8 100644 --- a/test/index.js +++ b/test/index.js @@ -1,3 +1,4 @@ +'use strict'; /* global describe, it */ var instant = require('..') @@ -8,6 +9,7 @@ var instant = require('..') var ins = instant(__dirname + '/fixture') , app = http.createServer(ins) + ins.add(__dirname + '/fixture2') describe('instant', function() { it('should inject the client script', function(done) { @@ -57,6 +59,13 @@ describe('instant', function() { }) }) +describe('instant add', function() { + it('should inject the client script', function(done) { + request(app) + .get('/index2.html') + .expect(/ +

Hello

+to index2 \ No newline at end of file diff --git a/test/fixture2/index2.html b/test/fixture2/index2.html index f92c3e4..fbd6c4c 100644 --- a/test/fixture2/index2.html +++ b/test/fixture2/index2.html @@ -3,9 +3,12 @@ + + -

Hello

+

Hello2

+to index \ No newline at end of file diff --git a/test/fixture2/main.js b/test/fixture2/main.js deleted file mode 100644 index 57d2a94..0000000 --- a/test/fixture2/main.js +++ /dev/null @@ -1 +0,0 @@ -console.log('hello') \ No newline at end of file diff --git a/test/fixture2/main.css b/test/fixture2/main2.css similarity index 100% rename from test/fixture2/main.css rename to test/fixture2/main2.css diff --git a/test/fixture2/main2.js b/test/fixture2/main2.js new file mode 100644 index 0000000..fea888a --- /dev/null +++ b/test/fixture2/main2.js @@ -0,0 +1 @@ +console.log('hello2') \ No newline at end of file diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..8a36ee9 --- /dev/null +++ b/test/server.js @@ -0,0 +1,20 @@ +'use strict'; +var express = require('express'); +var app = module.exports = express(); + +var instance = require('../lib/index.js'); +var instantClient = instance(); +app.use( instantClient.add( __dirname + '/fixture' ) ); +app.use( instantClient.add( __dirname + '/fixture2' ) ); + +app.start = function(port) { + // start the web server + return app.listen(port,function() { + console.log('Web server listening at: %s', port); + }); +}; + +// start the server if `$ node server.js` +if (require.main === module) { + app.start(3000); +} From 4d6dca647ccde36e38187d405f4d00f5c60c696b Mon Sep 17 00:00:00 2001 From: jseto Date: Fri, 9 Jan 2015 22:52:53 +0100 Subject: [PATCH 3/3] extract code out of add method feature create add method finished fixes #6 --- lib/index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5115465..ae5711f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -21,6 +21,7 @@ module.exports = function(root, opts) { var fn = stack() fn.reload = function() {} var watcher = filewatcher() + var re; // bypass in production var bypass = opts.bypass @@ -46,21 +47,20 @@ module.exports = function(root, opts) { if (typeof ev == 'string') ev = { url: ev } if (ev) events.broadcast(ev) } - } - - fn.add = function( new_root ) { - if (new_root && opts.watch !== false) { - var urlsByFile = {} + var urlsByFile = {} - // when a file is modifed tell all clients to reload it - watcher.on('change', function(file) { - fn.reload(urlsByFile[file]) - }) + // when a file is modifed tell all clients to reload it + watcher.on('change', function(file) { + fn.reload(urlsByFile[file]) + }) - // build a RegExp to match all watched file extensions - var exts = opts.watch || ['html', 'js', 'css'] - , re = new RegExp('\\.(' + exts.join('|') + ')$') + // build a RegExp to match all watched file extensions + var exts = opts.watch || ['html', 'js', 'css'] + re = new RegExp('\\.(' + exts.join('|') + ')$') + } + fn.add = function( new_root ) { + if ( !bypass && new_root && opts.watch !== false) { // pass an `onfile` handler that watches matching files opts = Object.create(opts, { onfile: { value: function(path, stat) {