NewTabInit.sys.mjs (1630B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { 6 actionCreators as ac, 7 actionTypes as at, 8 } from "resource://newtab/common/Actions.mjs"; 9 10 /** 11 * NewTabInit - A placeholder for now. This will send a copy of the state to all 12 * newly opened tabs. 13 */ 14 export class NewTabInit { 15 constructor() { 16 this._repliedEarlyTabs = new Map(); 17 } 18 19 reply(target) { 20 // Skip this reply if we already replied to an early tab 21 if (this._repliedEarlyTabs.get(target)) { 22 return; 23 } 24 25 const action = { 26 type: at.NEW_TAB_INITIAL_STATE, 27 data: this.store.getState(), 28 }; 29 this.store.dispatch(ac.AlsoToOneContent(action, target)); 30 31 // Remember that this early tab has already gotten a rehydration response in 32 // case it thought we lost its initial REQUEST and asked again 33 if (this._repliedEarlyTabs.has(target)) { 34 this._repliedEarlyTabs.set(target, true); 35 } 36 } 37 38 onAction(action) { 39 switch (action.type) { 40 case at.NEW_TAB_STATE_REQUEST: 41 this.reply(action.meta.fromTarget); 42 break; 43 case at.NEW_TAB_INIT: 44 // Initialize data for early tabs that might REQUEST twice 45 if (action.data.simulated) { 46 this._repliedEarlyTabs.set(action.data.portID, false); 47 } 48 break; 49 case at.NEW_TAB_UNLOAD: 50 // Clean up for any tab (no-op if not an early tab) 51 this._repliedEarlyTabs.delete(action.meta.fromTarget); 52 break; 53 } 54 } 55 }