Chromium Code Reviews| Index: remoting/webapp/crd/js/smart_reconnector.js |
| diff --git a/remoting/webapp/crd/js/smart_reconnector.js b/remoting/webapp/crd/js/smart_reconnector.js |
| index 15973323fda52c8e206f903adcc4526ff078508a..5fb2358e1231c09a6b89fe60beb54a60b40a1ca5 100644 |
| --- a/remoting/webapp/crd/js/smart_reconnector.js |
| +++ b/remoting/webapp/crd/js/smart_reconnector.js |
| @@ -12,11 +12,13 @@ |
| * |connector| to reconnect the session. |
| */ |
| -'use strict'; |
| - |
| /** @suppress {duplicate} */ |
| var remoting = remoting || {}; |
| +(function () { |
| + |
| +'use strict'; |
| + |
| /** |
| * @constructor |
| * @param {function()} reconnectCallback |
| @@ -37,101 +39,80 @@ remoting.SmartReconnector = |
| /** @private */ |
| this.clientSession_ = clientSession; |
| - /** @private */ |
| - this.reconnectTimerId_ = null; |
| + /** @private {base.Disposable} */ |
| + this.pending_ = null; |
|
Jamie
2015/04/16 20:05:49
You're using this for two separate pending operati
kelvinp
2015/04/16 20:57:58
|this.pending_| is intended to be a placeholder of
|
| + var Events = remoting.ClientSession.Events; |
| /** @private */ |
| - this.connectionTimeoutTimerId_ = null; |
| - |
| - /** @private */ |
| - this.bound_ = { |
| - reconnect: this.reconnect_.bind(this), |
| - reconnectAsync: this.reconnectAsync_.bind(this), |
| - startReconnectTimeout: this.startReconnectTimeout_.bind(this), |
| - stateChanged: this.stateChanged_.bind(this), |
| - videoChannelStateChanged: this.videoChannelStateChanged_.bind(this) |
| - }; |
| - |
| - clientSession.addEventListener( |
| - remoting.ClientSession.Events.stateChanged, |
| - this.bound_.stateChanged); |
| - clientSession.addEventListener( |
| - remoting.ClientSession.Events.videoChannelStateChanged, |
| - this.bound_.videoChannelStateChanged); |
| + this.eventHooks_ = new base.Disposables( |
| + new base.EventHook(clientSession, Events.stateChanged, |
| + this.stateChanged_.bind(this)), |
| + new base.EventHook(clientSession, Events.videoChannelStateChanged, |
| + this.videoChannelStateChanged_.bind(this))); |
| }; |
| // The online event only means the network adapter is enabled, but |
| // it doesn't necessarily mean that we have a working internet connection. |
| -// Therefore, delay the connection by |kReconnectDelay| to allow for the network |
| -// to connect. |
| -remoting.SmartReconnector.kReconnectDelay = 2000; |
| +// Therefore, delay the connection by |RECONNECT_DELAY_MS| to allow for the |
| +// network to connect. |
| +var RECONNECT_DELAY_MS = 2000; |
| // If the video channel is inactive for 10 seconds reconnect the session. |
| -remoting.SmartReconnector.kConnectionTimeout = 10000; |
| - |
| -remoting.SmartReconnector.prototype = { |
| - reconnect_: function() { |
| - this.cancelPending_(); |
| - this.disconnectCallback_(); |
| - remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| - this.reconnectCallback_(); |
| - }, |
| - |
| - reconnectAsync_: function() { |
| - this.cancelPending_(); |
| - remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| - this.reconnectTimerId_ = window.setTimeout( |
| - this.bound_.reconnect, remoting.SmartReconnector.kReconnectDelay); |
| - }, |
| - |
| - /** |
| - * @param {remoting.ClientSession.StateEvent=} event |
| - */ |
| - stateChanged_: function(event) { |
| - var State = remoting.ClientSession.State; |
| - if (event.previous === State.CONNECTED && event.current === State.FAILED) { |
| - this.cancelPending_(); |
| - if (navigator.onLine) { |
| - this.reconnect_(); |
| - } else { |
| - window.addEventListener('online', this.bound_.reconnectAsync, false); |
| - } |
| - } |
| - }, |
| - |
| - /** |
| - * @param {boolean=} active True if the video channel is active. |
| - */ |
| - videoChannelStateChanged_: function (active) { |
| - this.cancelPending_(); |
| - if (!active) { |
| - window.addEventListener( |
| - 'online', this.bound_.startReconnectTimeout, false); |
| +var CONNECTION_TIMEOUT_MS = 10000; |
| + |
| +remoting.SmartReconnector.prototype.reconnect_ = function() { |
| + base.dispose(this.pending_); |
|
Jamie
2015/04/16 20:05:49
Reset pending_ to null, here and below.
kelvinp
2015/04/16 20:57:58
Done.
|
| + this.disconnectCallback_(); |
| + remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| + this.reconnectCallback_(); |
| +}; |
| + |
| +remoting.SmartReconnector.prototype.reconnectAsync_ = function() { |
| + base.dispose(this.pending_); |
| + remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); |
| + this.pending_ = |
| + new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS); |
| +}; |
| + |
| +/** |
| + * @param {remoting.ClientSession.StateEvent=} event |
| + */ |
| +remoting.SmartReconnector.prototype.stateChanged_ = function(event) { |
| + var State = remoting.ClientSession.State; |
| + if (event.previous === State.CONNECTED && event.current === State.FAILED) { |
| + base.dispose(this.pending_); |
| + if (navigator.onLine) { |
| + this.reconnect_(); |
| + } else { |
| + this.pending_ = new base.DomEventHook( |
| + window, 'online', this.reconnectAsync_.bind(this), false); |
| } |
| - }, |
| - |
| - startReconnectTimeout_: function () { |
| - this.cancelPending_(); |
| - this.connectionTimeoutTimerId_ = window.setTimeout( |
| - this.bound_.reconnect, remoting.SmartReconnector.kConnectionTimeout); |
| - }, |
| - |
| - cancelPending_: function() { |
| - window.removeEventListener( |
| - 'online', this.bound_.startReconnectTimeout, false); |
| - window.removeEventListener('online', this.bound_.reconnectAsync, false); |
| - window.clearTimeout(this.reconnectTimerId_); |
| - window.clearTimeout(this.connectionTimeoutTimerId_); |
| - this.reconnectTimerId_ = null; |
| - this.connectionTimeoutTimerId_ = null; |
| - }, |
| - |
| - dispose: function() { |
| - this.clientSession_.removeEventListener( |
| - remoting.ClientSession.Events.stateChanged, |
| - this.bound_.stateChanged); |
| - this.clientSession_.removeEventListener( |
| - remoting.ClientSession.Events.videoChannelStateChanged, |
| - this.bound_.videoChannelStateChanged); |
| } |
| }; |
| + |
| +/** |
| + * @param {boolean=} active True if the video channel is active. |
| + */ |
| +remoting.SmartReconnector.prototype.videoChannelStateChanged_ = |
| + function (active) { |
| + base.dispose(this.pending_); |
| + if (!active) { |
| + this.pending_ = new base.DomEventHook( |
|
Jamie
2015/04/16 20:05:49
Indentation.
kelvinp
2015/04/16 20:57:58
Done.
Jamie
2015/04/17 00:02:47
This still looks wrong. This line is indented by 4
kelvinp
2015/04/17 00:12:53
My bad. My this.pending_ change overwrote the ind
|
| + window, 'online', this.startReconnectTimeout_.bind(this), false); |
| + } |
| +}; |
| + |
| +remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() { |
| + base.dispose(this.pending_); |
| + this.pending_ = |
| + new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS); |
| +}; |
| + |
| +remoting.SmartReconnector.prototype.dispose = function() { |
| + base.dispose(this.pending_); |
| + this.pending_ = null; |
| + base.dispose(this.eventHooks_); |
| + this.eventHooks_ = null; |
| +}; |
| + |
| +})(); |