mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add stricter typing, and simplify code
Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
c058e89238
commit
95babe245c
@ -2,7 +2,10 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { DiContainerForInjection } from "@ogre-tools/injectable";
|
||||
import type {
|
||||
DiContainerForInjection,
|
||||
Injectable,
|
||||
} from "@ogre-tools/injectable";
|
||||
|
||||
import {
|
||||
lifecycleEnum,
|
||||
@ -11,7 +14,6 @@ import {
|
||||
} from "@ogre-tools/injectable";
|
||||
import assert from "assert";
|
||||
|
||||
|
||||
import { isFunction } from "lodash/fp";
|
||||
import emitTelemetryInjectable from "./emit-telemetry.injectable";
|
||||
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
|
||||
@ -23,31 +25,33 @@ const telemetryDecoratorInjectable = getInjectable({
|
||||
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
||||
|
||||
const whiteList = diForDecorator.inject(
|
||||
telemetryWhiteListForFunctionsInjectable,
|
||||
telemetryWhiteListForFunctionsInjectable
|
||||
);
|
||||
|
||||
const shouldEmitTelemetry = shouldEmitTelemetryFor(whiteList);
|
||||
|
||||
return {
|
||||
decorate:
|
||||
(instantiateToBeDecorated: any) =>
|
||||
(di: DiContainerForInjection, instantiationParameter: any) => {
|
||||
const instance = instantiateToBeDecorated(di, instantiationParameter);
|
||||
(di: DiContainerForInjection, instantiationParameter: any) => {
|
||||
const instance = instantiateToBeDecorated(di, instantiationParameter);
|
||||
|
||||
if (isFunction(instance)) {
|
||||
return (...args: any[]) => {
|
||||
const currentContext = di.context.at(-1);
|
||||
if (isFunction(instance)) {
|
||||
return (...args: any[]) => {
|
||||
const currentContext = di.context.at(-1);
|
||||
|
||||
assert(currentContext);
|
||||
assert(currentContext);
|
||||
|
||||
if (shouldEmitTelemetry(currentContext, whiteList)) {
|
||||
emitTelemetry({ action: currentContext.injectable.id, args });
|
||||
}
|
||||
if (shouldEmitTelemetry(currentContext.injectable)) {
|
||||
emitTelemetry({ action: currentContext.injectable.id, args });
|
||||
}
|
||||
|
||||
return instance(...args);
|
||||
};
|
||||
}
|
||||
return instance(...args);
|
||||
};
|
||||
}
|
||||
|
||||
return instance;
|
||||
},
|
||||
return instance;
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@ -57,9 +61,10 @@ const telemetryDecoratorInjectable = getInjectable({
|
||||
injectionToken: instantiationDecoratorToken,
|
||||
});
|
||||
|
||||
const shouldEmitTelemetry = (currentContext: any, whiteList: any) => (
|
||||
currentContext.injectable.tags?.includes("emit-telemetry") ||
|
||||
whiteList.includes(currentContext.injectable.id)
|
||||
);
|
||||
const shouldEmitTelemetryFor = (whiteList: string[]) => (
|
||||
injectable: Injectable<any, any, any>,
|
||||
) =>
|
||||
injectable.tags?.includes("emit-telemetry") ||
|
||||
whiteList.includes(injectable.id);
|
||||
|
||||
export default telemetryDecoratorInjectable;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user