I'm implementing a scoped container architecture such that a new container is created for every Express request (or apollographql request). I have a lifecycle method that can be called after we're done sending the response, which is good for cleaning up and freeing memory, and this method can reference the context of the request we're done serving. In that context I have a reference to the inversifyjs container that I created earlier so I can reference that container in the cleanup method, how can I delete that container?
function ScopedContainer(userId: number) {
const container = new Container();
container.bind<number>(TYPES.userId).toConstantValue(userId);
//rest of bindings container.bind ...
return container;
}
async({req, res}) => {
const { headers } = req;
const { userId } = headers;
const container = ScopedContainer(clientId);
const context = { container }
///...
}
willSendResponse(({container}) => {
// how to instruct inversifyjs to ditch all references to container?
// something like container.destroy?
});
I want to make sure that this container will not stay there after the http request is done, causing huge memory leak. Problem is I'm not sure it will be garbage collected as reference count reaches zeros. I'm also using lazyInject from inversify-inject-decorators
Is there any inversifyjs container API like destroy or reset method?