Skip to content

onClose not called on single, works when define in scope #834

Closed
@dima-zemingo

Description

@dima-zemingo
    val module = module {
        single { SomeComponent() }.onClose{
          // not called
       }
    }
  loadKoinModules(module)

// do some work
unloadKoinModules(module)
}

Activity

Reevn

Reevn commented on Jul 30, 2020

@Reevn

From looking at the source code I think the onClose callback is not implemented for root definitions. It's only called once in the codebase when a scope is closed.

This seems like a major problem, but apparently not many people are using onClose? 🤔

dima-zemingo

dima-zemingo commented on Aug 3, 2020

@dima-zemingo
Author

So will it be fixed? Because if you have this functionality, it should work or move the onClose to only support for Scope defenition

Reevn

Reevn commented on Apr 6, 2021

@Reevn

To better understand this: Is this actually a bug or is onClose actually not supposed to work for the root scope of a module? Having some way of automatically cleaning up components in a callback like onClose would be really beneficial when unloading a module, no matther if the definitions inside were further isolated in a sub-scope or not.

If this is a bug that needs to be fixed I'd be willing to have a look at it when I get the time.

stale

stale commented on Mar 23, 2022

@stale

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

grabarczyk-t

grabarczyk-t commented on Mar 23, 2022

@grabarczyk-t

For me this seems like a bug, it's weird that you can define an onClose method which isn't actually called when closing. And I don't see a reason why this shouldn't work for the root scope of a module. But I might be wrong, my understanding of Koin is limited.

added this to the 3.2.1 milestone on Jun 27, 2022
arnaudgiuliani

arnaudgiuliani commented on Aug 29, 2022

@arnaudgiuliani
Member

For me this seems like a bug, it's weird that you can define an onClose method which isn't actually called when closing. And I don't see a reason why this shouldn't work for the root scope of a module. But I might be wrong, my understanding of Koin is limited.

not a bug, as for now single definitions need to be dropped with module unloading, not closing it. It's more an issue to be able to write onClose on it ... as it would be only on Scoped definitions.

modified the milestones: 3.2.1, 3.3.0 on Aug 29, 2022
arnaudgiuliani

arnaudgiuliani commented on Dec 13, 2022

@arnaudgiuliani
Member

This issue is outdated. The following test is running successfully:

@Test
    fun test_onClose_from_unload(){
        var closed = false

        val module = module {
            single { Simple.ComponentA() } onClose {
                closed = !closed
                println("closing ComponentA - closed = $closed")
            }
        }

        val koin = koinApplication {
            printLogger(Level.DEBUG)
            modules(
                module
            )
        }.koin

        assertTrue(!closed)
        koin.unloadModules(listOf(module))
        assertTrue(closed)
        koin.loadModules(listOf(module))
        koin.unloadModules(listOf(module))
        assertTrue(!closed)
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    corestatus:checkingcurrently in analysis - discussion or need more detailed specstype:issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @arnaudgiuliani@Reevn@grabarczyk-t@dima-zemingo

        Issue actions

          onClose not called on single, works when define in scope · Issue #834 · InsertKoinIO/koin