Skip to content

Commit

Permalink
refactor(#463): deprecate ScopeCloseable in benefit of kotlin AutoClo…
Browse files Browse the repository at this point in the history
…seable
  • Loading branch information
romainbsl committed Jan 7, 2025
1 parent a032fb4 commit 97ad91c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 12 additions & 9 deletions kodein-di/src/commonMain/kotlin/org/kodein/di/bindings/scopes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import org.kodein.di.internal.synchronizedIfNotNull
import org.kodein.di.internal.synchronizedIfNull
import org.kodein.type.TypeToken

public interface ScopeCloseable {
public fun close()
}
@Deprecated(
message = "Use kotlin AutoCloseable instead.",
replaceWith = ReplaceWith("AutoCloseable", imports = arrayOf("AutoCloseable"))
)
public typealias ScopeCloseable = AutoCloseable

private typealias RegKey = Any

/**
* A registry is responsible managing references inside a scope.
*/
@Suppress("DEPRECATION")
public sealed class ScopeRegistry : ScopeCloseable {
/**
* Get or create a value that correspond for the given key.
Expand Down Expand Up @@ -74,7 +77,7 @@ public class StandardScopeRegistry : ScopeRegistry() {
override fun values(): List<Pair<RegKey, () -> Any?>> = _cache.map { it.toPair() }

override fun remove(key: RegKey) {
(_cache.remove(key)?.invoke() as? ScopeCloseable)?.close()
(_cache.remove(key)?.invoke() as? AutoCloseable)?.close()
}

/**
Expand All @@ -87,7 +90,7 @@ public class StandardScopeRegistry : ScopeRegistry() {
refs
}
refs.forEach {
(it.invoke() as? ScopeCloseable)?.close()
(it.invoke() as? AutoCloseable)?.close()
}
}

Expand Down Expand Up @@ -125,7 +128,7 @@ public class SingleItemScopeRegistry : ScopeRegistry() {
oldRef to value
}
)
(oldRef?.invoke() as? ScopeCloseable)?.close()
(oldRef?.invoke() as? AutoCloseable)?.close()
return value
}

Expand All @@ -151,7 +154,7 @@ public class SingleItemScopeRegistry : ScopeRegistry() {
}
)

(ref?.invoke() as? ScopeCloseable)?.close()
(ref?.invoke() as? AutoCloseable)?.close()
}

/**
Expand All @@ -168,7 +171,7 @@ public class SingleItemScopeRegistry : ScopeRegistry() {
}
)

(ref?.invoke() as? ScopeCloseable)?.close()
(ref?.invoke() as? AutoCloseable)?.close()
}
}

Expand Down Expand Up @@ -221,7 +224,7 @@ public interface Scope<in C> {
*
* This is kind of equivalent to having no scope at all, except that you can call [clear].
*/
public open class UnboundedScope(public val registry: ScopeRegistry = StandardScopeRegistry()) : Scope<Any?>, ScopeCloseable {
public open class UnboundedScope(public val registry: ScopeRegistry = StandardScopeRegistry()) : Scope<Any?>, AutoCloseable {
override fun getRegistry(context: Any?): ScopeRegistry = registry

override fun close(): Unit = registry.clear()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.kodein.di.test

import org.kodein.di.bindings.ScopeCloseable

class CloseableData(val name: String? = null) : ScopeCloseable {
class CloseableData(val name: String? = null) : AutoCloseable {
var closed = false
private set

Expand Down

0 comments on commit 97ad91c

Please sign in to comment.