koin icon indicating copy to clipboard operation
koin copied to clipboard

Automatically destroy Closeable beans on shutdown

Open alturkovic opened this issue 1 year ago • 4 comments

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

alturkovic avatar Feb 21 '24 18:02 alturkovic

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?

marcellogalhardo avatar Feb 24 '24 17:02 marcellogalhardo

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 :/

alturkovic avatar Feb 24 '24 23:02 alturkovic

Really need this feature too. Adding onClose callbacks manually is not sweet.

slipdef avatar May 01 '24 22:05 slipdef