Remove use of window in background

This commit is contained in:
Nathan Bierema 2024-08-10 11:50:34 -04:00
parent 9c75659692
commit 21c9dcff47
3 changed files with 23 additions and 37 deletions

View File

@ -1,20 +1,11 @@
import '../chromeApiMock'; import configureStore from './store/backgroundStore';
import { Store } from 'redux';
import configureStore, { BackgroundAction } from './store/backgroundStore';
import openDevToolsWindow, { DevToolsPosition } from './openWindow'; import openDevToolsWindow, { DevToolsPosition } from './openWindow';
import { createMenu, removeMenu } from './contextMenus'; import { createMenu, removeMenu } from './contextMenus';
import syncOptions from '../options/syncOptions'; import syncOptions from '../options/syncOptions';
import { BackgroundState } from './store/backgroundReducer';
declare global {
interface Window {
store: Store<BackgroundState, BackgroundAction>;
}
}
// Expose the extension's store globally to access it from the windows // Expose the extension's store globally to access it from the windows
// via chrome.runtime.getBackgroundPage // via chrome.runtime.getBackgroundPage
window.store = configureStore(); export const store = configureStore();
// Listen for keyboard shortcuts // Listen for keyboard shortcuts
chrome.commands.onCommand.addListener((shortcut) => { chrome.commands.onCommand.addListener((shortcut) => {

View File

@ -1,4 +1,5 @@
import { LIFTED_ACTION } from '@redux-devtools/app'; import { LIFTED_ACTION } from '@redux-devtools/app';
import { store } from './index';
export function getReport( export function getReport(
reportId: string, reportId: string,
@ -24,7 +25,7 @@ export function getReport(
.then((json) => { .then((json) => {
const { payload, preloadedState } = json; const { payload, preloadedState } = json;
if (!payload) return; if (!payload) return;
window.store.dispatch({ store.dispatch({
type: LIFTED_ACTION, type: LIFTED_ACTION,
message: 'IMPORT', message: 'IMPORT',
state: JSON.stringify({ payload, preloadedState }), state: JSON.stringify({ payload, preloadedState }),

View File

@ -14,7 +14,6 @@ import {
import syncOptions, { import syncOptions, {
Options, Options,
OptionsMessage, OptionsMessage,
SyncOptions,
} from '../../options/syncOptions'; } from '../../options/syncOptions';
import openDevToolsWindow, { DevToolsPosition } from '../openWindow'; import openDevToolsWindow, { DevToolsPosition } from '../openWindow';
import { getReport } from '../logging'; import { getReport } from '../logging';
@ -32,6 +31,7 @@ import { LiftedState } from '@redux-devtools/instrument';
import type { BackgroundAction, LiftedActionAction } from './backgroundStore'; import type { BackgroundAction, LiftedActionAction } from './backgroundStore';
import type { Position } from '../../pageScript/api/openWindow'; import type { Position } from '../../pageScript/api/openWindow';
import type { BackgroundState } from './backgroundReducer'; import type { BackgroundState } from './backgroundReducer';
import { store } from '../index';
interface TabMessageBase { interface TabMessageBase {
readonly type: string; readonly type: string;
@ -346,7 +346,7 @@ function toContentScript(messageBody: ToContentScriptMessage) {
type: message, type: message,
action, action,
state: nonReduxDispatch( state: nonReduxDispatch(
window.store, store,
message, message,
instanceId, instanceId,
action as AppDispatchAction, action as AppDispatchAction,
@ -360,7 +360,7 @@ function toContentScript(messageBody: ToContentScriptMessage) {
type: message, type: message,
action, action,
state: nonReduxDispatch( state: nonReduxDispatch(
window.store, store,
message, message,
instanceId, instanceId,
action as unknown as AppDispatchAction, action as unknown as AppDispatchAction,
@ -374,7 +374,7 @@ function toContentScript(messageBody: ToContentScriptMessage) {
type: message, type: message,
action, action,
state: nonReduxDispatch( state: nonReduxDispatch(
window.store, store,
message, message,
instanceId, instanceId,
action as unknown as AppDispatchAction, action as unknown as AppDispatchAction,
@ -388,7 +388,7 @@ function toContentScript(messageBody: ToContentScriptMessage) {
type: message, type: message,
action, action,
state: nonReduxDispatch( state: nonReduxDispatch(
window.store, store,
message, message,
instanceId, instanceId,
action as unknown as AppDispatchAction, action as unknown as AppDispatchAction,
@ -402,7 +402,7 @@ function toContentScript(messageBody: ToContentScriptMessage) {
type: message, type: message,
action, action,
state: nonReduxDispatch( state: nonReduxDispatch(
window.store, store,
message, message,
instanceId, instanceId,
action as AppDispatchAction, action as AppDispatchAction,
@ -434,7 +434,7 @@ function monitorInstances(shouldMonitor: boolean, id?: string) {
} }
function getReducerError() { function getReducerError() {
const instancesState = window.store.getState().instances; const instancesState = store.getState().instances;
const payload = instancesState.states[instancesState.current]; const payload = instancesState.states[instancesState.current];
const computedState = payload.computedStates[payload.currentStateIndex]; const computedState = payload.computedStates[payload.currentStateIndex];
if (!computedState) return false; if (!computedState) return false;
@ -442,11 +442,11 @@ function getReducerError() {
} }
function togglePersist() { function togglePersist() {
const state = window.store.getState(); const state = store.getState();
if (state.instances.persisted) { if (state.instances.persisted) {
Object.keys(state.instances.connections).forEach((id) => { Object.keys(state.instances.connections).forEach((id) => {
if (connections.tab[id]) return; if (connections.tab[id]) return;
window.store.dispatch({ type: REMOVE_INSTANCE, id }); store.dispatch({ type: REMOVE_INSTANCE, id });
toMonitors({ type: 'NA', id }); toMonitors({ type: 'NA', id });
}); });
} }
@ -487,8 +487,8 @@ function messaging<S, A extends Action<string>>(
if (sender.frameId) tabId = `${tabId}-${sender.frameId}`; if (sender.frameId) tabId = `${tabId}-${sender.frameId}`;
if (request.type === 'STOP') { if (request.type === 'STOP') {
if (!Object.keys(window.store.getState().instances.connections).length) { if (!Object.keys(store.getState().instances.connections).length) {
window.store.dispatch({ type: DISCONNECTED }); store.dispatch({ type: DISCONNECTED });
} }
return; return;
} }
@ -497,7 +497,7 @@ function messaging<S, A extends Action<string>>(
return; return;
} }
if (request.type === 'GET_OPTIONS') { if (request.type === 'GET_OPTIONS') {
window.syncOptions.get((options) => { syncOptionsToAllTabs.get((options) => {
sendResponse!({ options }); sendResponse!({ options });
}); });
return; return;
@ -560,7 +560,7 @@ function messaging<S, A extends Action<string>>(
if (request.instanceId) { if (request.instanceId) {
action.request.instanceId = instanceId; action.request.instanceId = instanceId;
} }
window.store.dispatch(action); store.dispatch(action);
if (request.type === 'EXPORT') { if (request.type === 'EXPORT') {
toMonitors(action, tabId, true); toMonitors(action, tabId, true);
@ -580,8 +580,8 @@ function disconnect(
if (p) p.onDisconnect.removeListener(disconnectListener); if (p) p.onDisconnect.removeListener(disconnectListener);
delete connections[type][id]; delete connections[type][id];
if (type === 'tab') { if (type === 'tab') {
if (!window.store.getState().instances.persisted) { if (!store.getState().instances.persisted) {
window.store.dispatch({ type: REMOVE_INSTANCE, id }); store.dispatch({ type: REMOVE_INSTANCE, id });
toMonitors({ type: 'NA', id }); toMonitors({ type: 'NA', id });
} }
} else { } else {
@ -595,7 +595,7 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
let id: number | string; let id: number | string;
let listener; let listener;
window.store.dispatch({ type: CONNECTED, port }); store.dispatch({ type: CONNECTED, port });
if (port.name === 'tab') { if (port.name === 'tab') {
id = getId(port.sender!); id = getId(port.sender!);
@ -609,7 +609,7 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
} }
if (isMonitored) port.postMessage({ type: 'START' }); if (isMonitored) port.postMessage({ type: 'START' });
const state = window.store.getState(); const state = store.getState();
if (state.instances.persisted) { if (state.instances.persisted) {
const instanceId = `${id}/${msg.instanceId}`; const instanceId = `${id}/${msg.instanceId}`;
const persistedState = state.instances.states[instanceId]; const persistedState = state.instances.states[instanceId];
@ -645,7 +645,7 @@ function onConnect<S, A extends Action<string>>(port: chrome.runtime.Port) {
monitorInstances(true, port.name); monitorInstances(true, port.name);
monitors++; monitors++;
listener = (msg: BackgroundAction) => { listener = (msg: BackgroundAction) => {
window.store.dispatch(msg); store.dispatch(msg);
}; };
port.onMessage.addListener(listener); port.onMessage.addListener(listener);
port.onDisconnect.addListener(disconnect('panel', id, listener)); port.onDisconnect.addListener(disconnect('panel', id, listener));
@ -662,13 +662,7 @@ chrome.notifications.onClicked.addListener((id) => {
openDevToolsWindow('devtools-right'); openDevToolsWindow('devtools-right');
}); });
declare global { const syncOptionsToAllTabs = syncOptions(toAllTabs); // Expose to the options page
interface Window {
syncOptions: SyncOptions;
}
}
window.syncOptions = syncOptions(toAllTabs); // Expose to the options page
const api: Middleware<{}, BackgroundState, Dispatch<BackgroundAction>> = const api: Middleware<{}, BackgroundState, Dispatch<BackgroundAction>> =
(store) => (next) => (untypedAction) => { (store) => (next) => (untypedAction) => {