Skip to content

Commit

Permalink
Koin scope injection refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Jun 27, 2023
1 parent 08d5a35 commit c0c167f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ProjectRootComponent(
private val removeMenu: (id: String) -> Unit,
) : ProjectComponentBase(projectDef, componentContext), ProjectRoot {

private val projScope = createProjectScope(projectDef)

private val synchronizer: ClientProjectSynchronizer by projectInject()
private val projectEditor: ProjectEditorRepository by projectInject()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ suspend fun KoinComponent.temporaryProjectTask(projectDef: ProjectDef, block: su
}
}

fun createProjectScope(projectDef: ProjectDef): Scope {
val alreadyCreated = getKoin().getScopeOrNull(ProjectDefScope(projectDef).getScopeId()) != null
if (alreadyCreated) error("Scope was already created")

val defScope = ProjectDefScope(projectDef)
val projScope = getKoin().createScope<ProjectDefScope>(defScope.getScopeId())
projScope.declare(projectDef)

return projScope
}

suspend fun openProjectScope(projectDef: ProjectDef): Scope {
val defScope = ProjectDefScope(projectDef)

val needsInit = getKoin().getScopeOrNull(ProjectDefScope(projectDef).getScopeId()) == null
val projScope = getKoin().getOrCreateScope<ProjectDefScope>(defScope.getScopeId())
projScope.declare(projectDef)

if (needsInit) {
initializeProjectScope(projectDef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.darkrockstudios.apps.hammer.common.dependencyinjection.ProjectDefScop
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.Qualifier
import org.koin.mp.KoinPlatformTools

Expand All @@ -22,11 +21,5 @@ inline fun <reified T : Any> ProjectScoped.projectInject(
noinline parameters: ParametersDefinition? = null
): Lazy<T> =
lazy(mode) {
val newParameters = if (parameters != null) {
val params = parameters()
parametersOf(arrayOf(params.values.toMutableList().add(projectScope.projectDef)))
} else {
parametersOf(projectScope.projectDef)
}
projectScope.get<T>(qualifier) { newParameters }
projectScope.get<T>(qualifier, parameters)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.darkrockstudios.apps.hammer.common.dependencyinjection

import com.akuleshov7.ktoml.Toml
import com.darkrockstudios.apps.hammer.base.http.createJsonSerializer
import com.darkrockstudios.apps.hammer.common.data.ProjectDef
import com.darkrockstudios.apps.hammer.common.data.accountrepository.AccountRepository
import com.darkrockstudios.apps.hammer.common.data.drafts.SceneDraftRepository
import com.darkrockstudios.apps.hammer.common.data.drafts.SceneDraftRepositoryOkio
Expand Down Expand Up @@ -85,6 +86,8 @@ val mainModule = module {
singleOf(::createProjectBackup) bind ProjectBackupRepository::class

scope<ProjectDefScope> {
scoped<ProjectDef> { get<ProjectDefScope>().projectDef }

scopedOf(::ProjectEditorRepositoryOkio) bind ProjectEditorRepository::class

scopedOf(::SceneDraftRepositoryOkio) bind SceneDraftRepository::class
Expand Down

0 comments on commit c0c167f

Please sign in to comment.