File size: 1,551 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

/*
 * Dependencies
 */

var Broadcast = require('./broadcast');
var EventEmitter = require('events').EventEmitter;
var inherits = require('./util').inherits;


/*
 * Namespace Constructor
 *
 * - name (string)
 * - item (socket) : a single socket
 * - item (room) : a collection of sockets
 */

var Namespace = function Namespace (name, item) {
  Namespace.__super__.call(this);

  this.name = name;
  this.item = item;
  this._broadcast = Broadcast.bind(this.item);
};


/*
 * Inherit from EventEmitter
 */

inherits(Namespace, EventEmitter);
Namespace.prototype._emit = EventEmitter.prototype.emit;


Namespace.parse = function parse (string) {
  var namespace, event;
  string = string.split('.');
  if (string.length === 2) {
    namespace = string[0];
    event = string[1];
  } else {
    event = string[0];
  }
  return {
    namespace: namespace,
    event: event
  };
};

/*
 * Emit
 *
 * - event (string)
 * - args... (mixed) : any other data you want to send
 */

Namespace.prototype.emit = function emit (event, arg1, arg2, arg3) {
  event = this.name + '.' + event;
  this.item.emit(event, arg1, arg2, arg3);
};


/*
 * Broadcast
 *
 * - event (string)
 * - args... (mixed)
 */

Namespace.prototype.broadcast = function broadcast (event, arg1, arg2, arg3) {
  event = this.name + '.' + event;
  this._broadcast(event, arg1, arg2, arg3);
};


/*
 * (Private) Release
 */

Namespace.prototype._release = function release () {
  delete this.name;
  delete this.item;
  delete this._broadcast;
};


module.exports = Namespace;