koin
koin copied to clipboard
Automatically destroy Closeable beans on shutdown
Is your feature request related to a problem? Please describe.
I have a lot of beans which implement the Closeable
interface. It would be nice if koin could destroy these beans on shutdown automatically instead of having to use single { MyBean } onClose { it?.close() }
Describe the solution you'd like
Have koin call close()
on all Closeable
beans on shutdown.
Describe alternatives you've considered Support a new callback when destroying beans so I could do something similar to:
startKoin {
module {
singleOf(::SomeBean)
single { MyBean }
}
onClose {
if (it is Closeable) {
it.close()
}
}
}
Target Koin project core
It seems you want all bindings that implement a Closeable
interface to act as onClose { it?.close}
automatically.
How would you differentiate a Closeable
binding that is provided in Koin that (1) should be closeable by Koin onClose
from (2) one that should not be closed by Koin, such as one associated with androidx ViewModel.addCloseable
?
I have no experience on Android, I was comparing the behaviour of Closeable
beans in Spring. When shutting down a Spring application, all beans implementing Closeable
are being closed, like having an implicit onClose { it?.close}
call. I have not encountered a scenario where I implemented a Closeable
interface that was handled as a singleton that I did not want to be closed on shutdown, so I am not familiar with the Android handling :/
Really need this feature too. Adding onClose callbacks manually is not sweet.