1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/technical-features/feature-core/src/register-feature.ts
Sebastian Malton 807f98ed1b
Persist apiVersion when editing resources in monaco (#4406)
* Persist apiVersion when editing resources in monaco

- Use a new custom k8slens prefixed label

- Means that users aren't surprised when they use lens to update a
  resource to a new apiVersionWithGroup

- Doesn't touch the versions in the stores

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

* Fix lint

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

* fix: Fix lint issues

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

* chore: make lint not bail on failure

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

* chore: Run lint:fix on all files

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

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-04-05 10:56:23 -04:00

77 lines
2.3 KiB
TypeScript

import type { DiContainer } from "@ogre-tools/injectable";
import { getInjectable } from "@ogre-tools/injectable";
import type { Feature } from "./feature";
import { featureContextMapInjectable, featureContextMapInjectionToken } from "./feature-context-map-injectable";
const createFeatureContext = (feature: Feature, di: DiContainer) => {
const featureContextInjectable = getInjectable({
id: feature.id,
instantiate: (diForContextOfFeature) => ({
register: () => {
feature.register(diForContextOfFeature);
},
deregister: () => {
diForContextOfFeature.deregister(featureContextInjectable);
},
dependedBy: new Map<Feature, number>(),
numberOfRegistrations: 0,
}),
scope: true,
});
di.register(featureContextInjectable);
const featureContextMap = di.inject(featureContextMapInjectable);
const featureContext = di.inject(featureContextInjectable);
featureContextMap.set(feature, featureContext);
return featureContext;
};
const registerFeatureRecursed = (di: DiContainer, feature: Feature, dependedBy?: Feature) => {
const featureContextMaps = di.injectMany(featureContextMapInjectionToken);
if (featureContextMaps.length === 0) {
di.register(featureContextMapInjectable);
}
const featureContextMap = di.inject(featureContextMapInjectable);
const existingFeatureContext = featureContextMap.get(feature);
if (!dependedBy && existingFeatureContext && existingFeatureContext.dependedBy.size === 0) {
throw new Error(`Tried to register feature "${feature.id}", but it was already registered.`);
}
const featureContext = existingFeatureContext || createFeatureContext(feature, di);
featureContext.numberOfRegistrations++;
if (dependedBy) {
const oldNumberOfDependents = featureContext.dependedBy.get(dependedBy) || 0;
const newNumberOfDependents = oldNumberOfDependents + 1;
featureContext.dependedBy.set(dependedBy, newNumberOfDependents);
}
if (!existingFeatureContext) {
featureContext.register();
}
feature.dependencies?.forEach((dependency) => {
registerFeatureRecursed(di, dependency, feature);
});
};
export const registerFeature = (di: DiContainer, ...features: Feature[]) => {
features.forEach((feature) => {
registerFeatureRecursed(di, feature);
});
};