activity-stream.jsx (2756B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs"; 6 import { Base } from "content-src/components/Base/Base"; 7 import { DetectUserSessionStart } from "content-src/lib/detect-user-session-start"; 8 import { initStore } from "content-src/lib/init-store"; 9 import { Provider } from "react-redux"; 10 import React from "react"; 11 import ReactDOM from "react-dom"; 12 import { reducers } from "common/Reducers.sys.mjs"; 13 14 export const NewTab = ({ store }) => ( 15 <Provider store={store}> 16 <Base /> 17 </Provider> 18 ); 19 20 function doRequestWhenReady() { 21 // If this document has already gone into the background by the time we've reached 22 // here, we can deprioritize the request until the event loop 23 // frees up. If, however, the visibility changes, we then send the request. 24 const doRequestPromise = new Promise(resolve => { 25 let didRequest = false; 26 let requestIdleCallbackId = 0; 27 function doRequest() { 28 if (!didRequest) { 29 if (requestIdleCallbackId) { 30 cancelIdleCallback(requestIdleCallbackId); 31 } 32 didRequest = true; 33 resolve(); 34 } 35 } 36 37 if (document.hidden) { 38 requestIdleCallbackId = requestIdleCallback(doRequest); 39 addEventListener("visibilitychange", doRequest, { once: true }); 40 } else { 41 resolve(); 42 } 43 }); 44 45 return doRequestPromise; 46 } 47 48 export function renderWithoutState() { 49 const store = initStore(reducers); 50 new DetectUserSessionStart(store).sendEventOrAddListener(); 51 52 doRequestWhenReady().then(() => { 53 // If state events happened before we got here, we can request state again. 54 store.dispatch(ac.AlsoToMain({ type: at.NEW_TAB_STATE_REQUEST })); 55 // If we rendered without state, we don't need the startup cache. 56 store.dispatch( 57 ac.OnlyToMain({ type: at.NEW_TAB_STATE_REQUEST_WITHOUT_STARTUPCACHE }) 58 ); 59 }); 60 61 ReactDOM.hydrate(<NewTab store={store} />, document.getElementById("root")); 62 } 63 64 export function renderCache(initialState) { 65 if (initialState) { 66 initialState.App.isForStartupCache.App = false; 67 } 68 const store = initStore(reducers, initialState); 69 new DetectUserSessionStart(store).sendEventOrAddListener(); 70 71 doRequestWhenReady().then(() => { 72 // If state events happened before we got here, 73 // we can notify main that we need updates. 74 // The individual feeds know what state is not cached. 75 store.dispatch( 76 ac.OnlyToMain({ type: at.NEW_TAB_STATE_REQUEST_STARTUPCACHE }) 77 ); 78 }); 79 80 ReactDOM.hydrate(<NewTab store={store} />, document.getElementById("root")); 81 }