Bug 1571479 - Deprioritize getting state for about:newtab pages that aren't visible. r=k88hudson
authorMike Conley <mconley@mozilla.com>
Tue, 07 Apr 2020 15:01:54 +0000
changeset 522713 35e472faa9f4ec742f3f1de00ecaf98e56de706a
parent 522712 1d09a2030d3588a916ba2d29beca1307420c26a0
child 522714 90e193cc5064eae2004bde7367f2ec77b780b79f
push id37293
push userapavel@mozilla.com
push dateTue, 07 Apr 2020 21:44:02 +0000
treeherdermozilla-central@de63e64b9090 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersk88hudson
bugs1571479
milestone77.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1571479 - Deprioritize getting state for about:newtab pages that aren't visible. r=k88hudson Differential Revision: https://phabricator.services.mozilla.com/D63531
browser/components/newtab/content-src/activity-stream.jsx
browser/components/newtab/data/content/activity-stream.bundle.js
--- a/browser/components/newtab/content-src/activity-stream.jsx
+++ b/browser/components/newtab/content-src/activity-stream.jsx
@@ -10,17 +10,37 @@ import { Provider } from "react-redux";
 import React from "react";
 import ReactDOM from "react-dom";
 import { reducers } from "common/Reducers.jsm";
 
 const store = initStore(reducers);
 
 new DetectUserSessionStart(store).sendEventOrAddListener();
 
-store.dispatch(ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }));
+// If this document has already gone into the background by the time we've reached
+// here, we can deprioritize requesting the initial state until the event loop
+// frees up. If, however, the visibility changes, we then send the request.
+let didRequest = false;
+let requestIdleCallbackId = 0;
+function doRequest() {
+  if (!didRequest) {
+    if (requestIdleCallbackId) {
+      cancelIdleCallback(requestIdleCallbackId);
+    }
+    didRequest = true;
+    store.dispatch(ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST }));
+  }
+}
+
+if (document.hidden) {
+  requestIdleCallbackId = requestIdleCallback(doRequest);
+  addEventListener("visibilitychange", doRequest, { once: true });
+} else {
+  doRequest();
+}
 
 ReactDOM.hydrate(
   <Provider store={store}>
     <Base
       isFirstrun={global.document.location.href === "about:welcome"}
       strings={global.gActivityStreamStrings}
     />
   </Provider>,
--- a/browser/components/newtab/data/content/activity-stream.bundle.js
+++ b/browser/components/newtab/data/content/activity-stream.bundle.js
@@ -109,20 +109,45 @@
 
 
 
 
 
 
 
 const store = Object(content_src_lib_init_store__WEBPACK_IMPORTED_MODULE_3__["initStore"])(common_Reducers_jsm__WEBPACK_IMPORTED_MODULE_7__["reducers"]);
-new content_src_lib_detect_user_session_start__WEBPACK_IMPORTED_MODULE_2__["DetectUserSessionStart"](store).sendEventOrAddListener();
-store.dispatch(common_Actions_jsm__WEBPACK_IMPORTED_MODULE_0__["actionCreators"].AlsoToMain({
-  type: common_Actions_jsm__WEBPACK_IMPORTED_MODULE_0__["actionTypes"].NEW_TAB_STATE_REQUEST
-}));
+new content_src_lib_detect_user_session_start__WEBPACK_IMPORTED_MODULE_2__["DetectUserSessionStart"](store).sendEventOrAddListener(); // If this document has already gone into the background by the time we've reached
+// here, we can deprioritize requesting the initial state until the event loop
+// frees up. If, however, the visibility changes, we then send the request.
+
+let didRequest = false;
+let requestIdleCallbackId = 0;
+
+function doRequest() {
+  if (!didRequest) {
+    if (requestIdleCallbackId) {
+      cancelIdleCallback(requestIdleCallbackId);
+    }
+
+    didRequest = true;
+    store.dispatch(common_Actions_jsm__WEBPACK_IMPORTED_MODULE_0__["actionCreators"].AlsoToMain({
+      type: common_Actions_jsm__WEBPACK_IMPORTED_MODULE_0__["actionTypes"].NEW_TAB_STATE_REQUEST
+    }));
+  }
+}
+
+if (document.hidden) {
+  requestIdleCallbackId = requestIdleCallback(doRequest);
+  addEventListener("visibilitychange", doRequest, {
+    once: true
+  });
+} else {
+  doRequest();
+}
+
 react_dom__WEBPACK_IMPORTED_MODULE_6___default.a.hydrate(react__WEBPACK_IMPORTED_MODULE_5___default.a.createElement(react_redux__WEBPACK_IMPORTED_MODULE_4__["Provider"], {
   store: store
 }, react__WEBPACK_IMPORTED_MODULE_5___default.a.createElement(content_src_components_Base_Base__WEBPACK_IMPORTED_MODULE_1__["Base"], {
   isFirstrun: global.document.location.href === "about:welcome",
   strings: global.gActivityStreamStrings
 })), document.getElementById("root"));
 /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(1)))