transit-im
Version:
express-like framework for AIM bots creation (ICQ as well)
90 lines (76 loc) • 2.34 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Sessions, UserSession;
UserSession = (function() {
function UserSession(userId) {
this.userId = userId;
}
UserSession.prototype.close = function() {};
return UserSession;
})();
Sessions = (function() {
function Sessions(options) {
var _ref;
this.options = options != null ? options : {};
this._sessions = {};
this._idle = {};
this._sessionClass = (_ref = this.options.sessionClass) != null ? _ref : UserSession;
}
Sessions.prototype.session = function(id) {
var session, _ref, _ref1,
_this = this;
session = this._sessions[id];
if (!session) {
session = new this._sessionClass(id);
this._sessions[id] = session;
if (this.options.forgetOnIdle) {
this._idle[id] = {
interval: this.options.forgetOnIdle,
to: null
};
}
}
if ((_ref = this._idle[id]) != null ? _ref.interval : void 0) {
clearTimeout((_ref1 = this._idle[id]) != null ? _ref1.to : void 0);
this._idle[id].to = setTimeout((function() {
return forget(id);
}), this._idle[id].interval);
}
return session;
};
Sessions.prototype.listSessions = function() {
return this._sessions.slice();
};
Sessions.prototype.forget = function(id) {
var _ref;
if ((_ref = this._sessions[id]) != null) {
_ref.close();
}
return delete this._sessions[id];
};
Sessions.prototype.install = function(transit) {
var _this = this;
transit.extendRequest("session", "listSessions");
return function(req, res, next) {
var userId, _ref;
req.attr("listSessions", _this.listSessions.bind(_this));
userId = req.user;
if (req.command === 'exit') {
_this.forget(userId);
} else {
req.attr("session", _this.session(userId));
if ((_ref = req.session) != null ? _ref.postCreate : void 0) {
return req.session.postCreate(function() {
return next();
});
}
}
return next();
};
};
return Sessions;
})();
module.exports = function(options) {
return new Sessions(options);
};
}).call(this);