feat(extension): use persist from app (#821)

* feat(extension): use persist from app

* Fix imports
This commit is contained in:
Nathan Bierema 2021-08-30 05:21:32 +00:00 committed by GitHub
parent a640e7345a
commit 91b89826ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 24 additions and 57 deletions

View File

@ -355,7 +355,7 @@ function getReducerError() {
function togglePersist() { function togglePersist() {
const state = window.store.getState(); const state = window.store.getState();
if (state.persistStates) { 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 }); window.store.dispatch({ type: REMOVE_INSTANCE, id });
@ -492,7 +492,7 @@ 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().persistStates) { if (!window.store.getState().instances.persisted) {
window.store.dispatch({ type: REMOVE_INSTANCE, id }); window.store.dispatch({ type: REMOVE_INSTANCE, id });
toMonitors({ type: 'NA', id }); toMonitors({ type: 'NA', id });
} }
@ -522,7 +522,7 @@ function onConnect<S, A extends Action<unknown>>(port: chrome.runtime.Port) {
if (isMonitored) port.postMessage({ type: 'START' }); if (isMonitored) port.postMessage({ type: 'START' });
const state = window.store.getState(); const state = window.store.getState();
if (state.persistStates) { 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];
if (!persistedState) return; if (!persistedState) return;
@ -585,7 +585,6 @@ window.syncOptions = syncOptions(toAllTabs); // Expose to the options page
export default function api() { export default function api() {
return (next: Dispatch<BackgroundAction>) => (action: BackgroundAction) => { return (next: Dispatch<BackgroundAction>) => (action: BackgroundAction) => {
if (action.type === LIFTED_ACTION) toContentScript(action); if (action.type === LIFTED_ACTION) toContentScript(action);
else if (action.type === 'TOGGLE_PERSIST') togglePersist();
return next(action); return next(action);
}; };
} }

View File

@ -6,17 +6,15 @@ import {
import { getActiveInstance } from '@redux-devtools/app/lib/reducers/instances'; import { getActiveInstance } from '@redux-devtools/app/lib/reducers/instances';
import { Dispatch, MiddlewareAPI } from 'redux'; import { Dispatch, MiddlewareAPI } from 'redux';
import { StoreState } from '@redux-devtools/app/lib/reducers'; import { StoreState } from '@redux-devtools/app/lib/reducers';
import { StoreActionWithTogglePersist } from '../stores/windowStore'; import { StoreAction } from '@redux-devtools/app/lib/actions';
function panelDispatcher(bgConnection: chrome.runtime.Port) { function panelDispatcher(bgConnection: chrome.runtime.Port) {
let autoselected = false; let autoselected = false;
const tabId = chrome.devtools.inspectedWindow.tabId; const tabId = chrome.devtools.inspectedWindow.tabId;
return ( return (store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) =>
store: MiddlewareAPI<Dispatch<StoreActionWithTogglePersist>, StoreState> (next: Dispatch<StoreAction>) =>
) => (action: StoreAction) => {
(next: Dispatch<StoreActionWithTogglePersist>) =>
(action: StoreActionWithTogglePersist) => {
const result = next(action); const result = next(action);
if (!autoselected && action.type === UPDATE_STATE && tabId) { if (!autoselected && action.type === UPDATE_STATE && tabId) {
autoselected = true; autoselected = true;
@ -25,7 +23,7 @@ function panelDispatcher(bgConnection: chrome.runtime.Port) {
next({ type: SELECT_INSTANCE, selected: connections[0] }); next({ type: SELECT_INSTANCE, selected: connections[0] });
} }
} }
if (action.type === LIFTED_ACTION || action.type === 'TOGGLE_PERSIST') { if (action.type === LIFTED_ACTION) {
const instances = store.getState().instances; const instances = store.getState().instances;
const instanceId = getActiveInstance(instances); const instanceId = getActiveInstance(instances);
const id = instances.options[instanceId].connectionId; const id = instances.options[instanceId].connectionId;

View File

@ -6,10 +6,7 @@ import { getActiveInstance } from '@redux-devtools/app/lib/reducers/instances';
import { Dispatch, MiddlewareAPI, Store } from 'redux'; import { Dispatch, MiddlewareAPI, Store } from 'redux';
import { BackgroundState } from '../reducers/background'; import { BackgroundState } from '../reducers/background';
import { StoreAction } from '@redux-devtools/app/lib/actions'; import { StoreAction } from '@redux-devtools/app/lib/actions';
import { import { WindowStoreAction } from '../stores/windowStore';
WindowStoreAction,
StoreActionWithTogglePersist,
} from '../stores/windowStore';
import { StoreState } from '@redux-devtools/app/lib/reducers'; import { StoreState } from '@redux-devtools/app/lib/reducers';
import { BackgroundAction } from '../stores/backgroundStore'; import { BackgroundAction } from '../stores/backgroundStore';
@ -17,14 +14,14 @@ const syncStores =
(baseStore: Store<BackgroundState, BackgroundAction>) => (baseStore: Store<BackgroundState, BackgroundAction>) =>
(store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) => (store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) =>
(next: Dispatch<WindowStoreAction>) => (next: Dispatch<WindowStoreAction>) =>
(action: StoreActionWithTogglePersist) => { (action: StoreAction) => {
if (action.type === UPDATE_STATE) { if (action.type === UPDATE_STATE) {
return next({ return next({
...action, ...action,
instances: baseStore.getState().instances, instances: baseStore.getState().instances,
}); });
} }
if (action.type === LIFTED_ACTION || action.type === 'TOGGLE_PERSIST') { if (action.type === LIFTED_ACTION) {
const instances = store.getState().instances; const instances = store.getState().instances;
const instanceId = getActiveInstance(instances); const instanceId = getActiveInstance(instances);
const id = instances.options[instanceId].connectionId; const id = instances.options[instanceId].connectionId;

View File

@ -2,18 +2,15 @@ import { combineReducers, Reducer } from 'redux';
import instances, { import instances, {
InstancesState, InstancesState,
} from '@redux-devtools/app/lib/reducers/instances'; } from '@redux-devtools/app/lib/reducers/instances';
import persistStates from './persistStates';
import { BackgroundAction } from '../../stores/backgroundStore'; import { BackgroundAction } from '../../stores/backgroundStore';
export interface BackgroundState { export interface BackgroundState {
readonly instances: InstancesState; readonly instances: InstancesState;
readonly persistStates: boolean;
} }
const rootReducer: Reducer<BackgroundState, BackgroundAction> = const rootReducer: Reducer<BackgroundState, BackgroundAction> =
combineReducers<BackgroundState>({ combineReducers<BackgroundState>({
instances, instances,
persistStates,
}); });
export default rootReducer; export default rootReducer;

View File

@ -1,6 +0,0 @@
import { BackgroundAction } from '../../stores/backgroundStore';
export default function persistStates(state = false, action: BackgroundAction) {
if (action.type === 'TOGGLE_PERSIST') return !state;
return state;
}

View File

@ -18,7 +18,7 @@ import theme, { ThemeState } from '@redux-devtools/app/lib/reducers/theme';
import connection, { import connection, {
ConnectionState, ConnectionState,
} from '@redux-devtools/app/lib/reducers/connection'; } from '@redux-devtools/app/lib/reducers/connection';
import { StoreActionWithTogglePersist } from '../../stores/windowStore'; import { StoreAction } from '@redux-devtools/app/lib/actions';
export interface StoreStateWithoutSocket { export interface StoreStateWithoutSocket {
readonly section: SectionState; readonly section: SectionState;
@ -30,17 +30,15 @@ export interface StoreStateWithoutSocket {
readonly notification: NotificationState; readonly notification: NotificationState;
} }
const rootReducer: Reducer< const rootReducer: Reducer<StoreStateWithoutSocket, StoreAction> =
StoreStateWithoutSocket, combineReducers<StoreStateWithoutSocket>({
StoreActionWithTogglePersist instances,
> = combineReducers<StoreStateWithoutSocket>({ monitor,
instances, reports,
monitor, notification,
reports, section,
notification, theme,
section, connection,
theme, });
connection,
});
export default rootReducer; export default rootReducer;

View File

@ -45,12 +45,6 @@ export type LiftedActionAction =
| LiftedActionActionAction | LiftedActionActionAction
| LiftedActionExportAction; | LiftedActionExportAction;
interface TogglePersistAction {
readonly type: 'TOGGLE_PERSIST';
readonly instanceId: string | number;
readonly id: string | number | undefined;
}
interface ConnectedAction { interface ConnectedAction {
readonly type: typeof CONNECTED; readonly type: typeof CONNECTED;
} }
@ -62,7 +56,6 @@ interface DisconnectedAction {
export type BackgroundAction = export type BackgroundAction =
| StoreActionWithoutLiftedAction | StoreActionWithoutLiftedAction
| LiftedActionAction | LiftedActionAction
| TogglePersistAction
| ConnectedAction | ConnectedAction
| DisconnectedAction; | DisconnectedAction;

View File

@ -23,19 +23,12 @@ import { BackgroundState } from '../reducers/background';
import { BackgroundAction } from './backgroundStore'; import { BackgroundAction } from './backgroundStore';
import { EmptyUpdateStateAction, NAAction } from '../middlewares/api'; import { EmptyUpdateStateAction, NAAction } from '../middlewares/api';
export interface TogglePersistAction {
readonly type: 'TOGGLE_PERSIST';
}
export type StoreActionWithTogglePersist = StoreAction | TogglePersistAction;
export interface ExpandedUpdateStateAction extends UpdateStateAction { export interface ExpandedUpdateStateAction extends UpdateStateAction {
readonly instances: InstancesState; readonly instances: InstancesState;
} }
export type WindowStoreAction = export type WindowStoreAction =
| StoreActionWithoutUpdateState | StoreActionWithoutUpdateState
| TogglePersistAction
| ExpandedUpdateStateAction | ExpandedUpdateStateAction
| NAAction | NAAction
| EmptyUpdateStateAction; | EmptyUpdateStateAction;

View File

@ -9,8 +9,8 @@ import getPreloadedState from '../background/getPreloadedState';
import '../../views/devpanel.pug'; import '../../views/devpanel.pug';
import { Action, PreloadedState, Store } from 'redux'; import { Action, PreloadedState, Store } from 'redux';
import { StoreState } from '@redux-devtools/app/lib/reducers'; import { StoreState } from '@redux-devtools/app/lib/reducers';
import { StoreAction } from '@redux-devtools/app/lib/actions';
import { PanelMessage } from '../../../app/middlewares/api'; import { PanelMessage } from '../../../app/middlewares/api';
import { StoreActionWithTogglePersist } from '../../../app/stores/windowStore';
import { StoreStateWithoutSocket } from '../../../app/reducers/panel'; import { StoreStateWithoutSocket } from '../../../app/reducers/panel';
const position = location.hash; const position = location.hash;
@ -21,9 +21,7 @@ const messageStyle: CSSProperties = {
}; };
let rendered: boolean | undefined; let rendered: boolean | undefined;
let store: let store: Store<StoreStateWithoutSocket, StoreAction> | undefined;
| Store<StoreStateWithoutSocket, StoreActionWithTogglePersist>
| undefined;
let bgConnection: chrome.runtime.Port; let bgConnection: chrome.runtime.Port;
let naTimeout: NodeJS.Timeout; let naTimeout: NodeJS.Timeout;
let preloadedState: PreloadedState<StoreState>; let preloadedState: PreloadedState<StoreState>;