All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.org.koin.dsl.ScopeSet.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2-khan
Show newest version
/*
 * Copyright 2017-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.koin.dsl

import org.koin.core.definition.BeanDefinition
import org.koin.core.definition.Definition
import org.koin.core.definition.DefinitionFactory
import org.koin.core.definition.Options
import org.koin.core.error.DefinitionOverrideException
import org.koin.core.mp.KoinMultiPlatform
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.ScopeDefinition

/**
 * DSL Scope Definition
 */
data class ScopeSet(val qualifier: Qualifier) {

    val definitions: MutableSet> = KoinMultiPlatform.emptyMutableSet()

    @Deprecated("Can't use Single in a scope. Use Scoped instead", level = DeprecationLevel.ERROR)
    @Suppress("UNUSED_PARAMETER")
    inline fun  single(
        qualifier: Qualifier? = null,
        override: Boolean = false,
        noinline definition: Definition
    ): BeanDefinition {
        error("Scoped definition is deprecated and has been replaced with Single scope definitions")
    }

    inline fun  scoped(
        qualifier: Qualifier? = null,
        override: Boolean = false,
        noinline definition: Definition
    ): BeanDefinition {
        val beanDefinition = DefinitionFactory.createScoped(qualifier, this.qualifier, definition)
        declareDefinition(beanDefinition, Options(false, override))
        if (!definitions.contains(beanDefinition)) {
            definitions.add(beanDefinition)
        } else {
            throw DefinitionOverrideException("Can't add definition $beanDefinition for scope ${this.qualifier} as it already exists")
        }
        return beanDefinition
    }

    inline fun  factory(
        qualifier: Qualifier? = null,
        override: Boolean = false,
        noinline definition: Definition
    ): BeanDefinition {
        val beanDefinition = DefinitionFactory.createFactory(qualifier, this.qualifier, definition)
        declareDefinition(beanDefinition, Options(false, override))
        if (!definitions.contains(beanDefinition)) {
            definitions.add(beanDefinition)
        } else {
            throw DefinitionOverrideException("Can't add definition $beanDefinition for scope ${this.qualifier} as it already exists")
        }
        return beanDefinition
    }

    fun createDefinition(): ScopeDefinition {
        val scopeDefinition = ScopeDefinition(qualifier)
        scopeDefinition.definitions.addAll(definitions)
        return scopeDefinition
    }

    fun  declareDefinition(definition: BeanDefinition, options: Options) {
        definition.updateOptions(options)
    }

    private fun BeanDefinition<*>.updateOptions(options: Options) {
        this.options = options.copy() //TODO: Probably don't need this, but still
    }

    override fun toString(): String {
        return "Scope['$qualifier']"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy