Skip to content

Commit

Permalink
[Webapp Refactor] Fix styles in SmartReconnector to be consistent wit…
Browse files Browse the repository at this point in the history
…h the rest of the codebase.

Summary of changes:
1. Uses base.OneShotTimer
2. Uses base.EventHooks
3. Uses
     SmartReconnector.prototype.foo = function() {...}
   instead of
     SmartReconnector.prototype = {
       foo: function() {...},
     }
4 Removes this.bound_

BUG=

Review URL: https://codereview.chromium.org/1091103002

Cr-Commit-Position: refs/heads/master@{#325578}
  • Loading branch information
kelvinp authored and Commit bot committed Apr 17, 2015
1 parent 933778c commit 18bad48
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 84 deletions.
26 changes: 26 additions & 0 deletions remoting/webapp/base/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,32 @@ base.RepeatingTimer.prototype.dispose = function() {
this.intervalId_ = null;
};

/**
* A disposable one shot timer.
*
* @param {Function} callback
* @param {number} timeout
*
* @constructor
* @implements {base.Disposable}
*/
base.OneShotTimer = function(callback, timeout) {
var that = this;

/** @private */
this.timerId_ = window.setTimeout(function() {
that.timerId_ = null;
callback();
}, timeout);
};

base.OneShotTimer.prototype.dispose = function() {
if (this.timerId_ !== null) {
window.clearTimeout(this.timerId_);
this.timerId_ = null;
}
};

/**
* Converts UTF-8 string to ArrayBuffer.
*
Expand Down
159 changes: 75 additions & 84 deletions remoting/webapp/crd/js/smart_reconnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
* |connector| to reconnect the session.
*/

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

(function () {

'use strict';

/**
* @constructor
* @param {function()} reconnectCallback
Expand All @@ -37,101 +39,90 @@ remoting.SmartReconnector =
/** @private */
this.clientSession_ = clientSession;

/** @private */
this.reconnectTimerId_ = null;

/** @private */
this.connectionTimeoutTimerId_ = null;
/**
* Placeholder of any pending reconnect operations, e.g. Waiting for online,
* or a timeout to reconnect.
*
* @private {base.Disposable}
*/
this.pending_ = null;

var Events = remoting.ClientSession.Events;
/** @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_();
},
var CONNECTION_TIMEOUT_MS = 10000;

reconnectAsync_: function() {
this.cancelPending_();
remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
this.reconnectTimerId_ = window.setTimeout(
this.bound_.reconnect, remoting.SmartReconnector.kReconnectDelay);
},
remoting.SmartReconnector.prototype.reconnect_ = function() {
this.cancelPending_();
this.disconnectCallback_();
remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
this.reconnectCallback_();
};

/**
* @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);
}
}
},
remoting.SmartReconnector.prototype.reconnectAsync_ = function() {
this.cancelPending_();
remoting.setMode(remoting.AppMode.CLIENT_CONNECTING);
this.pending_ =
new base.OneShotTimer(this.reconnect_.bind(this), RECONNECT_DELAY_MS);
};

/**
* @param {boolean=} active True if the video channel is active.
*/
videoChannelStateChanged_: function (active) {
/**
* @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) {
this.cancelPending_();
if (!active) {
window.addEventListener(
'online', this.bound_.startReconnectTimeout, false);
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) {
this.cancelPending_();
if (!active) {
this.pending_ = new base.DomEventHook(
window, 'online', this.startReconnectTimeout_.bind(this), false);
}
};

remoting.SmartReconnector.prototype.startReconnectTimeout_ = function() {
this.cancelPending_();
this.pending_ =
new base.OneShotTimer(this.reconnect_.bind(this), CONNECTION_TIMEOUT_MS);
};

/** @private */
remoting.SmartReconnector.prototype.cancelPending_ = function() {
base.dispose(this.pending_);
this.pending_ = null;
};

remoting.SmartReconnector.prototype.dispose = function() {
this.cancelPending_();
base.dispose(this.eventHooks_);
this.eventHooks_ = null;
};

})();

0 comments on commit 18bad48

Please sign in to comment.