Fix behaviour of auto generated CRD KubeApis and KubeObjectStores (#7384)

* Simplify CRD KubeApi registrations

- Switch to auto injectable registrations

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Make sure that stores can still be retrieved

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Cleanup get extension fake to simplify impl

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Simplify logic for extensionInjectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix test in differencing registrator

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Cleanup code style

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix some tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix HPA details tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2023-03-22 17:51:26 +02:00
1 parent 8a80607d85
commit 4b1d740d61
17 files changed
+315 -189

No files matched your search

@@ -2,29 +2,18 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { Injectable } from "@ogre-tools/injectable";
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { difference, find, map } from "lodash";
import { reaction, runInAction } from "mobx";
import { disposer } from "@k8slens/utilities";
import type { LensExtension } from "../../lens-extension";
import { extensionRegistratorInjectionToken } from "../extension-registrator-injection-token";
import { injectableDifferencingRegistratorWith } from "../../../common/utils/registrator-helper";
export interface Extension {
register: () => void;
deregister: () => void;
}
const idsToInjectables = (ids: string[], injectables: Injectable<any, any, any>[]) => ids.map(id => {
const injectable = find(injectables, { id });
if (!injectable) {
throw new Error(`Injectable ${id} not found`);
}
return injectable;
});
const extensionInjectable = getInjectable({
id: "extension",
@@ -35,36 +24,27 @@ const extensionInjectable = getInjectable({
instantiate: (childDi) => {
const extensionRegistrators = childDi.injectMany(extensionRegistratorInjectionToken);
const reactionDisposer = disposer();
const injectableDifferencingRegistrator = injectableDifferencingRegistratorWith(childDi);
return {
register: () => {
extensionRegistrators.forEach((getInjectablesOfExtension) => {
const injectables = getInjectablesOfExtension(instance);
for (const extensionRegistrator of extensionRegistrators) {
const injectables = extensionRegistrator(instance);
reactionDisposer.push(
// injectables is either an array or a computed array, in which case
// we need to update the registered injectables with a reaction every time they change
reaction(
() => Array.isArray(injectables) ? injectables : injectables.get(),
(currentInjectables, previousInjectables = []) => {
// Register new injectables and deregister removed injectables by id
const currentIds = map(currentInjectables, "id");
const previousIds = map(previousInjectables, "id");
const idsToAdd = difference(currentIds, previousIds);
const idsToRemove = previousIds.filter(previousId => !currentIds.includes(previousId));
if (idsToRemove.length > 0) {
childDi.deregister(...idsToInjectables(idsToRemove, previousInjectables));
}
if (idsToAdd.length > 0) {
childDi.register(...idsToInjectables(idsToAdd, currentInjectables));
}
}, {
if (Array.isArray(injectables)) {
runInAction(() => {
injectableDifferencingRegistrator(injectables);
});
} else {
reactionDisposer.push(reaction(
() => injectables.get(),
injectableDifferencingRegistrator,
{
fireImmediately: true,
},
));
});
}
}
},
deregister: () => {