More work on types

This commit is contained in:
Nathan Bierema 2021-07-18 15:44:42 -04:00
parent 3cc1a62177
commit b1a71b4a72
3 changed files with 23 additions and 21 deletions

View File

@ -325,7 +325,7 @@ interface StateMessage<S, A extends Action<unknown>> {
export interface ErrorMessage { export interface ErrorMessage {
readonly type: 'ERROR'; readonly type: 'ERROR';
readonly payload: unknown; readonly payload: string;
readonly source: typeof source; readonly source: typeof source;
readonly instanceId: number; readonly instanceId: number;
} }
@ -607,7 +607,7 @@ export function connect(preConfig: Config) {
post(message); post(message);
}; };
const error = (payload: unknown) => { const error = (payload: string) => {
post({ type: 'ERROR', payload, instanceId: id, source }); post({ type: 'ERROR', payload, instanceId: id, source });
}; };

View File

@ -157,10 +157,14 @@ type UpdateStateRequest<S, A extends Action<unknown>> =
| SerializedActionMessage | SerializedActionMessage
| SerializedStateMessage<S, A>; | SerializedStateMessage<S, A>;
interface EmptyUpdateStateAction {
readonly type: typeof UPDATE_STATE;
}
interface UpdateStateAction<S, A extends Action<unknown>> { interface UpdateStateAction<S, A extends Action<unknown>> {
readonly type: typeof UPDATE_STATE; readonly type: typeof UPDATE_STATE;
readonly request?: UpdateStateRequest<S, A>; readonly request: UpdateStateRequest<S, A>;
readonly id?: string | number; readonly id: string | number;
} }
export type TabMessage = export type TabMessage =
@ -171,14 +175,11 @@ export type TabMessage =
| ImportAction | ImportAction
| ActionAction | ActionAction
| ExportAction; | ExportAction;
type PanelMessage<S, A extends Action<unknown>> = export type PanelMessage<S, A extends Action<unknown>> =
| NAAction
| ErrorMessage
| UpdateStateAction<S, A>;
type MonitorMessage<S, A extends Action<unknown>> =
| NAAction | NAAction
| ErrorMessage | ErrorMessage
| UpdateStateAction<S, A>; | UpdateStateAction<S, A>;
export type MonitorMessage = NAAction | ErrorMessage | EmptyUpdateStateAction;
type TabPort = Omit<chrome.runtime.Port, 'postMessage'> & { type TabPort = Omit<chrome.runtime.Port, 'postMessage'> & {
postMessage: (message: TabMessage) => void; postMessage: (message: TabMessage) => void;
@ -189,9 +190,7 @@ type PanelPort = Omit<chrome.runtime.Port, 'postMessage'> & {
) => void; ) => void;
}; };
type MonitorPort = Omit<chrome.runtime.Port, 'postMessage'> & { type MonitorPort = Omit<chrome.runtime.Port, 'postMessage'> & {
postMessage: <S, A extends Action<unknown>>( postMessage: (message: MonitorMessage) => void;
message: MonitorMessage<S, A>
) => void;
}; };
const CONNECTED = 'socket/CONNECTED'; const CONNECTED = 'socket/CONNECTED';

View File

@ -7,9 +7,10 @@ import configureStore from '../../../app/stores/panelStore';
import getPreloadedState from '../background/getPreloadedState'; import getPreloadedState from '../background/getPreloadedState';
import '../../views/devpanel.pug'; import '../../views/devpanel.pug';
import { 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 { StoreAction } from '@redux-devtools/app/lib/actions';
import { PanelMessage } from '../../../app/middlewares/api';
const position = location.hash; const position = location.hash;
const messageStyle: CSSProperties = { const messageStyle: CSSProperties = {
@ -96,7 +97,8 @@ function init(id: number) {
bgConnection = chrome.runtime.connect({ bgConnection = chrome.runtime.connect({
name: id ? id.toString() : undefined, name: id ? id.toString() : undefined,
}); });
bgConnection.onMessage.addListener((message) => { bgConnection.onMessage.addListener(
<S, A extends Action<unknown>>(message: PanelMessage<S, A>) => {
if (message.type === 'NA') { if (message.type === 'NA') {
if (message.id === id) renderNA(); if (message.id === id) renderNA();
else store!.dispatch({ type: REMOVE_INSTANCE, id: message.id }); else store!.dispatch({ type: REMOVE_INSTANCE, id: message.id });
@ -104,7 +106,8 @@ function init(id: number) {
if (!rendered) renderDevTools(); if (!rendered) renderDevTools();
store!.dispatch(message); store!.dispatch(message);
} }
}); }
);
} }
init(chrome.devtools.inspectedWindow.tabId); init(chrome.devtools.inspectedWindow.tabId);