faye 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE.md +1 -1
  3. package/client/faye-browser-min.js +1 -1
  4. package/client/faye-browser-min.js.map +1 -1
  5. package/client/faye-browser.js +224 -847
  6. package/client/faye-browser.js.map +1 -1
  7. package/package.json +3 -7
  8. package/src/adapters/node_adapter.js +37 -33
  9. package/src/adapters/static_server.js +12 -11
  10. package/src/engines/connection.js +2 -3
  11. package/src/engines/memory.js +32 -27
  12. package/src/engines/proxy.js +19 -20
  13. package/src/mixins/deferrable.js +12 -8
  14. package/src/mixins/logging.js +8 -8
  15. package/src/mixins/publisher.js +2 -3
  16. package/src/mixins/timeouts.js +9 -9
  17. package/src/protocol/channel.js +11 -16
  18. package/src/protocol/client.js +24 -21
  19. package/src/protocol/dispatcher.js +14 -14
  20. package/src/protocol/error.js +5 -6
  21. package/src/protocol/extensible.js +2 -3
  22. package/src/protocol/scheduler.js +5 -6
  23. package/src/protocol/server.js +25 -27
  24. package/src/protocol/socket.js +2 -1
  25. package/src/protocol/subscription.js +5 -5
  26. package/src/transport/cors.js +13 -14
  27. package/src/transport/event_source.js +12 -12
  28. package/src/transport/jsonp.js +13 -15
  29. package/src/transport/node_http.js +27 -19
  30. package/src/transport/node_local.js +3 -5
  31. package/src/transport/transport.js +29 -23
  32. package/src/transport/web_socket.js +16 -15
  33. package/src/transport/xhr.js +10 -9
  34. package/src/util/array.js +6 -5
  35. package/src/util/browser/event.js +6 -4
  36. package/src/util/class.js +1 -3
  37. package/src/util/constants.js +1 -1
  38. package/src/util/copy_object.js +6 -2
  39. package/src/util/namespace.js +6 -6
  40. package/src/util/set.js +10 -22
  41. package/src/util/uri.js +8 -67
  42. package/src/util/validate_options.js +3 -2
  43. package/src/util/assign.js +0 -16
  44. package/src/util/promise.js +0 -172
@@ -1,16 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var Buffer = require('safe-buffer').Buffer,
4
- path = require('path'),
5
- querystring = require('querystring'),
6
- url = require('url'),
3
+ var path = require('path'),
7
4
  WebSocket = require('faye-websocket'),
8
5
  EventSource = WebSocket.EventSource;
9
6
 
10
7
  var constants = require('../util/constants'),
11
- assign = require('../util/assign'),
12
8
  idFromMessages = require('../util/id_from_messages'),
13
9
  toJSON = require('../util/to_json'),
10
+ URI = require('../util/uri'),
14
11
  validateOptions = require('../util/validate_options'),
15
12
  Class = require('../util/class'),
16
13
  Logging = require('../mixins/logging'),
@@ -21,6 +18,7 @@ var constants = require('../util/constants'),
21
18
  StaticServer = require('./static_server');
22
19
 
23
20
  var NodeAdapter = Class({ className: 'NodeAdapter',
21
+ URL_BASE: 'http://localhost',
24
22
  DEFAULT_ENDPOINT: '/bayeux',
25
23
  SCRIPT_PATH: 'faye-browser-min.js',
26
24
 
@@ -45,14 +43,12 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
45
43
 
46
44
  if (extensions) {
47
45
  extensions = [].concat(extensions);
48
- for (i = 0, n = extensions.length; i < n; i++)
49
- this.addExtension(extensions[i]);
46
+ for (let ext of extensions) this.addExtension(ext);
50
47
  }
51
48
 
52
49
  if (websocketExtensions) {
53
50
  websocketExtensions = [].concat(websocketExtensions);
54
- for (i = 0, n = websocketExtensions.length; i < n; i++)
55
- this.addWebsocketExtension(websocketExtensions[i]);
51
+ for (let wsExt of websocketExtensions) this.addWebsocketExtension(wsExt);
56
52
  }
57
53
  },
58
54
 
@@ -94,18 +90,19 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
94
90
  httpServer.on(event, function(request) {
95
91
  if (self.check(request)) return self[method].apply(self, arguments);
96
92
 
97
- for (var i = 0, n = listeners.length; i < n; i++)
98
- listeners[i].apply(this, arguments);
93
+ for (let listener of listeners) {
94
+ listener.apply(this, arguments);
95
+ }
99
96
  });
100
97
  },
