Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 23x 23x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 1x 1x 79x 79x 79x 21x 21x 79x 79x 79x 79x 79x 79x 1x 1x 78x 78x 79x 79x 79x 79x 34x 34x 34x 34x 34x 34x 78x 78x 58x 58x 78x 79x 34x 34x 79x 1x 1x 31x 45x 45x 31x | 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 newNumberOfDependants = oldNumberOfDependents + 1;
featureContext.dependedBy.set(dependedBy, newNumberOfDependants);
}
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);
});
};
|