Permalink
Browse files

use helper to attach callbacks to a specific session

  • Loading branch information...
1 parent c274f27 commit 6a63b2ce06e1c433f56910558363d22670770860 @Floby committed Dec 23, 2012
Showing with 13 additions and 11 deletions.
  1. +11 −9 lib/Session.js
  2. +2 −2 test/test-010-session02-login.js
View
@@ -99,10 +99,15 @@ function Session (params) {
}
util.inherits(Session, EventEmitter);
-function sessionCallback (session, sid, cb) {
+/**
+ * wraps a callback in a function that checks if the callbacks is still relevant for
+ * the current session
+ */
+function sessionCallback (session, cb) {
+ var sid = session._id;
return function() {
if(sid != session._id) return;
- return cb.apply(session, args);
+ return cb.apply(session, arguments);
}
}
@@ -112,15 +117,12 @@ function sessionCallback (session, sid, cb) {
*/
Session.prototype._setupNativeCallbacks = function _setupNativeCallbacks() {
var self = this;
- var session_id = this._id;
- this._sp_session.logged_in = function(err) {
- if(self._id != session_id) return;
+ this._sp_session.logged_in = sessionCallback(this, function(err) {
self.emit('login', err);
- }
- this._sp_session.logged_out = function() {
- if(self._id != session_id) return;
+ });
+ this._sp_session.logged_out = sessionCallback(this, function() {
self.emit('logout');
- }
+ });
};
/**
@@ -22,9 +22,9 @@ exports.testLoginIsSucessful = function(test) {
var logged = false;
setTimeout(function() {
if(!logged) {
- test.fail("Waited too long to login");
+ test.done(new Error("Waited too long for login"));
}
- }, 8000);
+ }, 10000);
session.on('login', function(err) {
test.equal(null, err, err + ' occured on login');
logged = true;

0 comments on commit 6a63b2c

Please sign in to comment.