@inversifyjs/core
Version:
InversifyJs core package
57 lines • 2.53 kB
JavaScript
import { BindingConstraintsImplementation, } from '../../binding/models/BindingConstraintsImplementation.js';
import { InversifyCoreError } from '../../error/models/InversifyCoreError.js';
import { InversifyCoreErrorKind } from '../../error/models/InversifyCoreErrorKind.js';
import { LazyPlanServiceNode } from '../models/LazyPlanServiceNode.js';
/**
* Detach a binding to the root service node if it is context-free.
* @param serviceNode The service node to attach the binding to.
* @param binding The binding to attach.
* @param bindingConstraintsList The list of binding constraints.
* @param optionalBindings Whether the bindings are optional.
* @returns True if the binding requires ancestor metadata, false otherwise.
*/
export function removeServiceNodeBindingIfContextFree(serviceNode, binding, bindingConstraintsList, optionalBindings) {
if (LazyPlanServiceNode.is(serviceNode) && !serviceNode.isExpanded()) {
return {
bindingNodeRemoved: undefined,
isContextFreeBinding: true,
};
}
const bindingConstraints = new BindingConstraintsImplementation(bindingConstraintsList.last);
if (!binding.isSatisfiedBy(bindingConstraints) ||
bindingConstraintsList.last.elem.getAncestorsCalled) {
return {
bindingNodeRemoved: undefined,
isContextFreeBinding: !bindingConstraintsList.last.elem.getAncestorsCalled,
};
}
let bindingNodeRemoved;
if (Array.isArray(serviceNode.bindings)) {
serviceNode.bindings = serviceNode.bindings.filter((bindingNode) => {
if (bindingNode.binding === binding) {
bindingNodeRemoved = bindingNode;
return false;
}
return true;
});
}
else {
if (serviceNode.bindings?.binding === binding) {
bindingNodeRemoved = serviceNode.bindings;
if (optionalBindings) {
serviceNode.bindings = undefined;
}
else {
if (!LazyPlanServiceNode.is(serviceNode)) {
throw new InversifyCoreError(InversifyCoreErrorKind.planning, 'Unexpected non-lazy plan service node. This is likely a bug in the planning logic. Please, report this issue');
}
serviceNode.invalidate();
}
}
}
return {
bindingNodeRemoved: bindingNodeRemoved,
isContextFreeBinding: true,
};
}
//# sourceMappingURL=removeServiceNodeBindingIfContextFree.js.map