Eliminate relay

This commit is contained in:
Nathan Bierema 2021-07-17 08:47:05 -04:00
parent 82c8f142e6
commit 86c270fa91
3 changed files with 138 additions and 71 deletions

View File

@ -78,17 +78,11 @@ function filterStates(computedStates, stateSanitizer) {
export function filterState<S, A extends Action<unknown>>( export function filterState<S, A extends Action<unknown>>(
state: LiftedState<S, A, unknown>, state: LiftedState<S, A, unknown>,
type,
localFilter: LocalFilter | undefined, localFilter: LocalFilter | undefined,
stateSanitizer: ((state: S, index: number) => S) | undefined, stateSanitizer: ((state: S, index: number) => S) | undefined,
actionSanitizer: ((action: A, id: number) => A) | undefined, actionSanitizer: ((action: A, id: number) => A) | undefined,
nextActionId: number | undefined,
predicate: ((state: S, action: A) => boolean) | undefined predicate: ((state: S, action: A) => boolean) | undefined
) { ) {
if (type === 'ACTION') {
return !stateSanitizer ? state : stateSanitizer(state, nextActionId - 1);
} else if (type !== 'STATE') return state;
if (predicate || !noFiltersApplied(localFilter)) { if (predicate || !noFiltersApplied(localFilter)) {
const filteredStagedActionIds: number[] = []; const filteredStagedActionIds: number[] = [];
const filteredComputedStates: { state: S; error?: string | undefined }[] = const filteredComputedStates: { state: S; error?: string | undefined }[] =

View File

@ -8,6 +8,7 @@ import generateId from './generateInstanceId';
import { PageScriptToContentScriptMessage } from '../../browser/extension/inject/contentScript'; import { PageScriptToContentScriptMessage } from '../../browser/extension/inject/contentScript';
import { Config } from '../../browser/extension/inject/pageScript'; import { Config } from '../../browser/extension/inject/pageScript';
import { Action } from 'redux'; import { Action } from 'redux';
import { LiftedState, PerformAction } from '@redux-devtools/instrument';
const listeners = {}; const listeners = {};
export const source = '@devtools-page'; export const source = '@devtools-page';
@ -186,10 +187,62 @@ interface ExportMessage<S, A extends Action<unknown>> {
readonly instanceId: number; readonly instanceId: number;
} }
interface ActionMessage<S, A extends Action<unknown>> {
readonly type: 'ACTION';
readonly payload: S;
readonly source: typeof source;
readonly instanceId: number;
readonly action: PerformAction<A> | A;
readonly maxAge: number;
readonly nextActionId: number;
}
interface StateMessage<S, A extends Action<unknown>> {
readonly type: 'STATE';
readonly payload: LiftedState<S, A, unknown>;
readonly source: typeof source;
readonly instanceId: number;
readonly libConfig?: unknown;
}
interface ErrorMessage {
readonly type: 'ERROR';
readonly payload: unknown;
readonly source: typeof source;
readonly instanceId: number;
}
interface InitInstanceMessage {
readonly type: 'INIT_INSTANCE';
readonly payload: undefined;
readonly source: typeof source;
readonly instanceId: number;
}
interface GetReportMessage {
readonly type: 'GET_REPORT';
readonly payload: string;
readonly source: typeof source;
readonly instanceId: number;
}
interface StopMessage {
readonly type: 'STOP';
readonly payload: undefined;
readonly source: typeof source;
readonly instanceId: number;
}
type ToContentScriptMessage<S, A extends Action<unknown>> = type ToContentScriptMessage<S, A extends Action<unknown>> =
| LiftedMessage | LiftedMessage
| PartialStateMessage<S, A> | PartialStateMessage<S, A>
| ExportMessage<S, A>; | ExportMessage<S, A>
| ActionMessage<S, A>
| StateMessage<S, A>
| ErrorMessage
| InitInstanceMessage
| GetReportMessage
| StopMessage;
export function toContentScript<S, A extends Action<unknown>>( export function toContentScript<S, A extends Action<unknown>>(
message: ToContentScriptMessage<S, A>, message: ToContentScriptMessage<S, A>,

View File

@ -187,7 +187,23 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
relayAction.cancel(); relayAction.cancel();
const state = liftedState || store.liftedStore.getState(); const state = liftedState || store.liftedStore.getState();
sendingActionId = state.nextActionId; sendingActionId = state.nextActionId;
relay('STATE', state, undefined, undefined, libConfig); toContentScript(
{
type: 'STATE',
payload: filterState(
state,
localFilter,
stateSanitizer,
actionSanitizer,
predicate
),
source,
instanceId,
libConfig,
},
serializeState,
serializeAction
);
}, },
latency latency
); );
@ -226,58 +242,6 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
); );
} }
function relay(
type: 'ACTION',
state: S,
action: PerformAction<A>,
nextActionId: number
): void;
function relay(
type: 'STATE',
state: LiftedState<S, A, unknown>,
action?: undefined,
nextActionId?: undefined,
libConfig?: unknown
): void;
function relay(type: 'ERROR', message: unknown): void;
function relay(type: 'INIT_INSTANCE'): void;
function relay(type: 'GET_REPORT', reportId: string): void;
function relay(type: 'STOP'): void;
function relay(
type: string,
state?: S | LiftedState<S, A, unknown> | unknown,
action?: PerformAction<A> | undefined,
nextActionId?: number | undefined,
libConfig?: unknown
) {
const message = {
type,
payload: filterState(
state,
type,
localFilter,
stateSanitizer,
actionSanitizer,
nextActionId,
predicate
),
source,
instanceId,
};
if (type === 'ACTION') {
message.action = !actionSanitizer
? action
: actionSanitizer(action.action, nextActionId - 1);
message.maxAge = getMaxAge();
message.nextActionId = nextActionId;
} else if (libConfig) {
message.libConfig = libConfig;
}
toContentScript(message, serializeState, serializeAction);
}
const relayAction = throttle(() => { const relayAction = throttle(() => {
const liftedState = store.liftedStore.getState(); const liftedState = store.liftedStore.getState();
const nextActionId = liftedState.nextActionId; const nextActionId = liftedState.nextActionId;
@ -298,11 +262,25 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
} }
const state = const state =
liftedState.computedStates[liftedState.computedStates.length - 1].state; liftedState.computedStates[liftedState.computedStates.length - 1].state;
relay( toContentScript(
'ACTION', {
state, type: 'ACTION',
liftedState.actionsById[nextActionId - 1], payload: !stateSanitizer
nextActionId ? state
: stateSanitizer(state, nextActionId - 1),
source,
instanceId,
action: !actionSanitizer
? liftedState.actionsById[nextActionId - 1]
: actionSanitizer(
liftedState.actionsById[nextActionId - 1].action,
nextActionId - 1
),
maxAge: getMaxAge(),
nextActionId,
},
serializeState,
serializeAction
); );
return; return;
} }
@ -319,7 +297,22 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
sendingActionId = nextActionId; sendingActionId = nextActionId;
if (typeof payload === 'undefined') return; if (typeof payload === 'undefined') return;
if ('skippedActionIds' in payload) { if ('skippedActionIds' in payload) {
relay('STATE', payload); toContentScript(
{
type: 'STATE',
payload: filterState(
payload,
localFilter,
stateSanitizer,
actionSanitizer,
predicate
),
source,
instanceId,
},
serializeState,
serializeAction
);
return; return;
} }
toContentScript( toContentScript(
@ -341,7 +334,12 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
const result = evalAction(action, actionCreators); const result = evalAction(action, actionCreators);
(store.initialDispatch || store.dispatch)(result); (store.initialDispatch || store.dispatch)(result);
} catch (e) { } catch (e) {
relay('ERROR', e.message); toContentScript({
type: 'ERROR',
payload: e.message,
source,
instanceId,
});
} }
} }
@ -352,7 +350,12 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
if (!nextLiftedState) return; if (!nextLiftedState) return;
store.liftedStore.dispatch({ type: 'IMPORT_STATE', ...nextLiftedState }); store.liftedStore.dispatch({ type: 'IMPORT_STATE', ...nextLiftedState });
} catch (e) { } catch (e) {
relay('ERROR', e.message); toContentScript({
type: 'ERROR',
payload: e.message,
source,
instanceId,
});
} }
} }
@ -413,7 +416,12 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
}); });
if (reportId) { if (reportId) {
relay('GET_REPORT', reportId); toContentScript({
type: 'GET_REPORT',
payload: reportId,
source,
instanceId,
});
reportId = null; reportId = null;
} }
return; return;
@ -421,7 +429,14 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
monitor.stop(); monitor.stop();
relayAction.cancel(); relayAction.cancel();
relayState.cancel(); relayState.cancel();
if (!message.failed) relay('STOP'); if (!message.failed) {
toContentScript({
type: 'STOP',
payload: undefined,
source,
instanceId,
});
}
} }
} }
@ -471,7 +486,12 @@ function __REDUX_DEVTOOLS_EXTENSION__<S, A extends Action<unknown>>(
return true; return true;
}); });
relay('INIT_INSTANCE'); toContentScript({
type: 'INIT_INSTANCE',
payload: undefined,
source,
instanceId,
});
store.subscribe(handleChange); store.subscribe(handleChange);
if (typeof reportId === 'undefined') { if (typeof reportId === 'undefined') {