Skip to content

Commit

Permalink
Add warning for missing Keyer. (#2816)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite authored Jan 28, 2025
1 parent 5bc7dd8 commit 5ae6212
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 0 additions & 1 deletion coil-core/src/commonMain/kotlin/coil3/ComponentRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class ComponentRegistry private constructor(
fun newBuilder() = Builder(this)

class Builder {

internal val interceptors: MutableList<Interceptor>
internal val mappers: MutableList<Pair<Mapper<out Any, *>, KClass<out Any>>>
internal val keyers: MutableList<Pair<Keyer<out Any>, KClass<out Any>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import coil3.size.isOriginal
import coil3.size.pxOrElse
import coil3.util.Logger
import coil3.util.isPlaceholderCached
import coil3.util.key
import coil3.util.log
import kotlin.math.abs

Expand All @@ -43,7 +44,7 @@ internal class MemoryCacheService(

// Slow path: create a new memory cache key.
eventListener.keyStart(request, mappedData)
val key = imageLoader.components.key(mappedData, options)
val key = imageLoader.components.key(mappedData, options, logger, TAG)
eventListener.keyEnd(request, key)
if (key == null) {
return null
Expand Down
27 changes: 27 additions & 0 deletions coil-core/src/commonMain/kotlin/coil3/util/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import coil3.decode.Decoder
import coil3.fetch.Fetcher
import coil3.intercept.Interceptor
import coil3.intercept.RealInterceptorChain
import coil3.key.Keyer
import coil3.request.ErrorResult
import coil3.request.ImageRequest
import coil3.request.NullRequestDataException
import coil3.request.Options
import kotlin.coroutines.CoroutineContext
import kotlin.experimental.ExperimentalNativeApi
import kotlin.reflect.KClass
Expand Down Expand Up @@ -46,6 +48,31 @@ internal fun AutoCloseable.closeQuietly() {

internal val EMPTY_IMAGE_FACTORY: (ImageRequest) -> Image? = { null }

/** Same as [ComponentRegistry.key], but with extra logging. */
@Suppress("UNCHECKED_CAST")
internal fun ComponentRegistry.key(
data: Any,
options: Options,
logger: Logger?,
tag: String,
): String? {
var hasKeyerForType = false
keyers.forEachIndices { (keyer, type) ->
if (type.isInstance(data)) {
hasKeyerForType = true
(keyer as Keyer<Any>).key(data, options)?.let { return it }
}
}
if (!hasKeyerForType) {
logger?.log(tag, Logger.Level.Warn) {
"No keyer is registered for data with type '${data::class.simpleName}'. " +
"Register Keyer<${data::class.simpleName}> in the component registry to " +
"cache the output image in the memory cache."
}
}
return null
}

internal fun ComponentRegistry.Builder.addFirst(
pair: Pair<Fetcher.Factory<*>, KClass<*>>?
) = apply { if (pair != null) lazyFetcherFactories.add(0) { listOf(pair) } }
Expand Down

0 comments on commit 5ae6212

Please sign in to comment.