Skip to content

Commit

Permalink
Merge pull request #132 from feathersjs/socket-init-131
Browse files Browse the repository at this point in the history
Run Socket configurations before service setup (#131)
  • Loading branch information
daffl committed Apr 10, 2015
2 parents 26e2f86 + 2061a95 commit ee3f158
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
20 changes: 10 additions & 10 deletions lib/providers/socket/primus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions lib/providers/socket/socketio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -39,11 +44,6 @@ module.exports = function (config) {
}
});

if (typeof config === 'function') {
debug('Calling SocketIO configuration function');
config.call(this, io);
}

return result;
}
}, app);
Expand Down
25 changes: 25 additions & 0 deletions test/providers/primus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
23 changes: 23 additions & 0 deletions test/providers/socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit ee3f158

Please sign in to comment.