store.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. };