Skip to content

Commit fd9f950

Browse files
authored
Fix monitoring on opening panel (#1739)
* Fix monitoring on opening panel * Create polite-foxes-rest.md * Further cleanup * Fix * Simplify * ===
1 parent e49708d commit fd9f950

File tree

2 files changed

+14
-51
lines changed

2 files changed

+14
-51
lines changed

.changeset/polite-foxes-rest.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'remotedev-redux-devtools-extension': patch
3+
---
4+
5+
Fix monitoring on opening panel

extension/src/background/store/apiMiddleware.ts

+9-51
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ export type UpdateStateRequest<S, A extends Action<string>> =
161161
| SerializedActionMessage
162162
| SerializedStateMessage<S, A>;
163163

164-
export interface EmptyUpdateStateAction {
165-
readonly type: typeof UPDATE_STATE;
166-
}
167-
168164
interface UpdateStateAction<S, A extends Action<string>> {
169165
readonly type: typeof UPDATE_STATE;
170166
request: UpdateStateRequest<S, A>;
@@ -213,11 +209,6 @@ export type PanelMessage<S, A extends Action<string>> =
213209
export type PanelMessageWithSplitAction<S, A extends Action<string>> =
214210
| PanelMessage<S, A>
215211
| SplitUpdateStateAction<S, A>;
216-
export type MonitorMessage =
217-
| NAAction
218-
| ErrorMessage
219-
| EmptyUpdateStateAction
220-
| SetPersistAction;
221212

222213
type TabPort = Omit<chrome.runtime.Port, 'postMessage'> & {
223214
postMessage: (message: TabMessage) => void;
@@ -227,20 +218,15 @@ type PanelPort = Omit<chrome.runtime.Port, 'postMessage'> & {
227218
message: PanelMessageWithSplitAction<S, A>,
228219
) => void;
229220
};
230-
type MonitorPort = Omit<chrome.runtime.Port, 'postMessage'> & {
231-
postMessage: (message: MonitorMessage) => void;
232-
};
233221

234222
export const CONNECTED = 'socket/CONNECTED';
235223
export const DISCONNECTED = 'socket/DISCONNECTED';
236224
const connections: {
237225
readonly tab: { [K in number | string]: TabPort };
238226
readonly panel: { [K in number | string]: PanelPort };
239-
readonly monitor: { [K in number | string]: MonitorPort };
240227
} = {
241228
tab: {},
242229
panel: {},
243-
monitor: {},
244230
};
245231
const chunks: {
246232
[instanceId: string]: PageScriptToContentScriptMessageForwardedToMonitors<
@@ -249,7 +235,6 @@ const chunks: {
249235
>;
250236
} = {};
251237
let monitors = 0;
252-
let isMonitored = false;
253238

254239
const getId = (sender: chrome.runtime.MessageSender, name?: string) =>
255240
sender.tab ? sender.tab.id! : name || sender.id!;
@@ -264,10 +249,7 @@ type MonitorAction<S, A extends Action<string>> =
264249
const maxChromeMsgSize = 32 * 1024 * 1024;
265250

266251
function toMonitors<S, A extends Action<string>>(action: MonitorAction<S, A>) {
267-
for (const port of [
268-
...Object.values(connections.monitor),
269-
...Object.values(connections.panel),
270-
]) {
252+
for (const port of Object.values(connections.panel)) {
271253
try {
272254
port.postMessage(action);
273255
} catch (err) {
@@ -412,19 +394,6 @@ function toAllTabs(msg: TabMessage) {
412394
}
413395
}
414396

415-
function monitorInstances(shouldMonitor: boolean, id?: string) {
416-
if (!id && isMonitored === shouldMonitor) return;
417-
const action = {
418-
type: shouldMonitor ? ('START' as const) : ('STOP' as const),
419-
};
420-
if (id) {
421-
if (connections.tab[id]) connections.tab[id].postMessage(action);
422-
} else {
423-
toAllTabs(action);
424-
}
425-
isMonitored = shouldMonitor;
426-
}
427-
428397
function getReducerError() {
429398
const instancesState = store.getState().instances;
430399
const payload = instancesState.states[instancesState.current];
@@ -436,11 +405,11 @@ function getReducerError() {
436405
function togglePersist() {
437406
const state = store.getState();
438407
if (state.instances.persisted) {
439-
Object.keys(state.instances.connections).forEach((id) => {
408+
for (const id of Object.keys(state.instances.connections)) {
440409
if (connections.tab[id]) return;
441410
store.dispatch({ type: REMOVE_INSTANCE, id });
442411
toMonitors({ type: 'NA', id });
443-
});
412+
}
444413
}
445414
}
446415

@@ -543,9 +512,9 @@ function messaging<S, A extends Action<string>>(
543512
}
544513

545514
function disconnect(
546-
type: 'tab' | 'monitor' | 'panel',
515+
type: 'tab' | 'panel',
547516
id: number | string,
548-
listener?: (message: any, port: chrome.runtime.Port) => void,
517+
listener: (message: any, port: chrome.runtime.Port) => void,
549518
) {
550519
return function disconnectListener() {
551520
const p = connections[type][id];
@@ -559,7 +528,7 @@ function disconnect(
559528
}
560529
} else {
561530
monitors--;
562-
if (!monitors) monitorInstances(false);
531+
if (monitors === 0) toAllTabs({ type: 'STOP' });
563532
}
564533
};
565534
}
@@ -581,7 +550,7 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
581550
chrome.action.enable(id);
582551
chrome.action.setIcon({ tabId: id, path: 'img/logo/38x38.png' });
583552
}
584-
if (isMonitored) port.postMessage({ type: 'START' });
553+
if (monitors > 0) port.postMessage({ type: 'START' });
585554

586555
const state = store.getState();
587556
if (state.instances.persisted) {
@@ -607,22 +576,11 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
607576
port.onMessage.addListener(listener);
608577
port.onDisconnect.addListener(disconnect('tab', id, listener));
609578
} else if (port.name && port.name.indexOf('monitor') === 0) {
610-
id = getId(port.sender!, port.name);
611-
connections.monitor[id] = port;
612-
monitorInstances(true);
613-
listener = (msg: BackgroundAction | 'heartbeat') => {
614-
if (msg === 'heartbeat') return;
615-
store.dispatch(msg);
616-
};
617-
port.onMessage.addListener(listener);
618-
monitors++;
619-
port.onDisconnect.addListener(disconnect('monitor', id));
620-
} else {
621579
// devpanel
622-
id = port.name || port.sender!.frameId!;
580+
id = getId(port.sender!, port.name);
623581
connections.panel[id] = port;
624-
monitorInstances(true, port.name);
625582
monitors++;
583+
toAllTabs({ type: 'START' });
626584
listener = (msg: BackgroundAction | 'heartbeat') => {
627585
if (msg === 'heartbeat') return;
628586
store.dispatch(msg);

0 commit comments

Comments
 (0)