NewTabInit.jsm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "use strict";
  5. const {actionCreators: ac, actionTypes: at} = ChromeUtils.import("resource://activity-stream/common/Actions.jsm");
  6. /**
  7. * NewTabInit - A placeholder for now. This will send a copy of the state to all
  8. * newly opened tabs.
  9. */
  10. this.NewTabInit = class NewTabInit {
  11. constructor() {
  12. this._repliedEarlyTabs = new Map();
  13. }
  14. reply(target) {
  15. // Skip this reply if we already replied to an early tab
  16. if (this._repliedEarlyTabs.get(target)) {
  17. return;
  18. }
  19. const action = {type: at.NEW_TAB_INITIAL_STATE, data: this.store.getState()};
  20. this.store.dispatch(ac.AlsoToOneContent(action, target));
  21. // Remember that this early tab has already gotten a rehydration response in
  22. // case it thought we lost its initial REQUEST and asked again
  23. if (this._repliedEarlyTabs.has(target)) {
  24. this._repliedEarlyTabs.set(target, true);
  25. }
  26. }
  27. onAction(action) {
  28. switch (action.type) {
  29. case at.NEW_TAB_STATE_REQUEST:
  30. this.reply(action.meta.fromTarget);
  31. break;
  32. case at.NEW_TAB_INIT:
  33. // Initialize data for early tabs that might REQUEST twice
  34. if (action.data.simulated) {
  35. this._repliedEarlyTabs.set(action.data.portID, false);
  36. }
  37. break;
  38. case at.NEW_TAB_UNLOAD:
  39. // Clean up for any tab (no-op if not an early tab)
  40. this._repliedEarlyTabs.delete(action.meta.fromTarget);
  41. break;
  42. }
  43. }
  44. };
  45. const EXPORTED_SYMBOLS = ["NewTabInit"];