More work on types

This commit is contained in:
Nathan Bierema 2021-07-19 23:15:22 -04:00
parent 9af6e54649
commit fcfe18c1db
6 changed files with 59 additions and 30 deletions

View File

@ -62,7 +62,9 @@ export default function openDevToolsWindow(position: DevToolsPosition) {
switch (position) { switch (position) {
case 'devtools-right': case 'devtools-right':
params.left = params.left =
window.screen.availLeft + window.screen.availWidth - params.width!; (window.screen as unknown as { availLeft: number }).availLeft +
window.screen.availWidth -
params.width!;
break; break;
case 'devtools-bottom': case 'devtools-bottom':
params.height = 420; params.height = 420;

View File

@ -214,7 +214,7 @@ function tryCatch<S, A extends Action<unknown>>(
} catch (err) { } catch (err) {
if (err.message === 'Message length exceeded maximum allowed length.') { if (err.message === 'Message length exceeded maximum allowed length.') {
const instanceId = args.instanceId; const instanceId = args.instanceId;
const newArgs: SplitMessageStart = { const newArgs = {
split: 'start', split: 'start',
}; };
const toSplit: [string, string][] = []; const toSplit: [string, string][] = [];

View File

@ -1,7 +1,12 @@
import { getActionsArray, evalAction } from '@redux-devtools/utils'; import {
getActionsArray,
evalAction,
ActionCreatorObject,
} from '@redux-devtools/utils';
import throttle from 'lodash/throttle'; import throttle from 'lodash/throttle';
import { import {
Action, Action,
ActionCreator,
PreloadedState, PreloadedState,
Reducer, Reducer,
Store, Store,
@ -115,6 +120,9 @@ export interface ConfigWithExpandedMaxAge {
readonly features?: Features; readonly features?: Features;
readonly type?: string; readonly type?: string;
readonly getActionType?: <A extends Action<unknown>>(action: A) => A; readonly getActionType?: <A extends Action<unknown>>(action: A) => A;
readonly actionCreators: {
readonly [key: string]: ActionCreator<Action<unknown>>;
};
} }
export interface Config extends ConfigWithExpandedMaxAge { export interface Config extends ConfigWithExpandedMaxAge {
@ -161,7 +169,7 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
let store: EnhancedStore<S, A, unknown>; let store: EnhancedStore<S, A, unknown>;
let errorOccurred = false; let errorOccurred = false;
let maxAge: number | undefined; let maxAge: number | undefined;
let actionCreators; let actionCreators: readonly ActionCreatorObject[];
let sendingActionId = 1; let sendingActionId = 1;
const instanceId = generateId(config.instanceId); const instanceId = generateId(config.instanceId);
const localFilter = getLocalFilter(config); const localFilter = getLocalFilter(config);
@ -375,26 +383,25 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
} }
function dispatchMonitorAction(action: DispatchAction) { function dispatchMonitorAction(action: DispatchAction) {
const type = action.type; const features = config!.features;
const features = config.features;
if (features) { if (features) {
if ( if (
!features.jump && !features.jump &&
(type === 'JUMP_TO_STATE' || type === 'JUMP_TO_ACTION') (action.type === 'JUMP_TO_STATE' || action.type === 'JUMP_TO_ACTION')
) { ) {
return; return;
} }
if (!features.skip && type === 'TOGGLE_ACTION') return; if (!features.skip && action.type === 'TOGGLE_ACTION') return;
if (!features.reorder && type === 'REORDER_ACTION') return; if (!features.reorder && action.type === 'REORDER_ACTION') return;
if (!features.import && type === 'IMPORT_STATE') return; if (!features.import && action.type === 'IMPORT_STATE') return;
if (!features.lock && type === 'LOCK_CHANGES') return; if (!features.lock && action.type === 'LOCK_CHANGES') return;
if (!features.pause && type === 'PAUSE_RECORDING') return; if (!features.pause && action.type === 'PAUSE_RECORDING') return;
} }
if (type === 'JUMP_TO_STATE') { if (action.type === 'JUMP_TO_STATE') {
const liftedState = store.liftedStore.getState(); const liftedState = store.liftedStore.getState();
const index = liftedState.stagedActionIds.indexOf(action.actionId); const index = liftedState.stagedActionIds.indexOf(action.actionId);
if (index === -1) return; if (index === -1) return;
store.liftedStore.dispatch({ type, index }); store.liftedStore.dispatch({ type: action.type, index });
return; return;
} }
store.liftedStore.dispatch(action); store.liftedStore.dispatch(action);
@ -419,8 +426,8 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
return; return;
case 'START': case 'START':
monitor.start(true); monitor.start(true);
if (!actionCreators && config.actionCreators) { if (!actionCreators && config!.actionCreators) {
actionCreators = getActionsArray(config.actionCreators); actionCreators = getActionsArray(config!.actionCreators);
} }
relayState(undefined, { relayState(undefined, {
name: config!.name || document.title, name: config!.name || document.title,

View File

@ -9,7 +9,9 @@ if (process.env.NODE_ENV === 'production') {
} else { } else {
s.src = chrome.extension.getURL('page.bundle.js'); s.src = chrome.extension.getURL('page.bundle.js');
s.onload = function () { s.onload = function () {
this.parentNode.removeChild(this); (this as HTMLScriptElement).parentNode!.removeChild(
this as HTMLScriptElement
);
}; };
(document.head || document.documentElement).appendChild(s); (document.head || document.documentElement).appendChild(s);
} }

View File

@ -120,13 +120,28 @@ export interface LockChangesAction {
} }
export interface ToggleActionAction { export interface ToggleActionAction {
type: 'TOGGLE_ACTION'; type: 'TOGGLE_ACTION';
id: number;
} }
export interface RollbackAction { export interface RollbackAction {
type: 'ROLLBACK'; type: 'ROLLBACK';
timestamp: number;
} }
export interface SweepAction { export interface SweepAction {
type: 'SWEEP'; type: 'SWEEP';
} }
interface ReorderActionAction {
type: 'REORDER_ACTION';
actionId: number;
beforeActionId: number;
}
interface ImportStateAction {
type: 'IMPORT_STATE';
nextLiftedState:
| LiftedState<unknown, Action<unknown>, unknown>
| readonly Action<unknown>[];
preloadedState?: unknown;
noRecompute?: boolean | undefined;
}
export type DispatchAction = export type DispatchAction =
| JumpToStateAction | JumpToStateAction
| JumpToActionAction | JumpToActionAction
@ -134,7 +149,9 @@ export type DispatchAction =
| LockChangesAction | LockChangesAction
| ToggleActionAction | ToggleActionAction
| RollbackAction | RollbackAction
| SweepAction; | SweepAction
| ReorderActionAction
| ImportStateAction;
interface LiftedActionActionBase { interface LiftedActionActionBase {
action?: DispatchAction | string | CustomAction; action?: DispatchAction | string | CustomAction;
state?: string; state?: string;
@ -151,6 +168,7 @@ interface LiftedActionImportAction extends LiftedActionActionBase {
message: 'IMPORT'; message: 'IMPORT';
state: string; state: string;
preloadedState: unknown | undefined; preloadedState: unknown | undefined;
id?: string | number;
} }
interface LiftedActionActionAction extends LiftedActionActionBase { interface LiftedActionActionAction extends LiftedActionActionBase {
type: typeof LIFTED_ACTION; type: typeof LIFTED_ACTION;

View File

@ -3,22 +3,24 @@ import jsan from 'jsan';
import { nanoid } from 'nanoid/non-secure'; import { nanoid } from 'nanoid/non-secure';
import { immutableSerialize } from '@redux-devtools/serialize'; import { immutableSerialize } from '@redux-devtools/serialize';
import Immutable from 'immutable'; import Immutable from 'immutable';
import { Action } from 'redux'; import { Action, ActionCreator } from 'redux';
export function generateId(id: string | undefined) { export function generateId(id: string | undefined) {
return id || nanoid(7); return id || nanoid(7);
} }
export interface ActionCreatorObject {
readonly name: string;
readonly func: ActionCreator<Action<unknown>>;
readonly args: readonly string[];
}
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
function flatTree( function flatTree(
obj: { [key: string]: (...args: any[]) => unknown }, obj: { [key: string]: ActionCreator<Action<unknown>> },
namespace = '' namespace = ''
) { ) {
let functions: { let functions: ActionCreatorObject[] = [];
name: string;
func: (...args: any[]) => unknown;
args: string[];
}[] = [];
Object.keys(obj).forEach((key) => { Object.keys(obj).forEach((key) => {
const prop = obj[key]; const prop = obj[key];
if (typeof prop === 'function') { if (typeof prop === 'function') {
@ -63,7 +65,7 @@ export function getMethods(obj: unknown) {
} }
export function getActionsArray(actionCreators: { export function getActionsArray(actionCreators: {
[key: string]: (...args: any[]) => unknown; [key: string]: ActionCreator<Action<unknown>>;
}) { }) {
if (Array.isArray(actionCreators)) return actionCreators; if (Array.isArray(actionCreators)) return actionCreators;
return flatTree(actionCreators); return flatTree(actionCreators);
@ -81,10 +83,8 @@ function evalArgs(inArgs: string[], restArgs: string) {
} }
export function evalAction( export function evalAction(
action: string | { args: string[]; rest: string; selected: string }, action: string | { args: string[]; rest: string; selected: number },
actionCreators: { actionCreators: readonly ActionCreatorObject[]
[selected: string]: { func: (...args: any[]) => Action<unknown> };
}
) { ) {
if (typeof action === 'string') { if (typeof action === 'string') {
// eslint-disable-next-line @typescript-eslint/no-implied-eval // eslint-disable-next-line @typescript-eslint/no-implied-eval