Skip to content

Commit

Permalink
Merge pull request #71 from jugglinmike/fix-stop
Browse files Browse the repository at this point in the history
Completely destroy instance state in `cloak.stop`
  • Loading branch information
Greg committed Mar 24, 2014
2 parents 90375c5 + 2fb327b commit 8ddd7c8
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 20 deletions.
65 changes: 45 additions & 20 deletions src/client/cloak.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

(function() {

var removeKey = function(val, key, obj) {
delete obj[key];
};

var createCloak = function() {

var uid;
Expand All @@ -19,21 +23,44 @@
io = b;
},

/**
* Set configuration options for Cloak. These options will be applied
* immediately if Cloak has already been started (see `cloak.run`), and
* they will be referenced in all future calls to `clock.run` until
* overwritten.
*/
configure: function(configArg) {

_(configArg).forEach(function(val, key) {
if (key === 'serverEvents') {
_(val).forEach(function(eventHandler, eventName) {
cloak._on('cloak-' + eventName, eventHandler);
});
}
else if (key === 'timerEvents') {
timerEvents = val;
}
else {
config[key] = val;
}
// When specified, the `messages` option should trigger the complete
// removal of any previously-bound message handlers.
if (socket && configArg.messages) {
_(config.messages).forEach(function(handler, name, messageHandlers) {
socket.removeListener('message-' + name);
cloak._off('message-' + name, handler);
});
}

_.extend(config, configArg);

if (socket) {
cloak._applyConfig(config);
}
},

_applyConfig: function(toApply) {

_(toApply.messages).forEach(function(handler, name) {
socket.on('message-' + name, function(data) {
cloak._trigger('message-' + name, data);
});
cloak._on('message-' + name, handler);
});

_(toApply.serverEvents).forEach(function(eventHandler, eventName) {
cloak._on('cloak-' + eventName, eventHandler);
});

_.extend(timerEvents, toApply.timerEvents);
},

_on: function(event, handler) {
Expand Down Expand Up @@ -156,18 +183,16 @@
}
});

_(config.messages).forEach(function(handler, name) {
socket.on('message-' + name, function(data) {
cloak._trigger('message-' + name, data);
});
cloak._on('message-' + name, handler);
});

cloak._applyConfig(config);
},

stop: function() {
this._disconnect();
cloak._trigger('cloak-end');
socket.removeAllListeners();
_.forEach(events, removeKey);
_.forEach(timerEvents, removeKey);
socket = null;
},

_disconnect: function() {
Expand All @@ -179,7 +204,7 @@
},

connected: function() {
return socket.socket.connected;
return socket && socket.socket.connected;
},

currentUser: function() {
Expand Down
84 changes: 84 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,90 @@ module.exports = _.extend(suite, {

},

// When stopped and re-run, Clock should not invoke overwritten message
// handlers.
stoppingAndRestarting: function(test) {

test.expect(0);

var server = this.server;
var client = suite.createClient();
var host = this.host;
var pingCount = 0;

server.configure({
port: this.port
});

// Reconfigure and restart the client once it receives the `begin` event
client.configure({
serverEvents: {
begin: function() {
client.stop();

client.configure({
messages: {
custom: function() {
test.done();
}
}
});
client.run(host);
},
resume: function() {
server.messageAll('custom');
}
}
});

client.configure({
messages: {
custom: function() {
test.ok(false, 'Overwritten message handlers should not be invoked');
}
}
});

server.run();
client.run(host);
},

// When Cloak is configured with a `messages` option, all
// previously-specified message handlers should be removed.
resetMessageHandlers: function(test) {
var server = this.server;
var client = suite.createClient();

test.expect(0);
server.configure({
port: this.port
});

server.run();
client.run(this.host);

client.configure({
serverEvents: {
begin: function() {
server.messageAll('custom');
}
},
messages: {
custom: function() {
test.ok(false, 'Overwritten message handlers should not be invoked');
}
}
});

client.configure({
messages: {
custom: function() {
test.done();
}
}
});
},

// Test that users are pruned if reconnectWait is set
reconnectWait: function(test) {

Expand Down

0 comments on commit 8ddd7c8

Please sign in to comment.