Skip to content

feat(extension): use persist from app #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions extension/src/app/middlewares/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ function getReducerError() {

function togglePersist() {
const state = window.store.getState();
if (state.persistStates) {
if (state.instances.persisted) {
Object.keys(state.instances.connections).forEach((id) => {
if (connections.tab[id]) return;
window.store.dispatch({ type: REMOVE_INSTANCE, id });
Expand Down Expand Up @@ -492,7 +492,7 @@ function disconnect(
if (p) p.onDisconnect.removeListener(disconnectListener);
delete connections[type][id];
if (type === 'tab') {
if (!window.store.getState().persistStates) {
if (!window.store.getState().instances.persisted) {
window.store.dispatch({ type: REMOVE_INSTANCE, id });
toMonitors({ type: 'NA', id });
}
Expand Down Expand Up @@ -522,7 +522,7 @@ function onConnect<S, A extends Action<unknown>>(port: chrome.runtime.Port) {
if (isMonitored) port.postMessage({ type: 'START' });

const state = window.store.getState();
if (state.persistStates) {
if (state.instances.persisted) {
const instanceId = `${id}/${msg.instanceId}`;
const persistedState = state.instances.states[instanceId];
if (!persistedState) return;
Expand Down Expand Up @@ -585,7 +585,6 @@ window.syncOptions = syncOptions(toAllTabs); // Expose to the options page
export default function api() {
return (next: Dispatch<BackgroundAction>) => (action: BackgroundAction) => {
if (action.type === LIFTED_ACTION) toContentScript(action);
else if (action.type === 'TOGGLE_PERSIST') togglePersist();
return next(action);
};
}
12 changes: 5 additions & 7 deletions extension/src/app/middlewares/panelSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import {
import { getActiveInstance } from '@redux-devtools/app/lib/reducers/instances';
import { Dispatch, MiddlewareAPI } from 'redux';
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) {
let autoselected = false;
const tabId = chrome.devtools.inspectedWindow.tabId;

return (
store: MiddlewareAPI<Dispatch<StoreActionWithTogglePersist>, StoreState>
) =>
(next: Dispatch<StoreActionWithTogglePersist>) =>
(action: StoreActionWithTogglePersist) => {
return (store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) =>
(next: Dispatch<StoreAction>) =>
(action: StoreAction) => {
const result = next(action);
if (!autoselected && action.type === UPDATE_STATE && tabId) {
autoselected = true;
Expand All @@ -25,7 +23,7 @@ function panelDispatcher(bgConnection: chrome.runtime.Port) {
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 instanceId = getActiveInstance(instances);
const id = instances.options[instanceId].connectionId;
Expand Down
9 changes: 3 additions & 6 deletions extension/src/app/middlewares/windowSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ import { getActiveInstance } from '@redux-devtools/app/lib/reducers/instances';
import { Dispatch, MiddlewareAPI, Store } from 'redux';
import { BackgroundState } from '../reducers/background';
import { StoreAction } from '@redux-devtools/app/lib/actions';
import {
WindowStoreAction,
StoreActionWithTogglePersist,
} from '../stores/windowStore';
import { WindowStoreAction } from '../stores/windowStore';
import { StoreState } from '@redux-devtools/app/lib/reducers';
import { BackgroundAction } from '../stores/backgroundStore';

const syncStores =
(baseStore: Store<BackgroundState, BackgroundAction>) =>
(store: MiddlewareAPI<Dispatch<StoreAction>, StoreState>) =>
(next: Dispatch<WindowStoreAction>) =>
(action: StoreActionWithTogglePersist) => {
(action: StoreAction) => {
if (action.type === UPDATE_STATE) {
return next({
...action,
instances: baseStore.getState().instances,
});
}
if (action.type === LIFTED_ACTION || action.type === 'TOGGLE_PERSIST') {
if (action.type === LIFTED_ACTION) {
const instances = store.getState().instances;
const instanceId = getActiveInstance(instances);
const id = instances.options[instanceId].connectionId;
Expand Down
3 changes: 0 additions & 3 deletions extension/src/app/reducers/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { combineReducers, Reducer } from 'redux';
import instances, {
InstancesState,
} from '@redux-devtools/app/lib/reducers/instances';
import persistStates from './persistStates';
import { BackgroundAction } from '../../stores/backgroundStore';

export interface BackgroundState {
readonly instances: InstancesState;
readonly persistStates: boolean;
}

const rootReducer: Reducer<BackgroundState, BackgroundAction> =
combineReducers<BackgroundState>({
instances,
persistStates,
});

export default rootReducer;
6 changes: 0 additions & 6 deletions extension/src/app/reducers/background/persistStates.ts

This file was deleted.

24 changes: 11 additions & 13 deletions extension/src/app/reducers/panel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import theme, { ThemeState } from '@redux-devtools/app/lib/reducers/theme';
import connection, {
ConnectionState,
} from '@redux-devtools/app/lib/reducers/connection';
import { StoreActionWithTogglePersist } from '../../stores/windowStore';
import { StoreAction } from '@redux-devtools/app/lib/actions';

export interface StoreStateWithoutSocket {
readonly section: SectionState;
Expand All @@ -30,17 +30,15 @@ export interface StoreStateWithoutSocket {
readonly notification: NotificationState;
}

const rootReducer: Reducer<
StoreStateWithoutSocket,
StoreActionWithTogglePersist
> = combineReducers<StoreStateWithoutSocket>({
instances,
monitor,
reports,
notification,
section,
theme,
connection,
});
const rootReducer: Reducer<StoreStateWithoutSocket, StoreAction> =
combineReducers<StoreStateWithoutSocket>({
instances,
monitor,
reports,
notification,
section,
theme,
connection,
});

export default rootReducer;
7 changes: 0 additions & 7 deletions extension/src/app/stores/backgroundStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ export type LiftedActionAction =
| LiftedActionActionAction
| LiftedActionExportAction;

interface TogglePersistAction {
readonly type: 'TOGGLE_PERSIST';
readonly instanceId: string | number;
readonly id: string | number | undefined;
}

interface ConnectedAction {
readonly type: typeof CONNECTED;
}
Expand All @@ -62,7 +56,6 @@ interface DisconnectedAction {
export type BackgroundAction =
| StoreActionWithoutLiftedAction
| LiftedActionAction
| TogglePersistAction
| ConnectedAction
| DisconnectedAction;

Expand Down
7 changes: 0 additions & 7 deletions extension/src/app/stores/windowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,12 @@ import { BackgroundState } from '../reducers/background';
import { BackgroundAction } from './backgroundStore';
import { EmptyUpdateStateAction, NAAction } from '../middlewares/api';

export interface TogglePersistAction {
readonly type: 'TOGGLE_PERSIST';
}

export type StoreActionWithTogglePersist = StoreAction | TogglePersistAction;

export interface ExpandedUpdateStateAction extends UpdateStateAction {
readonly instances: InstancesState;
}

export type WindowStoreAction =
| StoreActionWithoutUpdateState
| TogglePersistAction
| ExpandedUpdateStateAction
| NAAction
| EmptyUpdateStateAction;
Expand Down
6 changes: 2 additions & 4 deletions extension/src/browser/extension/devpanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import getPreloadedState from '../background/getPreloadedState';
import '../../views/devpanel.pug';
import { Action, PreloadedState, Store } from 'redux';
import { StoreState } from '@redux-devtools/app/lib/reducers';
import { StoreAction } from '@redux-devtools/app/lib/actions';
import { PanelMessage } from '../../../app/middlewares/api';
import { StoreActionWithTogglePersist } from '../../../app/stores/windowStore';
import { StoreStateWithoutSocket } from '../../../app/reducers/panel';

const position = location.hash;
Expand All @@ -21,9 +21,7 @@ const messageStyle: CSSProperties = {
};

let rendered: boolean | undefined;
let store:
| Store<StoreStateWithoutSocket, StoreActionWithTogglePersist>
| undefined;
let store: Store<StoreStateWithoutSocket, StoreAction> | undefined;
let bgConnection: chrome.runtime.Port;
let naTimeout: NodeJS.Timeout;
let preloadedState: PreloadedState<StoreState>;
Expand Down