store.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Expose the constructor.
  8. */
  9. exports = module.exports = Store;
  10. /**
  11. * Module dependencies.
  12. */
  13. var EventEmitter = process.EventEmitter;
  14. /**
  15. * Store interface
  16. *
  17. * @api public
  18. */
  19. function Store (options) {
  20. this.options = options;
  21. this.clients = {};
  22. };
  23. /**
  24. * Inherit from EventEmitter.
  25. */
  26. Store.prototype.__proto__ = EventEmitter.prototype;
  27. /**
  28. * Initializes a client store
  29. *
  30. * @param {String} id
  31. * @api public
  32. */
  33. Store.prototype.client = function (id) {
  34. if (!this.clients[id]) {
  35. this.clients[id] = new (this.constructor.Client)(this, id);
  36. }
  37. return this.clients[id];
  38. };
  39. /**
  40. * Destroys a client
  41. *
  42. * @api {String} sid
  43. * @param {Number} number of seconds to expire client data
  44. * @api private
  45. */
  46. Store.prototype.destroyClient = function (id, expiration) {
  47. if (this.clients[id]) {
  48. this.clients[id].destroy(expiration);
  49. delete this.clients[id];
  50. }
  51. return this;
  52. };
  53. /**
  54. * Destroys the store
  55. *
  56. * @param {Number} number of seconds to expire client data
  57. * @api private
  58. */
  59. Store.prototype.destroy = function (clientExpiration) {
  60. var keys = Object.keys(this.clients)
  61. , count = keys.length;
  62. for (var i = 0, l = count; i < l; i++) {
  63. this.destroyClient(keys[i], clientExpiration);
  64. }
  65. this.clients = {};
  66. return this;
  67. };
  68. /**
  69. * Client.
  70. *
  71. * @api public
  72. */
  73. Store.Client = function (store, id) {
  74. this.store = store;
  75. this.id = id;
  76. };