session.js 986 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var SessionService = require('../common/service/sessionService');
  2. module.exports = function(app, opts) {
  3. var cmp = new Component(app, opts);
  4. app.set('sessionService', cmp, true);
  5. return cmp;
  6. };
  7. /**
  8. * Session component. Manage sessions.
  9. *
  10. * @param {Object} app current application context
  11. * @param {Object} opts attach parameters
  12. */
  13. var Component = function(app, opts) {
  14. opts = opts || {};
  15. this.app = app;
  16. this.service = new SessionService(opts);
  17. var getFun = function(m) {
  18. return (function() {
  19. return function() {
  20. return self.service[m].apply(self.service, arguments);
  21. };
  22. })();
  23. };
  24. // proxy the service methods except the lifecycle interfaces of component
  25. var method, self = this;
  26. for(var m in this.service) {
  27. if(m !== 'start' && m !== 'stop') {
  28. method = this.service[m];
  29. if(typeof method === 'function') {
  30. this[m] = getFun(m);
  31. }
  32. }
  33. }
  34. };
  35. Component.prototype.name = '__session__';