1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Refactor for reduced nesting

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>

Co-authored-by: Gabriel <gaccettola@mirantis.com>
This commit is contained in:
Iku-turso 2023-03-23 14:36:22 +02:00
parent 3931c90d30
commit f3ecdddcb5

View File

@ -27,54 +27,54 @@ const telemetryDecoratorInjectable = getInjectable({
(di: DiContainerForInjection, instantiationParameter: any) => {
const instance = instantiateToBeDecorated(di, instantiationParameter);
if (isFunction(instance)) {
return (...args: any[]) => {
const currentContext = di.context.at(-1);
assert(currentContext);
const emitTelemetry = diForDecorator.inject(
emitTelemetryInjectable,
);
const logError = diForDecorator.inject(logErrorInjectable);
const whiteList = diForDecorator.inject(
telemetryWhiteListForFunctionsInjectable,
);
const whiteListMap = getWhiteListMap(whiteList);
const whiteListed = whiteListMap.get(currentContext.injectable.id);
if (whiteListed) {
let params;
try {
params = whiteListed.getParams(...args);
} catch (e) {
params = {
error:
"Tried to produce params for telemetry, but getParams() threw an error",
};
logError(
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
e,
);
}
emitTelemetry({
action: currentContext.injectable.id,
params,
});
}
return instance(...args);
};
if (!isFunction(instance)) {
return instance;
}
return instance;
return (...args: any[]) => {
const currentContext = di.context.at(-1);
assert(currentContext);
const emitTelemetry = diForDecorator.inject(
emitTelemetryInjectable
);
const logError = diForDecorator.inject(logErrorInjectable);
const whiteList = diForDecorator.inject(
telemetryWhiteListForFunctionsInjectable
);
const whiteListMap = getWhiteListMap(whiteList);
const whiteListed = whiteListMap.get(currentContext.injectable.id);
if (whiteListed) {
let params;
try {
params = whiteListed.getParams(...args);
} catch (e) {
params = {
error:
"Tried to produce params for telemetry, but getParams() threw an error",
};
logError(
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
e
);
}
emitTelemetry({
action: currentContext.injectable.id,
params,
});
}
return instance(...args);
};
},
}),