101
98
 
102
99
  check: function(request) {
103
- var path = url.parse(request.url, true).pathname;
100
+ var path = URI.parse(request.url, this.URL_BASE).pathname;
104
101
  return !!this._endpointRe.test(path);
105
102
  },
106
103
 
107
104
  handle: function(request, response) {
108
- var requestUrl = url.parse(request.url, true),
105
+ var requestUrl = URI.parse(request.url, this.URL_BASE),
109
106
  requestMethod = request.method,
110
107
  self = this;
111
108
 
@@ -114,49 +111,56 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
114
111
  request.on('error', function(error) { self._returnError(response, error) });
115
112
  response.on('error', function(error) { self._returnError(null, error) });
116
113
 
117
- if (this._static.test(requestUrl.pathname))
114
+ if (this._static.test(requestUrl.pathname)) {
118
115
  return this._static.call(request, response);
116
+ }
119
117
 
120
118
  // http://groups.google.com/group/faye-users/browse_thread/thread/4a01bb7d25d3636a
121
- if (requestMethod === 'OPTIONS' || request.headers['access-control-request-method'] === 'POST')
119
+ if (requestMethod === 'OPTIONS' || request.headers['access-control-request-method'] === 'POST') {
122
120
  return this._handleOptions(request, response);
121
+ }
123
122
 
124
- if (EventSource.isEventSource(request))
123
+ if (EventSource.isEventSource(request)) {
125
124
  return this.handleEventSource(request, response);
125
+ }
126
126
 
127
- if (requestMethod === 'GET')
128
- return this._callWithParams(request, response, requestUrl.query);
127
+ if (requestMethod === 'GET') {
128
+ return this._callWithParams(request, response, requestUrl.searchParams);
129
+ }
129
130
 
130
- if (requestMethod === 'POST')
131
+ if (requestMethod === 'POST') {
131
132
  return this._concatStream(request, function(data) {
132
133
  var type = (request.headers['content-type'] || '').split(';')[0],
133
134
  params = (type === 'application/json')
134
- ? { message: data }
135
- : querystring.parse(data);
135
+ ? new URLSearchParams({ message: data })
136
+ : new URLSearchParams(data);
136
137
 
137
138
  request.body = data;
138
139
  this._callWithParams(request, response, params);
139
140
  }, this);
141
+ }
140
142
 
141
143
  this._returnError(response, { message: 'Unrecognized request type' });
142
144
  },
143
145
 
144
146
  _callWithParams: function(request, response, params) {
145
- if (!params.message)
147
+ if (!params.has('message')) {
146
148
  return this._returnError(response, { message: 'Received request with no message: ' + this._formatRequest(request) });
149
+ }
147
150
 
148
151
  try {
149
- this.debug('Received message via HTTP ' + request.method + ': ?', params.message);
152
+ this.debug('Received message via HTTP ' + request.method + ': ?', params.get('message'));
150
153
 
151
- var message = this._parseJSON(params.message),
152
- jsonp = params.jsonp || constants.JSONP_CALLBACK,
154
+ var message = this._parseJSON(params.get('message')),
155
+ jsonp = params.get('jsonp') || constants.JSONP_CALLBACK,
153
156
  isGet = (request.method === 'GET'),
154
157
  type = isGet ? contenttypes.TYPE_SCRIPT : contenttypes.TYPE_JSON,
155
- headers = assign({}, type),
158
+ headers = Object.assign({}, type),
156
159
  origin = request.headers.origin;
157
160
 
158
- if (!this.VALID_JSONP_CALLBACK.test(jsonp))
161
+ if (!this.VALID_JSONP_CALLBACK.test(jsonp)) {
159
162
  return this._returnError(response, { message: 'Invalid JSON-P callback: ' + jsonp });
163
+ }
160
164
 
161
165
  headers['Cache-Control'] = 'no-cache, no-store';
162
166
  headers['X-Content-Type-Options'] = 'nosniff';
@@ -264,9 +268,9 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
264
268
  var buffer = Buffer.alloc(length),
265
269
  offset = 0;
266
270
 
267
- for (var i = 0, n = chunks.length; i < n; i++) {
268
- chunks[i].copy(buffer, offset);
269
- offset += chunks[i].length;
271
+ for (let chunk of chunks) {
272
+ chunk.copy(buffer, offset);
273
+ offset += chunk.length;
270
274
  }
271
275
  callback.call(context, buffer.toString('utf8'));
272
276
  });
@@ -302,12 +306,12 @@ var NodeAdapter = Class({ className: 'NodeAdapter',
302
306
  }
303
307
  });
