@inversifyjs/container
Version:
InversifyJs container
108 lines • 5.17 kB
JavaScript
import { CacheBindingInvalidationKind, resolveModuleDeactivations, } from '@inversifyjs/core';
import { BindToFluentSyntaxImplementation } from '../../binding/models/BindingFluentSyntaxImplementation.js';
import { InversifyContainerError } from '../../error/models/InversifyContainerError.js';
import { InversifyContainerErrorKind } from '../../error/models/InversifyContainerErrorKind.js';
export class ContainerModuleManager {
#bindingManager;
#deactivationParams;
#defaultScope;
#planResultCacheManager;
#serviceReferenceManager;
constructor(bindingManager, deactivationParams, defaultScope, planResultCacheManager, serviceReferenceManager) {
this.#bindingManager = bindingManager;
this.#deactivationParams = deactivationParams;
this.#defaultScope = defaultScope;
this.#planResultCacheManager = planResultCacheManager;
this.#serviceReferenceManager = serviceReferenceManager;
}
async loadAsync(...modules) {
// eslint-disable-next-line @typescript-eslint/await-thenable
await Promise.all(this.#load(...modules));
}
load(...modules) {
const results = this.#load(...modules);
for (const result of results) {
if (result !== undefined) {
throw new InversifyContainerError(InversifyContainerErrorKind.invalidOperation, 'Unexpected asynchronous module load. Consider using container.loadAsync() instead.');
}
}
}
async unloadAsync(...modules) {
// eslint-disable-next-line @typescript-eslint/await-thenable
await Promise.all(this.#unload(...modules));
/*
* Removing module related objects here so unload is deterministic.
*
* Removing modules as soon as resolveModuleDeactivations takes effect leads to
* module deactivations not triggering previously deleted deactivations,
* introducing non determinism depending in the order in which modules are
* deactivated.
*/
this.#clearAfterUnloadModules(modules);
}
unload(...modules) {
const results = this.#unload(...modules);
for (const result of results) {
if (result !== undefined) {
throw new InversifyContainerError(InversifyContainerErrorKind.invalidOperation, 'Unexpected asynchronous module unload. Consider using container.unloadAsync() instead.');
}
}
/*
* Removing module related objects here so unload is deterministic.
*
* Removing modules as soon as resolveModuleDeactivations takes effect leads to
* module deactivations not triggering previously deleted deactivations,
* introducing non determinism depending in the order in which modules are
* deactivated.
*/
this.#clearAfterUnloadModules(modules);
}
#buildContainerModuleLoadOptions(moduleId) {
return {
bind: (serviceIdentifier) => {
return new BindToFluentSyntaxImplementation((binding) => {
this.#setBinding(binding);
}, moduleId, this.#defaultScope, serviceIdentifier);
},
isBound: this.#bindingManager.isBound.bind(this.#bindingManager),
onActivation: (serviceIdentifier, activation) => {
this.#serviceReferenceManager.activationService.add(activation, {
moduleId,
serviceId: serviceIdentifier,
});
},
onDeactivation: (serviceIdentifier, deactivation) => {
this.#serviceReferenceManager.deactivationService.add(deactivation, {
moduleId,
serviceId: serviceIdentifier,
});
},
rebind: this.#bindingManager.rebind.bind(this.#bindingManager),
rebindAsync: this.#bindingManager.rebindAsync.bind(this.#bindingManager),
unbind: this.#bindingManager.unbind.bind(this.#bindingManager),
unbindAsync: this.#bindingManager.unbindAsync.bind(this.#bindingManager),
};
}
#clearAfterUnloadModules(modules) {
for (const module of modules) {
this.#serviceReferenceManager.activationService.removeAllByModuleId(module.id);
this.#serviceReferenceManager.bindingService.removeAllByModuleId(module.id);
this.#serviceReferenceManager.deactivationService.removeAllByModuleId(module.id);
}
this.#serviceReferenceManager.planResultCacheService.clearCache();
}
#load(...modules) {
return modules.map((module) => module.load(this.#buildContainerModuleLoadOptions(module.id)));
}
#setBinding(binding) {
this.#serviceReferenceManager.bindingService.set(binding);
this.#planResultCacheManager.invalidateService({
binding: binding,
kind: CacheBindingInvalidationKind.bindingAdded,
});
}
#unload(...modules) {
return modules.map((module) => resolveModuleDeactivations(this.#deactivationParams, module.id));
}
}
//# sourceMappingURL=ContainerModuleManager.js.map