Kord-Extensions / kord-extensions

Kord extensions framework, providing commands and distinct units of functionality
https://kordex.dev
European Union Public License 1.2
110 stars 27 forks source link

createdAtStart Koin flag is ignored #78

Closed derkalaender closed 2 years ago

derkalaender commented 3 years ago

Description

Creating a Koin module or definition inside of the beforeKoinSetup hook, the createdAtStart flag gets ignored, as those modules/definitions do not in fact get instantiated at start. This flag is useful for Database connections for example, which need to be established directly at application start to do migrations etc.

This might not be a deal-breaker at first, since you could just instantiate the object yourself (if it doesnt inject anything) and then create a single from that. However, things get difficult when that object itself has other dependencies. So you'd either need to use getKoin() manually or create those other dependencies outside of Koin as well, which might get ugly pretty quickly.

Versions

Latest Kord Extensions 1.5.0-SNAPSHOT

Code Examples

class MyDatabase(dep: DepOne) : KoinComponent, KLoggable {
    override val logger: KLogger = logger()

    val otherDep: DepTwo by inject()

    init {
        logger.info { "Setting up database with dependencies $dep and $otherDep" }
    }
}

suspend fun ExtensibleBotBuilder.setupDatabase() {
    hooks {
        beforeKoinSetup {
            loadModule {
                single(createdAtStart = true) { MyDatabase(get()) }
            }
        }
    }
}

No log can be seen (only once another object uses MyDatabase). MyDatabase can also not be instantiated externally because of the inject

Suggestions

Here in setupKoin() the startKoin() is called before running the runBeforeKoinSetup hooks. This might be the culprit, because according to Koin docs, anything flagged with createdAtStart is executed once startKoin is called and at that point, the modules have not been registered yet.

boring-cyborg[bot] commented 3 years ago

Hello, and thanks for opening an issue! As this is the first time you've created an issue on this repository, we'd just like to offer you a warm welcome to the project, and the following pointers:

gdude2002 commented 3 years ago

Hello, just getting around to this now!

Do you know if modules can be registered globally before startKoin is called? I guess the implication here is that they can, but it'd be good to double-check whether getKoin is safe to use that early.

I'll look into it.

gdude2002 commented 3 years ago

Nope, loadKoinModules() can't be called before startKoin - any ideas?

gdude2002 commented 2 years ago

Marking this stale as it hasn't been updated in 5 days.

gdude2002 commented 2 years ago

This hasn't been updated for 10 days, and I'm at a loss as to how this could be implemented. As the reporter seems to have disappeared, I'll be closing this issue now - if you have any further ideas or a proof of concept, please feel free to open another issue or comment on this one.