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,17 +1,15 @@
1
1
  'use strict';
2
2
 
3
- var Buffer = require('safe-buffer').Buffer,
4
- http = require('http'),
3
+ var http = require('http'),
5
4
  https = require('https'),
6
5
  tunnel = require('tunnel-agent');
7
6
 
8
7
  var Class = require('../util/class'),
9
8
  URI = require('../util/uri'),
10
- assign = require('../util/assign'),
11
9
  toJSON = require('../util/to_json'),
12
10
  Transport = require('./transport');
13
11
 
14
- var NodeHttp = assign(Class(Transport, { className: 'NodeHttp',
12
+ var NodeHttp = Object.assign(Class(Transport, { className: 'NodeHttp',
15
13
  SECURE_PROTOCOLS: ['https:', 'wss:'],
16
14
 
17
15
  initialize: function() {
@@ -31,17 +29,21 @@ var NodeHttp = assign(Class(Transport, { className: 'NodeHttp',
31
29
  return;
32
30
  }
33
31
 
34
- var options = assign({
32
+ var proxyAuth = this._proxyUri.username
33
+ ? [this._proxyUri.username, this._proxyUri.password].join(':')
34
+ : undefined;
35
+
36
+ var options = Object.assign({
35
37
  proxy: {
36
38
  host: this._proxyUri.hostname,
37
39
  port: this._proxyUri.port || this.DEFAULT_PORTS[this._proxyUri.protocol],
38
- proxyAuth: this._proxyUri.auth,
39
- headers: assign({ host: this.endpoint.host }, proxy.headers)
40
+ proxyAuth: proxyAuth,
41
+ headers: Object.assign({ host: this.endpoint.host }, proxy.headers)
40
42
  }
41
43
  }, this._dispatcher.tls);
42
44
 
43
45
  if (this._proxySecure) {
44
- assign(options.proxy, proxy.tls);
46
+ Object.assign(options.proxy, proxy.tls);
45
47
  this._tunnel = tunnel.httpsOverHttps(options);
46
48
  } else {
47
49
  this._tunnel = tunnel.httpsOverHttp(options);
@@ -75,7 +77,8 @@ var NodeHttp = assign(Class(Transport, { className: 'NodeHttp',
75
77
  _buildParams: function(content) {
76
78
  var uri = this.endpoint,
77
79
  proxy = this._proxyUri,
78
- target = this._tunnel ? uri : (proxy || uri);
80
+ target = this._tunnel ? uri : (proxy || uri),
81
+ auth;
79
82
 
80
83
  var headers = {
81
84
  'Content-Length': content.length,
@@ -83,15 +86,17 @@ var NodeHttp = assign(Class(Transport, { className: 'NodeHttp',
83
86
  'Host': uri.host
84
87
  };
85
88
 
86
- if (uri.auth)
87
- headers['Authorization'] = 'Basic ' + Buffer.from(uri.auth, 'utf8').toString('base64');
89
+ if (uri.username) {
90
+ auth = [uri.username, uri.password].join(':');
91
+ headers['Authorization'] = 'Basic ' + Buffer.from(auth, 'utf8').toString('base64');
92
+ }
88
93
 
89
94
  var params = {
90
95
  method: 'POST',
91
96
  host: target.hostname,
92
97
  port: target.port || this.DEFAULT_PORTS[target.protocol],
93
- path: uri.path,
94
- headers: assign(headers, this._dispatcher.headers)
98
+ path: uri.pathname + uri.search,
99
+ headers: Object.assign(headers, this._dispatcher.headers)
95
100
  };
96
101
 
97
102
  var cookie = this._getCookies();
@@ -100,12 +105,14 @@ var NodeHttp = assign(Class(Transport, { className: 'NodeHttp',
100
105
  if (this._tunnel) {
101
106
  params.agent = this._tunnel;
102
107
  } else if (this._endpointSecure) {
103
- assign(params, this._dispatcher.tls);
108
+ Object.assign(params, this._dispatcher.tls);
104
109
  } else if (proxy) {
105
110
  params.path = this.endpoint.href;
106
- assign(params, this._proxy.tls);
107
- if (proxy.auth)
108
- params.headers['Proxy-Authorization'] = Buffer.from(proxy.auth, 'utf8').toString('base64');
111
+ Object.assign(params, this._proxy.tls);
112
+ if (proxy.username) {
113
+ auth = [proxy.username, proxy.password].join(':');
114
+ params.headers['Proxy-Authorization'] = Buffer.from(auth, 'utf8').toString('base64');
115
+ }
109
116
  }
110
117
 
111
118
  return params;
@@ -122,10 +129,11 @@ var NodeHttp = assign(Class(Transport, { className: 'NodeHttp',
122
129
  var replies;
123
130
  try { replies = JSON.parse(body) } catch (error) {}
124
131
 
125
- if (replies)
132
+ if (replies) {
126
133
  self._receive(replies);
127
- else
134
+ } else {
128
135
  self._handleError(messages);
136
+ }
129
137
  });
130
138
  }
131
139
 
@@ -1,21 +1,19 @@
1
1
  'use strict';
2
2
 
3
- var asap = require('asap'),
4
- Class = require('../util/class'),
3
+ var Class = require('../util/class'),
5
4
  URI = require('../util/uri'),
6
5
  copyObject = require('../util/copy_object'),
7
- assign = require('../util/assign'),
8
6
  Server = require('../protocol/server'),
9
7
  Transport = require('./transport');
10
8
 
11
- var NodeLocal = assign(Class(Transport, {
9
+ var NodeLocal = Object.assign(Class(Transport, {
12
10
  batching: false,
13
11
 
14
12
  request: function(messages) {
15
13
  messages = copyObject(messages);
16
14
  var self = this;
17
15
 
18
- asap(function() {
16
+ Promise.resolve().then(function() {
19
17
  self.endpoint.process(messages, null, function(replies) {
20
18
  self._receive(copyObject(replies));
21
19
  });
@@ -2,14 +2,12 @@
2
2
 
3
3
  var Class = require('../util/class'),
4
4
  Cookie = require('../util/cookies').Cookie,
5
- Promise = require('../util/promise'),
6
5
  array = require('../util/array'),
7
- assign = require('../util/assign'),
8
6
  Logging = require('../mixins/logging'),
9
7
  Timeouts = require('../mixins/timeouts'),
10
8
  Channel = require('../protocol/channel');
11
9
 
12
- var Transport = assign(Class({ className: 'Transport',
10
+ var Transport = Object.assign(Class({ className: 'Transport',
13
11
  DEFAULT_PORTS: { 'http:': 80, 'https:': 443, 'ws:': 80, 'wss:': 443 },
14
12
  MAX_DELAY: 0,
15
13
 
@@ -19,10 +17,11 @@ var Transport = assign(Class({ className: 'Transport',
19
17
  this._dispatcher = dispatcher;
20
18
  this.endpoint = endpoint;
21
19
  this._outbox = [];
22
- this._proxy = assign({}, this._dispatcher.proxy);
20
+ this._proxy = Object.assign({}, this._dispatcher.proxy);
23
21
 
24
- if (!this._proxy.origin)
22
+ if (!this._proxy.origin) {
25
23
  this._proxy.origin = this._findProxy();
24
+ }
26
25
  },
27
26
 
28
27
  close: function() {},
@@ -40,11 +39,12 @@ var Transport = assign(Class({ className: 'Transport',
40
39
  this._outbox.push(message);
41
40
  this._flushLargeBatch();
42
41
 
43
- if (message.channel === Channel.HANDSHAKE)
42
+ if (message.channel === Channel.HANDSHAKE) {
44
43
  return this._publish(0.01);
45
-
46
- if (message.channel === Channel.CONNECT)
44
+ }
45
+ if (message.channel === Channel.CONNECT) {
47
46
  this._connectMessage = message;
47
+ }
48
48
 
49
49
  return this._publish(this.MAX_DELAY);
50
50
  },
@@ -71,8 +71,9 @@ var Transport = assign(Class({ className: 'Transport',
71
71
  _flush: function() {
72
72
  this.removeTimeout('publish');
73
73
 
74
- if (this._outbox.length > 1 && this._connectMessage)
74
+ if (this._outbox.length > 1 && this._connectMessage) {
75
75
  this._connectMessage.advice = { timeout: 0 };
76
+ }
76
77
 
77
78
  this._resolvePromise(this.request(this._outbox));
78
79
 
@@ -98,8 +99,9 @@ var Transport = assign(Class({ className: 'Transport',
98
99
  this.debug('Client ? received from ? via ?: ?',
99
100
  this._dispatcher.clientId, this.endpoint.href, this.connectionType, replies);
100
101
 
101
- for (var i = 0, n = replies.length; i < n; i++)
102
- this._dispatcher.handleResponse(replies[i]);
102
+ for (let reply of replies) {
103
+ this._dispatcher.handleResponse(reply);
104
+ }
103
105
  },
104
106
 
105
107
  _handleError: function(messages, immediate) {
@@ -108,8 +110,9 @@ var Transport = assign(Class({ className: 'Transport',
108
110
  this.debug('Client ? failed to send to ? via ?: ?',
109
111
  this._dispatcher.clientId, this.endpoint.href, this.connectionType, messages);
110
112
 
111
- for (var i = 0, n = messages.length; i < n; i++)
112
- this._dispatcher.handleError(messages[i]);
113
+ for (let message of messages) {
114
+ this._dispatcher.handleError(message);
115
+ }
113
116
  },
114
117
 
115
118
  _getCookies: function() {
@@ -131,8 +134,8 @@ var Transport = assign(Class({ className: 'Transport',
131
134
  if (!setCookie || !cookies) return;
132
135
  setCookie = [].concat(setCookie);
133
136
 
134
- for (var i = 0, n = setCookie.length; i < n; i++) {
135
- cookie = Cookie.parse(setCookie[i]);
137
+ for (let cookieStr of setCookie) {
138
+ cookie = Cookie.parse(cookieStr);
136
139
  cookies.setCookieSync(cookie, url);
137
140
  }
138
141
  },
@@ -151,17 +154,19 @@ var Transport = assign(Class({ className: 'Transport',
151
154
  if (name === 'http_proxy' && env.REQUEST_METHOD) {
152
155
  keys = Object.keys(env).filter(function(k) { return /^http_proxy$/i.test(k) });
153
156
  if (keys.length === 1) {
154
- if (keys[0] === name && env[upcase] === undefined)
157
+ if (keys[0] === name && env[upcase] === undefined) {
155
158
  proxy = env[name];
159
+ }
156
160
  } else if (keys.length > 1) {
157
161
  proxy = env[name];
158
162
  }
159
163
  proxy = proxy || env['CGI_' + upcase];
160
164
  } else {
161
165
  proxy = env[name] || env[upcase];
162
- if (proxy && !env[name])
166
+ if (proxy && !env[name]) {
163
167
  console.warn('The environment variable ' + upcase +
164
168
  ' is discouraged. Use ' + name + '.');
169
+ }
165
170
  }
166
171
  return proxy;
167
172
  }
@@ -174,9 +179,9 @@ var Transport = assign(Class({ className: 'Transport',
174
179
  var connType = pair[0], klass = pair[1],
175
180
  connEndpoint = dispatcher.endpointFor(connType);
176
181
 
177
- if (array.indexOf(disabled, connType) >= 0)
182
+ if (array.indexOf(disabled, connType) >= 0) {
178
183
  return resume();
179
-
184
+ }
180
185
  if (array.indexOf(allowed, connType) < 0) {
181
186
  klass.isUsable(dispatcher, connEndpoint, function() {});
182
187
  return resume();
@@ -204,14 +209,15 @@ var Transport = assign(Class({ className: 'Transport',
204
209
  disable: function(feature) {
205
210
  if (feature !== 'autodisconnect') return;
206
211
 
207
- for (var i = 0; i < this._transports.length; i++)
208
- this._transports[i][1]._unloaded = false;
212
+ for (let transport of this._transports) {
213
+ transport[1]._unloaded = false;
214
+ }
209
215
  },
210
216
 
211
217
  _transports: []
212
218
  });
213
219
 
214
- assign(Transport.prototype, Logging);
215
- assign(Transport.prototype, Timeouts);
220
+ Object.assign(Transport.prototype, Logging);
221
+ Object.assign(Transport.prototype, Timeouts);
216
222
 
217
223
  module.exports = Transport;
@@ -1,18 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  var Class = require('../util/class'),
4
- Promise = require('../util/promise'),
5
4
  Set = require('../util/set'),
6
5
  URI = require('../util/uri'),
7
6
  browser = require('../util/browser'),
8
- copyObject = require('../util/copy_object'),
9
- assign = require('../util/assign'),
10
7
  toJSON = require('../util/to_json'),
11
8
  ws = require('../util/websocket'),
12
9
  Deferrable = require('../mixins/deferrable'),
13
10
  Transport = require('./transport');
14
11
 
15
- var WebSocket = assign(Class(Transport, {
12
+ var WebSocket = Object.assign(Class(Transport, {
16
13
  UNCONNECTED: 1,
17
14
  CONNECTING: 2,
18
15
  CONNECTED: 3,
@@ -27,7 +24,7 @@ var WebSocket = assign(Class(Transport, {
27
24
 
28
25
  request: function(messages) {
29
26
  this._pending = this._pending || new Set();
30
- for (var i = 0, n = messages.length; i < n; i++) this._pending.add(messages[i]);
27
+ for (let message of messages) this._pending.add(message);
31
28
 
32
29
  var self = this;
33
30
 
@@ -77,7 +74,7 @@ var WebSocket = assign(Class(Transport, {
77
74
  delete self._socket;
78
75
  self._state = self.UNCONNECTED;
79
76
 
80
- var pending = self._pending ? self._pending.toArray() : [];
77
+ var pending = self._pending ? [...self._pending] : [];
81
78
  delete self._pending;
82
79
 
83
80
  if (wasConnected || self._everConnected) {
@@ -96,9 +93,9 @@ var WebSocket = assign(Class(Transport, {
96
93
 
97
94
  replies = [].concat(replies);
98
95
 
99
- for (var i = 0, n = replies.length; i < n; i++) {
100
- if (replies[i].successful === undefined) continue;
101
- self._pending.remove(replies[i]);
96
+ for (let reply of replies) {
97
+ if (reply.successful === undefined) continue;
98
+ self._pending.remove(reply);
102
99
  }
103
100
  self._receive(replies);
104
101
  };
@@ -133,13 +130,16 @@ var WebSocket = assign(Class(Transport, {
133
130
  },
134
131
 
135
132
  create: function(dispatcher, endpoint) {
136
- var sockets = dispatcher.transports.websocket = dispatcher.transports.websocket || {};
137
- sockets[endpoint.href] = sockets[endpoint.href] || new this(dispatcher, endpoint);
138
- return sockets[endpoint.href];
133
+ var transports = dispatcher.transports,
134
+ sockets = transports.websocket = transports.websocket || new Map();
135
+
136
+ if (!sockets.has(endpoint.href)) sockets.set(endpoint.href, new this(dispatcher, endpoint));
137
+
138
+ return sockets.get(endpoint.href);
139
139
  },
140
140
 
141
141
  getSocketUrl: function(endpoint) {
142
- endpoint = copyObject(endpoint);
142
+ endpoint = URI.clone(endpoint);
143
143
  endpoint.protocol = this.PROTOCOLS[endpoint.protocol];
144
144
  return URI.stringify(endpoint);
145
145
  },
@@ -149,12 +149,13 @@ var WebSocket = assign(Class(Transport, {
149
149
  }
150
150
  });
151
151
 
152
- assign(WebSocket.prototype, Deferrable);
152
+ Object.assign(WebSocket.prototype, Deferrable);
153
153
 
154
154
  if (browser.Event && global.onbeforeunload !== undefined) {
155
155
  browser.Event.on(global, 'beforeunload', function() {
156
- if (WebSocket._unloaded === undefined)
156
+ if (WebSocket._unloaded === undefined) {
157
157
  WebSocket._unloaded = true;
158
+ }
158
159
  });
159
160
  }
160
161
 
@@ -3,11 +3,10 @@
3
3
  var Class = require('../util/class'),
4
4
  URI = require('../util/uri'),
5
5
  browser = require('../util/browser'),
6
- assign = require('../util/assign'),
7
6
  toJSON = require('../util/to_json'),
8
7
  Transport = require('./transport');
9
8
 
10
- var XHR = assign(Class(Transport, {
9
+ var XHR = Object.assign(Class(Transport, {
11
10
  encode: function(messages) {
12
11
  return toJSON(messages);
13
12
  },
@@ -32,14 +31,14 @@ var XHR = assign(Class(Transport, {
32
31
  xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
33
32
 
34
33
  var headers = this._dispatcher.headers;
35
- for (var key in headers) {
36
- if (!headers.hasOwnProperty(key)) continue;
37
- xhr.setRequestHeader(key, headers[key]);
34
+ for (let [key, value] of Object.entries(headers)) {
35
+ xhr.setRequestHeader(key, value);
38
36
  }
39
37
 
40
38
  var abort = function() { xhr.abort() };
41
- if (global.onbeforeunload !== undefined)
39
+ if (global.onbeforeunload !== undefined) {
42
40
  browser.Event.on(global, 'beforeunload', abort);
41
+ }
43
42
 
44
43
  xhr.onreadystatechange = function() {
45
44
  if (!xhr || xhr.readyState !== 4) return;
@@ -49,8 +48,9 @@ var XHR = assign(Class(Transport, {
49
48
  text = xhr.responseText,
50
49
  successful = (status >= 200 && status < 300) || status === 304 || status === 1223;
51
50
 
52
- if (global.onbeforeunload !== undefined)
51
+ if (global.onbeforeunload !== undefined) {
53
52
  browser.Event.detach(global, 'beforeunload', abort);
53
+ }
54
54
 
55
55
  xhr.onreadystatechange = function() {};
56
56
  xhr = null;
@@ -61,10 +61,11 @@ var XHR = assign(Class(Transport, {
61
61
  replies = JSON.parse(text);
62
62
  } catch (error) {}
63
63
 
64
- if (replies)
64
+ if (replies) {
65
65
  self._receive(replies);
66
- else
66
+ } else {
67
67
  self._handleError(messages);
68
+ }
68
69
  };
69
70
 
70
71
  xhr.send(this.encode(messages));
package/src/util/array.js CHANGED
@@ -3,8 +3,9 @@
3
3
  module.exports = {
4
4
  commonElement: function(lista, listb) {
5
5
  for (var i = 0, n = lista.length; i < n; i++) {
6
- if (this.indexOf(listb, lista[i]) !== -1)
6
+ if (this.indexOf(listb, lista[i]) !== -1) {
7
7
  return lista[i];
8
+ }
8
9
  }
9
10
  return null;
10
11
  },
@@ -27,9 +28,8 @@ module.exports = {
27
28
  result.push(callback.call(context || null, object[i], i));
28
29
  }
29
30
  } else {
30
- for (var key in object) {
31
- if (!object.hasOwnProperty(key)) continue;
32
- result.push(callback.call(context || null, key, object[key]));
31
+ for (let [key, value] of Object.entries(object)) {
32
+ result.push(callback.call(context || null, key, value));
33
33
  }
34
34
  }
35
35
  return result;
@@ -39,8 +39,9 @@ module.exports = {
39
39
  if (array.filter) return array.filter(callback, context);
40
40
  var result = [];
41
41
  for (var i = 0, n = array.length; i < n; i++) {
42
- if (callback.call(context || null, array[i], i))
42
+ if (callback.call(context || null, array[i], i)) {
43
43
  result.push(array[i]);
44
+ }
44
45
  }
45
46
  return result;
46
47
  },
@@ -6,10 +6,11 @@ var Event = {
6
6
  on: function(element, eventName, callback, context) {
7
7
  var wrapped = function() { callback.call(context) };
8
8
 
9
- if (element.addEventListener)
9
+ if (element.addEventListener) {
10
10
  element.addEventListener(eventName, wrapped, false);
11
- else
11
+ } else {
12
12
  element.attachEvent('on' + eventName, wrapped);
13
+ }
13
14
 
14
15
  this._registry.push({
15
16
  _element: element,
@@ -31,10 +32,11 @@ var Event = {
31
32
  (context && context !== register._context))
32
33
  continue;
33
34
 
34
- if (register._element.removeEventListener)
35
+ if (register._element.removeEventListener) {
35
36
  register._element.removeEventListener(register._type, register._handler, false);
36
- else
37
+ } else {
37
38
  register._element.detachEvent('on' + register._type, register._handler);
39
+ }
38
40
 
39
41
  this._registry.splice(i,1);
40
42
  register = null;
package/src/util/class.js CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var assign = require('./assign');
4
-
5
3
  module.exports = function(parent, methods) {
6
4
  if (typeof parent !== 'function') {
7
5
  methods = parent;
@@ -17,7 +15,7 @@ module.exports = function(parent, methods) {
17
15
  bridge.prototype = parent.prototype;
18
16
 
19
17
  klass.prototype = new bridge();
20
- assign(klass.prototype, methods);
18
+ Object.assign(klass.prototype, methods);
21
19
 
22
20
  return klass;
23
21
  };
@@ -1,5 +1,5 @@
1
1
  module.exports = {
2
- VERSION: '1.4.1',
2
+ VERSION: '1.4.2',
3
3
 
4
4
  BAYEUX_VERSION: '1.0',
5
5
  ID_LENGTH: 160,
@@ -1,7 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  var copyObject = function(object) {
4
- var clone, i, key;
4
+ var clone, i;
5
+ if (!object) return object;
6
+
5
7
  if (object instanceof Array) {
6
8
  clone = [];
7
9
  i = object.length;
@@ -9,7 +11,9 @@ var copyObject = function(object) {
9
11
  return clone;
10
12
  } else if (typeof object === 'object') {
11
13
  clone = (object === null) ? null : {};
12
- for (key in object) clone[key] = copyObject(object[key]);
14
+ for (let [key, value] of Object.entries(object)) {
15
+ clone[key] = copyObject(value);
16
+ }
13
17
  return clone;
14
18
  } else {
15
19
  return object;
@@ -5,21 +5,21 @@ var Class = require('./class'),
5
5
 
6
6
  module.exports = Class({
7
7
  initialize: function() {
8
- this._used = {};
8
+ this._used = new Set();
9
9
  },
10
10
 
11
11
  exists: function(id) {
12
- return this._used.hasOwnProperty(id);
12
+ return this._used.has(id);
13
13
  },
14
14
 
15
15
  generate: function() {
16
16
  var name = random();
17
- while (this._used.hasOwnProperty(name))
18
- name = random();
19
- return this._used[name] = name;
17
+ while (this._used.has(name)) name = random();
18
+ this._used.add(name);
19
+ return name;
20
20
  },
21
21
 
22
22
  release: function(id) {
23
- delete this._used[id];
23
+ this._used.delete(id);
24
24
  }
25
25
  });
package/src/util/set.js CHANGED
@@ -4,47 +4,35 @@ var Class = require('./class');
4
4
 
5
5
  module.exports = Class({
6
6
  initialize: function() {
7
- this._index = {};
7
+ this._index = new Map();
8
8
  },
9
9
 
10
10
  add: function(item) {
11
11
  var key = (item.id !== undefined) ? item.id : item;
12
- if (this._index.hasOwnProperty(key)) return false;
13
- this._index[key] = item;
12
+ if (this._index.has(key)) return false;
13
+ this._index.set(key, item);
14
14
  return true;
15
15
  },
16
16
 
17
- forEach: function(block, context) {
18
- for (var key in this._index) {
19
- if (this._index.hasOwnProperty(key))
20
- block.call(context, this._index[key]);
21
- }
17
+ [Symbol.iterator]: function() {
18
+ return this._index.values();
22
19
  },
23
20
 
24
21
  isEmpty: function() {
25
- for (var key in this._index) {
26
- if (this._index.hasOwnProperty(key)) return false;
27
- }
28
- return true;
22
+ return this._index.size === 0;
29
23
  },
30
24
 
31
25
  member: function(item) {
32
- for (var key in this._index) {
33
- if (this._index[key] === item) return true;
26
+ for (let value of this._index.values()) {
27
+ if (value === item) return true;
34
28
  }
35
29
  return false;
36
30
  },
37
31
 
38
32
  remove: function(item) {
39
33
  var key = (item.id !== undefined) ? item.id : item;
40
- var removed = this._index[key];
41
- delete this._index[key];
34
+ var removed = this._index.get(key);
35
+ this._index.delete(key);
42
36
  return removed;
43
- },
44
-
45
- toArray: function() {
46
- var array = [];
47
- this.forEach(function(item) { array.push(item) });
48
- return array;
49
37
  }
50
38
  });