You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each resolver (registration) can specify a disposer function to call when container.dispose() is called.
Motivation
This is useful for managing dependencies that have resources to dispose—connection pools for example.
In test runners like Jest, watch-mode will recycle worker processes which means connections may or may not have been closed between each run. In testing HTTP servers, It is a good practice to call server.close() after the tests are done.
We want to be able to run cleanup on dependencies in the same way. For example:
server.on('close',()=>container.dispose())
Proposed API
Introduce container.dispose()
Introduce disposer option for asClass and asFunction resolvers
Disposing a container also disposes it's child (scoped) containers
Containers are disposed bottom-first; this means scoped containers are disposed before their parent.
Only SCOPED and SINGLETON registrations can be disposed as they are the only ones the container caches.
Example
constpg=require('pg')const{ createContainer, asFunction }=require('awilix')constcontainer=createContainer().register({pool: (asFunction(()=>newpg.Pool({ ... })).singleton().disposer((pool)=>pool.end()))})// .. later, but only if a `pool` was ever createdcontainer.dispose().then(()=>{console.log('One disposable connection.. disposed! Huehehehe')})
The text was updated successfully, but these errors were encountered:
Would it be possible to make disposers async?
Like waiting a disconnection before completing the disposal.
An example with Discord.js:
constDiscord=require('discord.js')const{ createContainer, asFunction }=require('awilix')constcontainer=createContainer().register({client: (asFunction(()=>newDiscord.Client({ ... })).disposer((client)=>client.destroy())// Destroy is an async method in this case)})// .. latercontainer.dispose().then(()=>{console.log('Client destroyed')})
Proposal
Support disposing dependencies.
Each resolver (registration) can specify a disposer function to call when
container.dispose()
is called.Motivation
This is useful for managing dependencies that have resources to dispose—connection pools for example.
In test runners like Jest, watch-mode will recycle worker processes which means connections may or may not have been closed between each run. In testing HTTP servers, It is a good practice to call
server.close()
after the tests are done.We want to be able to run cleanup on dependencies in the same way. For example:
Proposed API
container.dispose()
disposer
option forasClass
andasFunction
resolversSCOPED
andSINGLETON
registrations can be disposed as they are the only ones the container caches.Example
The text was updated successfully, but these errors were encountered: