Skip to content

Commit

Permalink
Disable macro-after-long-task in inabox. (#26459)
Browse files Browse the repository at this point in the history
  • Loading branch information
lannka authored Jan 23, 2020
1 parent 2f29293 commit 0ce5f2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const TAG = 'CHUNK';
* @type {boolean}
*/
let deactivated = /nochunking=1/.test(self.location.hash);
let allowLongTasks = false;

/**
* @const {!Promise}
Expand Down Expand Up @@ -114,6 +115,14 @@ export function deactivateChunking() {
deactivated = true;
}

/**
* Allow continuing macro tasks after a long task (>5ms).
* In particular this is the case when AMP runs in the `amp-inabox` ads mode.
*/
export function allowLongTasksInChunking() {
allowLongTasks = true;
}

/**
* @visibleForTesting
*/
Expand Down Expand Up @@ -443,7 +452,11 @@ class Chunks {
// If we've spent over 5 millseconds executing the
// last instruction yeild back to the main thread.
// 5 milliseconds is a magic number.
if (this.bodyIsVisible_ && this.durationOfLastExecution_ > 5) {
if (
!allowLongTasks &&
this.bodyIsVisible_ &&
this.durationOfLastExecution_ > 5
) {
this.durationOfLastExecution_ = 0;
this.requestMacroTask_();
return;
Expand Down
3 changes: 2 additions & 1 deletion src/inabox/amp-inabox.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import '../polyfills';
import {Navigation} from '../service/navigation';
import {Services} from '../services';
import {adopt} from '../runtime';
import {allowLongTasksInChunking, startupChunk} from '../chunk';
import {cssText as ampSharedCss} from '../../build/ampshared.css';
import {doNotTrackImpression} from '../impression';
import {fontStylesheetTimeout} from '../font-stylesheet-timeout';
Expand All @@ -43,7 +44,6 @@ import {
} from '../style-installer';
import {internalRuntimeVersion} from '../internal-version';
import {maybeValidate} from '../validator-integration';
import {startupChunk} from '../chunk';
import {stubElementsForDoc} from '../service/custom-element-registry';

getMode(self).runtime = 'inabox';
Expand All @@ -69,6 +69,7 @@ try {
makeBodyVisibleRecovery(self.document);
throw e;
}
allowLongTasksInChunking();
startupChunk(self.document, function initial() {
/** @const {!../service/ampdoc-impl.AmpDoc} */
const ampdoc = ampdocService.getAmpDoc(self.document);
Expand Down

0 comments on commit 0ce5f2f

Please sign in to comment.