-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
Fix DeclaredScopedInstance into adapted ScopedInstanceFactory #2111
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2017-Present 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.core.error | ||
|
||
/** | ||
* Scoep value search for given ScopeId is not found | ||
* @author Arnaud Giuliani | ||
*/ | ||
class MissingScopeValueException(msg: String) : Exception(msg) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
package org.koin.core.instance | ||
|
||
import org.koin.core.definition.BeanDefinition | ||
import org.koin.core.error.MissingScopeValueException | ||
import org.koin.core.scope.Scope | ||
import org.koin.core.scope.ScopeID | ||
import org.koin.mp.KoinPlatformTools | ||
|
@@ -24,11 +25,18 @@ import org.koin.mp.KoinPlatformTools | |
* Single instance holder | ||
* @author Arnaud Giuliani | ||
*/ | ||
class ScopedInstanceFactory<T>(beanDefinition: BeanDefinition<T>) : | ||
class ScopedInstanceFactory<T>(beanDefinition: BeanDefinition<T>, val holdInstance : Boolean = true) : | ||
InstanceFactory<T>(beanDefinition) { | ||
|
||
private var values = hashMapOf<ScopeID, T>() | ||
|
||
fun size() = values.size | ||
|
||
@PublishedApi | ||
internal fun saveValue(id : ScopeID, value : T){ | ||
values[id] = value | ||
} | ||
|
||
override fun isCreated(context: ResolutionContext?): Boolean = (values[context?.scope?.id] != null) | ||
|
||
override fun drop(scope: Scope?) { | ||
|
@@ -42,20 +50,20 @@ class ScopedInstanceFactory<T>(beanDefinition: BeanDefinition<T>) : | |
return if (values[context.scope.id] == null) { | ||
super.create(context) | ||
} else { | ||
values[context.scope.id] ?: error("Scoped instance not found for ${context.scope.id} in $beanDefinition") | ||
values[context.scope.id] ?: throw MissingScopeValueException("Factory.create - Scoped instance not found for ${context.scope.id} in $beanDefinition") | ||
} | ||
} | ||
|
||
override fun get(context: ResolutionContext): T { | ||
if (context.scope.scopeQualifier != beanDefinition.scopeQualifier) { | ||
error("Wrong Scope: trying to open instance for ${context.scope.id} in $beanDefinition") | ||
error("Wrong Scope qualifier: trying to open instance for ${context.scope.id} in $beanDefinition") | ||
} | ||
KoinPlatformTools.synchronized(this) { | ||
if (!isCreated(context)) { | ||
values[context.scope.id] = create(context) | ||
if (!isCreated(context) && holdInstance) { | ||
values[context.scope.id] = super.create(context) | ||
} | ||
} | ||
return values[context.scope.id] ?: error("Scoped instance not found for ${context.scope.id} in $beanDefinition") | ||
return values[context.scope.id] ?: throw MissingScopeValueException("Factory.get -Scoped instance not found for ${context.scope.id} in $beanDefinition") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. Make sure you increase major version before release There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not in that case, as with such new behavior we would have ISE and can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then this api, including exception, should be internal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are right, it is still an internal API. I can tag them @KoinInternalApi in 4.1 branch. |
||
} | ||
|
||
override fun dropAll() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This accessor is not thread safe