Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dima-zemingo opened this issue Jun 18, 2020 · 7 comments
Closed

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

dima-zemingo opened this issue Jun 18, 2020 · 7 comments
Labels
core status:checking currently in analysis - discussion or need more detailed specs type:issue
Milestone

Comments

@dima-zemingo
Copy link

dima-zemingo commented Jun 18, 2020

    val module = module {
        single { SomeComponent() }.onClose{
          // not called
       }
    }
  loadKoinModules(module)

// do some work
unloadKoinModules(module)
}

@Reevn
Copy link

Reevn commented Jul 30, 2020

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
Copy link
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
Copy link

Reevn commented Apr 6, 2021

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
Copy link

stale bot commented Mar 23, 2022

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.

@stale stale bot added the status:wontfix label Mar 23, 2022
@grabarczyk-t
Copy link

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.

@stale stale bot removed the status:wontfix label Mar 23, 2022
@arnaudgiuliani arnaudgiuliani added this to the 3.2.1 milestone Jun 27, 2022
@arnaudgiuliani arnaudgiuliani added core type:issue status:checking currently in analysis - discussion or need more detailed specs labels Jun 27, 2022
@arnaudgiuliani
Copy link
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.

@arnaudgiuliani arnaudgiuliani modified the milestones: 3.2.1, 3.3.0 Aug 29, 2022
@arnaudgiuliani
Copy link
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
Labels
core status:checking currently in analysis - discussion or need more detailed specs type:issue
Projects
None yet
Development

No branches or pull requests

4 participants