aboutsummaryrefslogtreecommitdiffstats
path: root/signaling-server/node_modules/socket.io/lib/store.js
blob: 06c0389a624e3cfe1318135822fc6275ff7680f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*!
 * socket.io-node
 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
 * MIT Licensed
 */

/**
 * Expose the constructor.
 */

exports = module.exports = Store;

/**
 * Module dependencies.
 */

var EventEmitter = process.EventEmitter;

/**
 * Store interface
 *
 * @api public
 */

function Store (options) {
  this.options = options;
  this.clients = {};
};

/**
 * Inherit from EventEmitter.
 */

Store.prototype.__proto__ = EventEmitter.prototype;

/**
 * Initializes a client store
 *
 * @param {String} id
 * @api public
 */

Store.prototype.client = function (id) {
  if (!this.clients[id]) {
    this.clients[id] = new (this.constructor.Client)(this, id);
  }

  return this.clients[id];
};

/**
 * Destroys a client
 *
 * @api {String} sid
 * @param {Number} number of seconds to expire client data
 * @api private
 */

Store.prototype.destroyClient = function (id, expiration) {
  if (this.clients[id]) {
    this.clients[id].destroy(expiration);
    delete this.clients[id];
  }

  return this;
};

/**
 * Destroys the store
 *
 * @param {Number} number of seconds to expire client data
 * @api private
 */

Store.prototype.destroy = function (clientExpiration) {
  var keys = Object.keys(this.clients)
    , count = keys.length;

  for (var i = 0, l = count; i < l; i++) {
    this.destroyClient(keys[i], clientExpiration);
  }

  this.clients = {};

  return this;
};

/**
 * Client.
 *
 * @api public
 */

Store.Client = function (store, id) {
  this.store = store;
  this.id = id;
};