304
308
 
305
- for (var method in Publisher) (function(method) {
309
+ for (let method of Object.keys(Publisher)) {
306
310
  NodeAdapter.prototype[method] = function() {
307
311
  return this._server._engine[method].apply(this._server._engine, arguments);
308
312
  };
309
- })(method);
313
+ }
310
314
 
311
- assign(NodeAdapter.prototype, Logging);
315
+ Object.assign(NodeAdapter.prototype, Logging);
312
316
 
313
317
  module.exports = NodeAdapter;
@@ -2,23 +2,24 @@
2
2
 
3
3
  var crypto = require('crypto'),
4
4
  fs = require('fs'),
5
- path = require('path'),
6
- url = require('url');
5
+ path = require('path');
7
6
 
8
7
  var Class = require('../util/class'),
9
- assign = require('../util/assign'),
8
+ URI = require('../util/uri'),
10
9
  contenttypes = require('./content_types');
11
10
 
12
11
  var StaticServer = Class({
12
+ URL_BASE: 'http://localhost',
13
+
13
14
  initialize: function(directory, pathRegex) {
14
15
  this._directory = directory;
15
16
  this._pathRegex = pathRegex;
16
- this._pathMap = {};
17
- this._index = {};
17
+ this._pathMap = new Map();
18
+ this._index = new Map();
18
19
  },
19
20
 
20
21
  map: function(requestPath, filename) {
21
- this._pathMap[requestPath] = filename;
22
+ this._pathMap.set(requestPath, filename);
22
23
  },
23
24
 
24
25
  test: function(pathname) {
@@ -26,13 +27,13 @@ var StaticServer = Class({
26
27
  },
27
28
 
28
29
  call: function(request, response) {
29
- var pathname = url.parse(request.url, true).pathname,
30
+ var pathname = URI.parse(request.url, this.URL_BASE).pathname,
30
31
  filename = path.basename(pathname);
31
32
 
32
- filename = this._pathMap[filename] || filename;
33
- this._index[filename] = this._index[filename] || {};
33
+ filename = this._pathMap.get(filename) || filename;
34
+ this._index.set(filename, this._index.get(filename) || {});
34
35
 
35
- var cache = this._index[filename],
36
+ var cache = this._index.get(filename),
36
37
  fullpath = path.join(this._directory, filename);
37
38
 
38
39
  try {
@@ -62,7 +63,7 @@ var StaticServer = Class({
62
63
  }
63
64
  else {
64
65
  headers['Content-Length'] = cache.content.length;
65
- assign(headers, contenttypes[type]);
66
+ Object.assign(headers, contenttypes[type]);
66
67
  response.writeHead(200, headers);
67
68
  response.end(cache.content);
68
69
  }
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var Class = require('../util/class'),
4
- assign = require('../util/assign'),
5
4
  Deferrable = require('../mixins/deferrable'),
6
5
  Timeouts = require('../mixins/timeouts');
7
6
 
@@ -51,7 +50,7 @@ var Connection = Class({
51
50
  }
52
51
  });
53
52
 
54
- assign(Connection.prototype, Deferrable);
55
- assign(Connection.prototype, Timeouts);
53
+ Object.assign(Connection.prototype, Deferrable);
54
+ Object.assign(Connection.prototype, Timeouts);
56
55
 
57
56
  module.exports = Connection;
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var copyObject = require('../util/copy_object'),
4
- assign = require('../util/assign'),
5
4
  Namespace = require('../util/namespace'),
6
5
  Set = require('../util/set'),
7
6
  Timeouts = require('../mixins/timeouts');
@@ -24,9 +23,9 @@ Memory.prototype = {
24
23
 
25
24
  reset: function() {
26
25
  this._namespace = new Namespace();
27
- this._clients = {};
28
- this._channels = {};
29
- this._messages = {};
26
+ this._clients = new Map();
27
+ this._channels = new Map();
28
+ this._messages = new Map();
30
29
  },
31
30
 
32
31
  createClient: function(callback, context) {
@@ -41,12 +40,15 @@ Memory.prototype = {
41
40
  if (!this._namespace.exists(clientId)) return;
42
41
  var clients = this._clients;
43
42
 
44
- if (clients[clientId])
45
- clients[clientId].forEach(function(channel) { this.unsubscribe(clientId, channel) }, this);
43
+ if (this._clients.has(clientId)) {
44
+ for (let channel of this._clients.get(clientId)) {
45
+ this.unsubscribe(clientId, channel);
46
+ }
47
+ }
46
48
 
47
49
  this.removeTimeout(clientId);
48
50
  this._namespace.release(clientId);
49
- delete this._messages[clientId];
51
+ this._messages.delete(clientId);
50
52
  this._server.debug('Destroyed client ?', clientId);
51
53
  this._server.trigger('disconnect', clientId);
52
54
  this._server.trigger('close', clientId);
@@ -71,11 +73,11 @@ Memory.prototype = {
71
73
  subscribe: function(clientId, channel, callback, context) {
72
74
  var clients = this._clients, channels = this._channels;
73
75
 
74
- clients[clientId] = clients[clientId] || new Set();
75
- var trigger = clients[clientId].add(channel);
76
+ if (!clients.has(clientId)) clients.set(clientId, new Set());
77
+ var trigger = clients.get(clientId).add(channel);
76
78
 
77
- channels[channel] = channels[channel] || new Set();
78
- channels[channel].add(clientId);
79
+ if (!channels.has(channel)) channels.set(channel, new Set());
80
+ channels.get(channel).add(clientId);
79
81
 
80
82
  this._server.debug('Subscribed client ? to channel ?', clientId, channel);
81
83
  if (trigger) this._server.trigger('subscribe', clientId, channel);
@@ -87,14 +89,14 @@ Memory.prototype = {
87
89
  channels = this._channels,
88
90
  trigger = false;
89
91
 
90
- if (clients[clientId]) {
91
- trigger = clients[clientId].remove(channel);
92
- if (clients[clientId].isEmpty()) delete clients[clientId];
92
+ if (clients.has(clientId)) {
93
+ trigger = clients.get(clientId).remove(channel);
94
+ if (clients.get(clientId).isEmpty()) clients.delete(clientId);
93
95
  }
94
96
 
95
- if (channels[channel]) {
96
- channels[channel].remove(clientId);
97
- if (channels[channel].isEmpty()) delete channels[channel];
97
+ if (channels.has(channel)) {
98
+ channels.get(channel).remove(clientId);
99
+ if (channels.get(channel).isEmpty()) channels.delete(channel);
98
100
  }
99
101
 
100
102
  this._server.debug('Unsubscribed client ? from channel ?', clientId, channel);
@@ -109,29 +111,32 @@ Memory.prototype = {
109
111
  clients = new Set(),
110
112
  subs;
111
113
 
112
- for (var i = 0, n = channels.length; i < n; i++) {
113
- subs = this._channels[channels[i]];
114
+ for (let channel of channels) {
115
+ subs = this._channels.get(channel);
114
116
  if (!subs) continue;
115
- subs.forEach(clients.add, clients);
117
+
118
+ for (let sub of subs) {
119
+ clients.add(sub);
120
+ }
116
121
  }
117
122
 
118
- clients.forEach(function(clientId) {
123
+ for (let clientId of clients) {
119
124
  this._server.debug('Queueing for client ?: ?', clientId, message);
120
- messages[clientId] = messages[clientId] || [];
121
- messages[clientId].push(copyObject(message));
125
+ if (!messages.has(clientId)) messages.set(clientId, []);
126
+ messages.get(clientId).push(copyObject(message));
122
127
  this.emptyQueue(clientId);
123
- }, this);
128
+ }
124
129
 
125
130
  this._server.trigger('publish', message.clientId, message.channel, message.data);
126
131
  },
127
132
 
128
133
  emptyQueue: function(clientId) {
129
134
  if (!this._server.hasConnection(clientId)) return;
130
- this._server.deliver(clientId, this._messages[clientId]);
131
- delete this._messages[clientId];
135
+ this._server.deliver(clientId, this._messages.get(clientId));
136
+ this._messages.delete(clientId);
132
137
  }
133
138
  };
134
139
 
135
- assign(Memory.prototype, Timeouts);
140
+ Object.assign(Memory.prototype, Timeouts);
136
141
 
137
142
  module.exports = Memory;
@@ -1,24 +1,21 @@
1
1
  'use strict';
2
2
 
3
- var asap = require('asap'),
4
- assign = require('../util/assign'),
5
- random = require('../util/random'),
3
+ var random = require('../util/random'),
6
4
  Class = require('../util/class'),
7
- Promise = require('../util/promise'),
8
5
  Logging = require('../mixins/logging'),
9
6
  Publisher = require('../mixins/publisher'),
10
7
  Channel = require('../protocol/channel'),
11
8
  Connection = require('./connection'),
12
9
  Memory = require('./memory');
13
10
 
14
- var Proxy = assign(Class({ className: 'Engine.Proxy',
11
+ var Proxy = Object.assign(Class({ className: 'Engine.Proxy',
15
12
  MAX_DELAY: 0,
16
13
  INTERVAL: 0,
17
14
  TIMEOUT: 60,
18
15
 
19
16
  initialize: function(options) {
20
17
  this._options = options || {};
21
- this._connections = {};
18
+ this._connections = new Map();
22
19
  this.interval = this._options.interval || this.INTERVAL;
23
20
  this.timeout = this._options.timeout || this.TIMEOUT;
24
21
 
@@ -27,7 +24,7 @@ var Proxy = assign(Class({ className: 'Engine.Proxy',
27
24
 
28
25
  this.bind('close', function(clientId) {
29
26
  var self = this;
30
- asap(function() { self.flushConnection(clientId) });
27
+ Promise.resolve().then(function() { self.flushConnection(clientId) });
31
28
  }, this);
32
29
 
33
30
  this.debug('Created new engine: ?', this._options);
@@ -42,24 +39,24 @@ var Proxy = assign(Class({ className: 'Engine.Proxy',
42
39
  },
43
40
 
44
41
  hasConnection: function(clientId) {
45
- return this._connections.hasOwnProperty(clientId);
42
+ return this._connections.has(clientId);
46
43
  },
47
44
 
48
45
  connection: function(clientId, create) {
49
- var conn = this._connections[clientId];
46
+ var conn = this._connections.get(clientId);
50
47
  if (conn || !create) return conn;
51
- this._connections[clientId] = new Connection(this, clientId);
48
+ this._connections.set(clientId, new Connection(this, clientId));
52
49
  this.trigger('connection:open', clientId);
53
- return this._connections[clientId];
50
+ return this._connections.get(clientId);
54
51
  },
55
52
 
56
53
  closeConnection: function(clientId) {
57
54
  this.debug('Closing connection for ?', clientId);
58
- var conn = this._connections[clientId];
55
+ var conn = this._connections.get(clientId);
59
56
  if (!conn) return;
60
57
  if (conn.socket) conn.socket.close();
61
58
  this.trigger('connection:close', clientId);
62
- delete this._connections[clientId];
59
+ this._connections.delete(clientId);
63
60
  },
64
61
 
65
62
  openSocket: function(clientId, socket) {
@@ -73,8 +70,8 @@ var Proxy = assign(Class({ className: 'Engine.Proxy',
73
70
  var conn = this.connection(clientId, false);
74
71
  if (!conn) return false;
75
72
 
76
- for (var i = 0, n = messages.length; i < n; i++) {
77
- conn.deliver(messages[i]);
73
+ for (let message of messages) {
74
+ conn.deliver(message);
78
75
  }
79
76
  return true;
80
77
  },
@@ -94,7 +91,9 @@ var Proxy = assign(Class({ className: 'Engine.Proxy',
94
91
  },
95
92
 
96
93
  close: function() {
97
- for (var clientId in this._connections) this.flushConnection(clientId);
94
+ for (let clientId of this._connections.keys()) {
95
+ this.flushConnection(clientId);
96
+ }
98
97
  this._engine.disconnect();
99
98
  },
100
99
 
@@ -114,13 +113,13 @@ var Proxy = assign(Class({ className: 'Engine.Proxy',
114
113
 
115
114
  var METHODS = ['createClient', 'clientExists', 'destroyClient', 'ping', 'subscribe', 'unsubscribe'];
116
115
 
117
- METHODS.forEach(function(method) {
116
+ for (let method of METHODS) {
118
117
  Proxy.prototype[method] = function() {
119
118
  return this._engine[method].apply(this._engine, arguments);
120
119
  };
121
- });
120
+ }
122
121
 
123
- assign(Proxy.prototype, Publisher);
124
- assign(Proxy.prototype, Logging);
122
+ Object.assign(Proxy.prototype, Publisher);
123
+ Object.assign(Proxy.prototype, Logging);
125
124
 
126
125
  module.exports = Proxy;
@@ -1,27 +1,30 @@
1
1
  'use strict';
2
2
 
3
- var Promise = require('../util/promise');
4
-
5
3
  module.exports = {
6
4
  then: function(callback, errback) {
7
5
  var self = this;
8
- if (!this._promise)
6
+
7
+ if (!this._promise) {
9
8
  this._promise = new Promise(function(resolve, reject) {
10
9
  self._resolve = resolve;
11
10
  self._reject = reject;
12
11
  });
12
+ }
13
13
 
14
- if (arguments.length === 0)
14
+ if (arguments.length === 0) {
15
15
  return this._promise;
16
- else
16
+ } else {
17
17
  return this._promise.then(callback, errback);
18
+ }
18
19
  },
19
20
 
20
21
  callback: function(callback, context) {
22
+ if (!callback) return;
21
23
  return this.then(function(value) { callback.call(context, value) });
22
24
  },
23
25
 
24
26
  errback: function(callback, context) {
27
+ if (!callback) return;
25
28
  return this.then(null, function(reason) { callback.call(context, reason) });
26
29
  },
27
30
 
@@ -38,11 +41,12 @@ module.exports = {
38
41
 
39
42
  this.then();
40
43
 
41
- if (status === 'succeeded')
44
+ if (status === 'succeeded') {
42
45
  this._resolve(value);
43
- else if (status === 'failed')
46
+ } else if (status === 'failed') {
44
47
  this._reject(value);
45
- else
48
+ } else {
46
49
  delete this._promise;
50
+ }
47
51
  }
48
52
  };
@@ -30,18 +30,18 @@ var Logging = {
30
30
  if (klass) banner += '.' + klass;
31
31
  banner += '] ';
32
32
 
33
- if (typeof logger[level] === 'function')
33
+ if (typeof logger[level] === 'function') {
34
34
  logger[level](banner + message);
35
- else if (typeof logger === 'function')
35
+ } else if (typeof logger === 'function') {
36
36
  logger(banner + message);
37
+ }
37
38
  }
38
39
  };
39
40
 
40
- for (var key in Logging.LOG_LEVELS)
41
- (function(level) {
42
- Logging[level] = function() {
43
- this.writeLog(arguments, level);
44
- };
45
- })(key);
41
+ for (let level of Object.keys(Logging.LOG_LEVELS)) {
42
+ Logging[level] = function() {
43
+ this.writeLog(arguments, level);
44
+ };
45
+ }
46
46
 
47
47
  module.exports = Logging;
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var assign = require('../util/assign'),
4
- EventEmitter = require('../util/event_emitter');
3
+ var EventEmitter = require('../util/event_emitter');
5
4
 
6
5
  var Publisher = {
7
6
  countListeners: function(eventType) {
@@ -31,7 +30,7 @@ var Publisher = {
31
30
  }
32
31
  };
33
32
 
34
- assign(Publisher, EventEmitter.prototype);
33
+ Object.assign(Publisher, EventEmitter.prototype);
35
34
  Publisher.trigger = Publisher.emit;
36
35
 
37
36
  module.exports = Publisher;
@@ -2,25 +2,25 @@
2
2
 
3
3
  module.exports = {
4
4
  addTimeout: function(name, delay, callback, context) {
5
- this._timeouts = this._timeouts || {};
6
- if (this._timeouts.hasOwnProperty(name)) return;
5
+ this._timeouts = this._timeouts || new Map();
6
+ if (this._timeouts.has(name)) return;
7
7
  var self = this;
8
- this._timeouts[name] = global.setTimeout(function() {
8
+ this._timeouts.set(name, global.setTimeout(function() {
9
9
  delete self._timeouts[name];
10
10
  callback.call(context);
11
- }, 1000 * delay);
11
+ }, 1000 * delay));
12
12
  },
13
13
 
14
14
  removeTimeout: function(name) {
15
- this._timeouts = this._timeouts || {};
16
- var timeout = this._timeouts[name];
15
+ this._timeouts = this._timeouts || new Map();
16
+ var timeout = this._timeouts.get(name);
17
17
  if (!timeout) return;
18
18
  global.clearTimeout(timeout);
19
- delete this._timeouts[name];
19
+ this._timeouts.delete(name);
20
20
  },
21
21
 
22
22
  removeAllTimeouts: function() {
23
- this._timeouts = this._timeouts || {};
24
- for (var name in this._timeouts) this.removeTimeout(name);
23
+ this._timeouts = this._timeouts || new Map();
24
+ for (let name of this._timeouts.keys()) this.removeTimeout(name);
25
25
  }
26
26
  };
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  var Class = require('../util/class'),
4
- assign = require('../util/assign'),
5
4
  Publisher = require('../mixins/publisher'),
6
5
  Grammar = require('./grammar');
7
6
 
@@ -19,9 +18,9 @@ var Channel = Class({
19
18
  }
20
19
  });
21
20
 
22
- assign(Channel.prototype, Publisher);
21
+ Object.assign(Channel.prototype, Publisher);
23
22
 
24
- assign(Channel, {
23
+ Object.assign(Channel, {
25
24
  HANDSHAKE: '/meta/handshake',
26
25
  CONNECT: '/meta/connect',
27
26
  SUBSCRIBE: '/meta/subscribe',
@@ -79,17 +78,15 @@ assign(Channel, {
79
78
 
80
79
  Set: Class({
81
80
  initialize: function() {
82
- this._channels = {};
81
+ this._channels = new Map();
83
82
  },
84
83
 
85
84
  getKeys: function() {
86
- var keys = [];
87
- for (var key in this._channels) keys.push(key);
88
- return keys;
85
+ return [...this._channels.keys()];
89
86
  },
90
87
 
91
88
  remove: function(name) {
92
- delete this._channels[name];
89
+ this._channels.delete(name);
93
90
  },
94
91
 
95
92
  hasSubscription: function(name) {
@@ -97,16 +94,14 @@ assign(Channel, {
97
94
  },
98
95
 
99
96
  subscribe: function(names, subscription) {
100
- var name;
101
- for (var i = 0, n = names.length; i < n; i++) {
102
- name = names[i];
103
- var channel = this._channels[name] = this._channels[name] || new Channel(name);
104
- channel.bind('message', subscription);
97
+ for (let name of names) {
98
+ if (!this._channels.has(name)) this._channels.set(name, new Channel(name));
99
+ this._channels.get(name).bind('message', subscription);
105
100
  }
106
101
  },
107
102
 
108
103
  unsubscribe: function(name, subscription) {
109
- var channel = this._channels[name];
104
+ var channel = this._channels.get(name);
110
105
  if (!channel) return false;
111
106
  channel.unbind('message', subscription);
112
107
 
@@ -121,8 +116,8 @@ assign(Channel, {
121
116
  distributeMessage: function(message) {
122
117
  var channels = Channel.expand(message.channel);
123
118
 
124
- for (var i = 0, n = channels.length; i < n; i++) {
125
- var channel = this._channels[channels[i]];
119
+ for (let chan of channels) {
120
+ var channel = this._channels.get(chan);
126
121
  if (channel) channel.trigger('message', message);
127
122
  }
128
123
  }