Skip to content

Commit

Permalink
Fix cyclic dependency. We need to keep the popup-messaging file to ha…
Browse files Browse the repository at this point in the history
…ndle

static access to the messaging port.
  • Loading branch information
sammacbeth committed Jan 24, 2025
1 parent a006817 commit 7665739
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion shared/js/background/before-request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import browser from 'webextension-polyfill';
import EventEmitter2 from 'eventemitter2';
import ATB from './atb';
import { postPopupMessage } from './components/message-router';
import { postPopupMessage } from './popup-messaging';

const utils = require('./utils');
const trackers = require('./trackers');
Expand Down
2 changes: 1 addition & 1 deletion shared/js/background/companies.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const TopBlocked = require('./classes/top-blocked');
const Company = require('./classes/company');
const browserWrapper = require('./wrapper');
const { postPopupMessage } = require('./components/message-router');
const { postPopupMessage } = require('./popup-messaging');

const Companies = (() => {
let companyContainer = {};
Expand Down
19 changes: 4 additions & 15 deletions shared/js/background/components/message-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import browser from 'webextension-polyfill';
import messageHandlers from '../message-handlers';
import { getExtensionId } from '../wrapper';
import { getBrowserName } from '../utils';
import { getActivePort, setActivePort } from '../popup-messaging';

/**
* @typedef {import('webextension-polyfill').Runtime.Port} Port
Expand All @@ -19,9 +20,6 @@ export class MessageReceivedEvent extends CustomEvent {
}
}

/** @type {Port?} */
let activePort = null;

export default class MessageRouter extends EventTarget {
constructor({ tabManager }) {
super();
Expand Down Expand Up @@ -92,10 +90,10 @@ export default class MessageRouter extends EventTarget {
* @param {Port} port
*/
popupConnectionOpened(port) {
activePort = port;
setActivePort(port);
port.onDisconnect.addListener(() => {
if (activePort === port) {
activePort = null;
if (getActivePort() === port) {
setActivePort(null);
}
});

Expand All @@ -119,12 +117,3 @@ export default class MessageRouter extends EventTarget {
});
}
}

/**
* Post a message to the popup UI, if it's open.
*
* @param {OutgoingPopupMessage} message
*/
export function postPopupMessage(message) {
activePort?.postMessage(message);
}
2 changes: 1 addition & 1 deletion shared/js/background/components/toggle-reports.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill';
import { registerMessageHandler } from '../message-handlers';
import { postPopupMessage } from './message-router';
import { postPopupMessage } from '../popup-messaging';
import settings from '../settings';
import { getFeatureSettings, reloadCurrentTab, resolveAfterDelay } from '../utils';
import { getDisclosureDetails, sendBreakageReportForCurrentTab } from '../broken-site-report';
Expand Down
2 changes: 1 addition & 1 deletion shared/js/background/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import httpsStorage from './storage/https';
import ATB from './atb';
import { clearExpiredBrokenSiteReportTimes } from './broken-site-report';
import { sendPageloadsWithAdAttributionPixelAndResetCount } from './classes/ad-click-attribution-policy';
import { postPopupMessage } from './components/message-router';
import { postPopupMessage } from './popup-messaging';
const utils = require('./utils');
const experiment = require('./experiments');
const settings = require('./settings');
Expand Down
2 changes: 1 addition & 1 deletion shared/js/background/message-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ensureClickToLoadRuleActionDisabled } from './dnr-click-to-load';
import tdsStorage from './storage/tds';
import { getArgumentsObject } from './helpers/arguments-object';
import { isFireButtonEnabled } from './components/fire-button';
import { postPopupMessage } from './components/message-router';
import { postPopupMessage } from './popup-messaging';
import ToggleReports from './components/toggle-reports';
const utils = require('./utils');
const settings = require('./settings');
Expand Down
29 changes: 29 additions & 0 deletions shared/js/background/popup-messaging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @typedef {import('webextension-polyfill').Runtime.Port} Port
* @typedef {import('@duckduckgo/privacy-dashboard/schema/__generated__/schema.types').IncomingExtensionMessage} OutgoingPopupMessage
*/

// Messaging connection with the popup UI (when active).
/** @type {Port?} */
let activePort = null;

export function getActivePort() {
return activePort;
}

/**
*
* @param {Port?} port
*/
export function setActivePort(port) {
activePort = port;
}

/**
* Post a message to the popup UI, if it's open.
*
* @param {OutgoingPopupMessage} message
*/
export function postPopupMessage(message) {
activePort?.postMessage(message);
}

0 comments on commit 7665739

Please sign in to comment.