Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Use async.run to schedule the next func call instead of setTimeout(0)…
Browse files Browse the repository at this point in the history
… to avoid throttling in background tabs.

RELNOTES: improved webchannel responsiveness in background tabs.

Tested: //firebase/datastore/clients/web/testing/integration:all

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=235186114
  • Loading branch information
nreid260 committed Feb 23, 2019
1 parent 80defe9 commit a2daa5f
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions closure/goog/labs/net/webchannel/webchannelbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ goog.labs.net.webChannel.WebChannelBase = function(

/**
* Timer identifier for asynchronously making a forward channel request.
* @private {?number}
* This is set to true if the func is scheduled with async.run, which
* is equivalent to setTimeout(0).
* @private {?number|?boolean}
*/
this.forwardChannelTimerId_ = null;

Expand Down Expand Up @@ -740,9 +742,21 @@ WebChannelBase.prototype.cancelRequests_ = function() {
this.forwardChannelRequestPool_.cancel();

if (this.forwardChannelTimerId_) {
this.clearForwardChannelTimer_();
}
};


/**
* Clears the forward channel timer.
* @private
*/
WebChannelBase.prototype.clearForwardChannelTimer_ = function() {
if (goog.isNumber(this.forwardChannelTimerId_)) {
goog.global.clearTimeout(this.forwardChannelTimerId_);
this.forwardChannelTimerId_ = null;
}

this.forwardChannelTimerId_ = null;
};


Expand Down Expand Up @@ -999,8 +1013,7 @@ WebChannelBase.prototype.setFailFast = function(failFast) {
if (!this.forwardChannelRequestPool_.forceComplete(
goog.bind(this.onRequestComplete, this))) {
// i.e., this.forwardChannelTimerId_
goog.global.clearTimeout(this.forwardChannelTimerId_);
this.forwardChannelTimerId_ = null;
this.clearForwardChannelTimer_();
// The error code from the last failed request is gone, so just use a
// generic one.
this.signalError_(WebChannelBase.Error.REQUEST_FAILED);
Expand Down Expand Up @@ -1119,8 +1132,11 @@ WebChannelBase.prototype.ensureForwardChannel_ = function() {
return;
}

this.forwardChannelTimerId_ = requestStats.setTimeout(
goog.bind(this.onStartForwardChannelTimer_, this), 0);
// Use async.run instead of setTimeout(0) to avoid the 1s message delay
// from chrome/firefox background tabs
this.forwardChannelTimerId_ = true;
goog.async.run(this.onStartForwardChannelTimer_, this);

this.forwardChannelRetryCount_ = 0;
};

Expand Down Expand Up @@ -1174,8 +1190,11 @@ WebChannelBase.prototype.maybeRetryForwardChannel_ = function(request) {
*/
WebChannelBase.prototype.onStartForwardChannelTimer_ = function(
opt_retryRequest) {
this.forwardChannelTimerId_ = null;
this.startForwardChannel_(opt_retryRequest);
// null is possible if scheduled with async.run
if (this.forwardChannelTimerId_) {
this.forwardChannelTimerId_ = null;
this.startForwardChannel_(opt_retryRequest);
}
};


Expand Down

0 comments on commit a2daa5f

Please sign in to comment.