This commit is contained in:
meluskyc 2023-06-23 20:34:42 -04:00 committed by GitHub
commit 3592eb3587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View File

@ -222,10 +222,11 @@ function post<S, A extends Action<unknown>>(
function getStackTrace(
config: Config,
toExcludeFromTrace: Function | undefined
toExcludeFromTrace: Function | undefined,
action: Action<unknown>
) {
if (!config.trace) return undefined;
if (typeof config.trace === 'function') return config.trace();
if (typeof config.trace === 'function') return config.trace(action);
let stack;
let extraFrames = 0;
@ -268,16 +269,33 @@ function amendActionType<A extends Action<unknown>>(
toExcludeFromTrace: Function | undefined
): StructuralPerformAction<A> {
let timestamp = Date.now();
let stack = getStackTrace(config, toExcludeFromTrace);
if (typeof action === 'string') {
return { action: { type: action } as A, timestamp, stack };
const amendedAction = { type: action } as A;
return {
action: amendedAction,
timestamp,
stack: getStackTrace(config, toExcludeFromTrace, amendedAction),
};
}
if (!(action as A).type)
return { action: { type: 'update' } as A, timestamp, stack };
if ((action as StructuralPerformAction<A>).action)
if (!(action as A).type) {
const amendedAction = { type: 'update' } as A;
return {
action: amendedAction,
timestamp,
stack: getStackTrace(config, toExcludeFromTrace, amendedAction),
};
}
if ((action as StructuralPerformAction<A>).action) {
const stack = getStackTrace(
config,
toExcludeFromTrace,
(action as StructuralPerformAction<A>).action
);
return (
stack ? { stack, ...action } : action
) as StructuralPerformAction<A>;
}
const stack = getStackTrace(config, toExcludeFromTrace, action as A);
return { action, timestamp, stack } as StructuralPerformAction<A>;
}

View File

@ -112,7 +112,9 @@ export interface ConfigWithExpandedMaxAge {
currentLiftedAction: LiftedAction<S, A, unknown>,
previousLiftedState: LiftedState<S, A, unknown> | undefined
) => number);
readonly trace?: boolean | (() => string | undefined);
readonly trace?:
| boolean
| (<A extends Action<unknown>>(action: A) => string | undefined);
readonly traceLimit?: number;
readonly shouldCatchErrors?: boolean;
readonly shouldHotReload?: boolean;

View File

@ -216,14 +216,15 @@ export function getSeralizeParameter(
return value;
}
export function getStackTrace(
export function getStackTrace<A extends Action<unknown>>(
// eslint-disable-next-line @typescript-eslint/ban-types
config: { trace?: () => {}; traceLimit: number },
config: { trace?: (action?: A) => {}; traceLimit: number },
// eslint-disable-next-line @typescript-eslint/ban-types
toExcludeFromTrace?: Function | undefined
toExcludeFromTrace?: Function | undefined,
action?: A
) {
if (!config.trace) return undefined;
if (typeof config.trace === 'function') return config.trace();
if (typeof config.trace === 'function') return config.trace(action);
let stack;
let extraFrames = 0;