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.
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* 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 {
|
import {
|
||||||
lifecycleEnum,
|
lifecycleEnum,
|
||||||
@ -11,7 +14,6 @@ import {
|
|||||||
} from "@ogre-tools/injectable";
|
} from "@ogre-tools/injectable";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
|
||||||
|
|
||||||
import { isFunction } from "lodash/fp";
|
import { isFunction } from "lodash/fp";
|
||||||
import emitTelemetryInjectable from "./emit-telemetry.injectable";
|
import emitTelemetryInjectable from "./emit-telemetry.injectable";
|
||||||
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
|
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
|
||||||
@ -23,31 +25,33 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
||||||
|
|
||||||
const whiteList = diForDecorator.inject(
|
const whiteList = diForDecorator.inject(
|
||||||
telemetryWhiteListForFunctionsInjectable,
|
telemetryWhiteListForFunctionsInjectable
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const shouldEmitTelemetry = shouldEmitTelemetryFor(whiteList);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
decorate:
|
decorate:
|
||||||
(instantiateToBeDecorated: any) =>
|
(instantiateToBeDecorated: any) =>
|
||||||
(di: DiContainerForInjection, instantiationParameter: any) => {
|
(di: DiContainerForInjection, instantiationParameter: any) => {
|
||||||
const instance = instantiateToBeDecorated(di, instantiationParameter);
|
const instance = instantiateToBeDecorated(di, instantiationParameter);
|
||||||
|
|
||||||
if (isFunction(instance)) {
|
if (isFunction(instance)) {
|
||||||
return (...args: any[]) => {
|
return (...args: any[]) => {
|
||||||
const currentContext = di.context.at(-1);
|
const currentContext = di.context.at(-1);
|
||||||
|
|
||||||
assert(currentContext);
|
assert(currentContext);
|
||||||
|
|
||||||
if (shouldEmitTelemetry(currentContext, whiteList)) {
|
if (shouldEmitTelemetry(currentContext.injectable)) {
|
||||||
emitTelemetry({ action: currentContext.injectable.id, args });
|
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,
|
injectionToken: instantiationDecoratorToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
const shouldEmitTelemetry = (currentContext: any, whiteList: any) => (
|
const shouldEmitTelemetryFor = (whiteList: string[]) => (
|
||||||
currentContext.injectable.tags?.includes("emit-telemetry") ||
|
injectable: Injectable<any, any, any>,
|
||||||
whiteList.includes(currentContext.injectable.id)
|
) =>
|
||||||
);
|
injectable.tags?.includes("emit-telemetry") ||
|
||||||
|
whiteList.includes(injectable.id);
|
||||||
|
|
||||||
export default telemetryDecoratorInjectable;
|
export default telemetryDecoratorInjectable;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user