Skip to content

Commit

Permalink
Add logging to background service worker (#1760)
Browse files Browse the repository at this point in the history
* Add logging in background service worker

* Create ninety-sheep-end.md
  • Loading branch information
Methuselah96 authored Sep 16, 2024
1 parent 3c39eb4 commit eb3ac09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-sheep-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'remotedev-redux-devtools-extension': patch
---

Add logging to background service worker
13 changes: 13 additions & 0 deletions extension/src/background/store/apiMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ type MonitorAction<S, A extends Action<string>> =
const maxChromeMsgSize = 32 * 1024 * 1024;

function toMonitors<S, A extends Action<string>>(action: MonitorAction<S, A>) {
console.log(`Message to monitors: ${action.type}`);

for (const port of Object.values(connections.panel)) {
try {
port.postMessage(action);
Expand Down Expand Up @@ -315,6 +317,8 @@ interface ImportMessage {
type ToContentScriptMessage = ImportMessage | LiftedActionAction;

function toContentScript(messageBody: ToContentScriptMessage) {
console.log(`Message to tab ${messageBody.id}: ${messageBody.message}`);

if (messageBody.message === 'DISPATCH') {
const { message, action, id, instanceId, state } = messageBody;
connections.tab[id!].postMessage({
Expand Down Expand Up @@ -389,6 +393,8 @@ function toContentScript(messageBody: ToContentScriptMessage) {
}

function toAllTabs(msg: TabMessage) {
console.log(`Message to all tabs: ${msg.type}`);

for (const tabPort of Object.values(connections.tab)) {
tabPort.postMessage(msg);
}
Expand Down Expand Up @@ -435,6 +441,7 @@ function messaging<S, A extends Action<string>>(
sender: chrome.runtime.MessageSender,
) {
let tabId = getId(sender);
console.log(`Message from tab ${tabId}: ${request.type ?? request.split}`);
if (!tabId) return;
if (sender.frameId) tabId = `${tabId}-${sender.frameId}`;

Expand Down Expand Up @@ -517,6 +524,8 @@ function disconnect(
listener: (message: any, port: chrome.runtime.Port) => void,
) {
return function disconnectListener() {
console.log(`Disconnected from ${type} ${id}`);

const p = connections[type][id];
if (listener && p) p.onMessage.removeListener(listener);
if (p) p.onDisconnect.removeListener(disconnectListener);
Expand All @@ -541,10 +550,12 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {

if (port.name === 'tab') {
id = getId(port.sender!);
console.log(`Connected to tab ${id}`);
if (port.sender!.frameId) id = `${id}-${port.sender!.frameId}`;
connections.tab[id] = port;
listener = (msg: ContentScriptToBackgroundMessage<S, A> | 'heartbeat') => {
if (msg === 'heartbeat') return;
console.log(`Message from tab ${id}: ${msg.name}`);
if (msg.name === 'INIT_INSTANCE') {
if (typeof id === 'number') {
chrome.action.enable(id);
Expand Down Expand Up @@ -578,11 +589,13 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
} else if (port.name && port.name.indexOf('monitor') === 0) {
// devpanel
id = getId(port.sender!, port.name);
console.log(`Connected to monitor ${id}`);
connections.panel[id] = port;
monitors++;
toAllTabs({ type: 'START' });
listener = (msg: BackgroundAction | 'heartbeat') => {
if (msg === 'heartbeat') return;
console.log(`Message from monitor ${id}: ${msg.type}`);
store.dispatch(msg);
};
port.onMessage.addListener(listener);
Expand Down

0 comments on commit eb3ac09

Please sign in to comment.