From 2061a95c43b93337d43201548a79cf7b14d4c7db Mon Sep 17 00:00:00 2001 From: David Luecke Date: Thu, 9 Apr 2015 09:46:41 -0600 Subject: [PATCH] Run Socket configurations before service setup (#131) --- lib/providers/socket/primus.js | 20 ++++++++++---------- lib/providers/socket/socketio.js | 16 ++++++++-------- test/providers/primus.test.js | 25 +++++++++++++++++++++++++ test/providers/socketio.test.js | 23 +++++++++++++++++++++++ 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/lib/providers/socket/primus.js b/lib/providers/socket/primus.js index 3300a893ee..f0e931c648 100644 --- a/lib/providers/socket/primus.js +++ b/lib/providers/socket/primus.js @@ -17,16 +17,23 @@ module.exports = function(config, configurer) { service: commons.service, setup: function(server) { - var result = this._super.apply(this, arguments); - if (this.disabled('feathers primus')) { - return result; + return this._super.apply(this, arguments); } debug('Setting up Primus'); var primus = this.primus = new Primus(server, config); + primus.use('emitter', Emitter); + + if (typeof configurer === 'function') { + debug('Calling Primus configuration function'); + configurer.call(this, primus); + } + + var result = this._super.apply(this, arguments); + commons.setup.call(this, { method: 'send', connection: function() { @@ -40,13 +47,6 @@ module.exports = function(config, configurer) { } }); - primus.use('emitter', Emitter); - - if (typeof configurer === 'function') { - debug('Calling Primus configuration function'); - configurer.call(this, primus); - } - return result; } }, app); diff --git a/lib/providers/socket/socketio.js b/lib/providers/socket/socketio.js index 2bfd6e181c..e8fc59b806 100644 --- a/lib/providers/socket/socketio.js +++ b/lib/providers/socket/socketio.js @@ -16,14 +16,19 @@ module.exports = function (config) { service: commons.service, setup: function (server) { - var result = this._super.apply(this, arguments); - if (this.disabled('feathers socketio')) { - return result; + return this._super.apply(this, arguments); } var io = this.io = socketio.listen(server); + if (typeof config === 'function') { + debug('Calling SocketIO configuration function'); + config.call(this, io); + } + + var result = this._super.apply(this, arguments); + debug('Setting up SocketIO'); commons.setup.call(this, { @@ -39,11 +44,6 @@ module.exports = function (config) { } }); - if (typeof config === 'function') { - debug('Calling SocketIO configuration function'); - config.call(this, io); - } - return result; } }, app); diff --git a/test/providers/primus.test.js b/test/providers/primus.test.js index 70c2f30c68..c118f4cdb9 100644 --- a/test/providers/primus.test.js +++ b/test/providers/primus.test.js @@ -39,6 +39,31 @@ describe('Primus provider', function () { server.close(done); }); + it('runs primus before setup (#131)', function(done) { + var counter = 0; + var app = feathers() + .configure(feathers.primus({ + transformer: 'websockets' + }, function() { + assert.equal(counter, 0); + counter++; + })) + .use('/todos', { + find: function(params, callback) { + callback(null, []); + }, + setup: function(app) { + assert.ok(app.primus); + assert.equal(counter, 1, 'SocketIO configuration ran first'); + } + }); + + var srv = app.listen(9119); + srv.on('listening', function() { + srv.close(done); + }); + }); + it('Passes handshake as service parameters.', function(done) { var service = app.service('todo'); var old = { diff --git a/test/providers/socketio.test.js b/test/providers/socketio.test.js index 70b57768cf..bb1d8e3fc3 100644 --- a/test/providers/socketio.test.js +++ b/test/providers/socketio.test.js @@ -38,6 +38,29 @@ describe('SocketIO provider', function () { server.close(done); }); + it('runs io before setup (#131)', function(done) { + var counter = 0; + var app = feathers() + .configure(feathers.socketio(function() { + assert.equal(counter, 0); + counter++; + })) + .use('/todos', { + find: function(params, callback) { + callback(null, []); + }, + setup: function(app) { + assert.ok(app.io); + assert.equal(counter, 1, 'SocketIO configuration ran first'); + } + }); + + var srv = app.listen(8887); + srv.on('listening', function() { + srv.close(done); + }); + }); + it('passes handshake as service parameters', function(done) { var service = app.service('todo'); var old = {