Open bc-lee opened 13 hours ago
The current intention is that all decoding work is performed inside the Decoder.decode
block exactly once. Any resources can be cleaned up when returning from that function as the ImageSource
is exhausted and decoding is complete. Does that cover your use case?
Is your feature request related to a problem? Please describe. Currently, the
Decoder
interface incoil
lacks a standardized way to handle resource cleanup. When developers implement customDecoder
classes that rely on native libraries (e.g.,libavif
for AVIF decoding), they often need to release resources explicitly when decoding is complete. However, without aclose
ordispose
method, there’s no consistent mechanism to safely handle resource cleanup, which can lead to resource leaks or necessitate complex workarounds.Describe the solution you'd like Adding a
close
ordispose
method to theDecoder
interface would enable customDecoder
implementations to reliably release resources when they’re no longer needed. This method would give developers an easy, built-in way to handle cleanup, allowingcoil
to automatically callclose
on theDecoder
instance when decoding is finished. This solution would align with the Dispose pattern, common in garbage-collected languages, and enhancecoil
's flexibility for those using external or native resources in custom decoders.To ensure minimal impact on existing
Decoder
implementations, a default (empty) implementation ofclose
could be provided, requiring no changes to existing decoders that don’t need explicit cleanup.Why this solution makes sense for
coil
This proposal aligns well with
coil
's goals for the following reasons:Increased Flexibility and Safety: The
close
ordispose
method would allow custom decoders to handle resource management consistently, supporting a broader range of use cases without risk of resource leaks. This change would be particularly beneficial for users working with resource-intensive or native libraries.Minimal Impact on Core Users: By providing a default implementation for
close
, this proposal ensures that typicalcoil
users and existingDecoder
implementations remain unaffected, keeping the API simple and intuitive.Standardized Cleanup Mechanism: A
close
method would create a consistent, reliable way to handle resource release, avoiding the need for developers to implement complex external workarounds like wrappers oruse
blocks. This aligns with Kotlin’s emphasis on developer ergonomics and ease of use.Alignment with Established Patterns: The Dispose pattern is a widely recognized solution for resource management, especially in garbage-collected languages. Adding
close
toDecoder
would bringcoil
in line with best practices for handling external resources.Additional context
While this proposal introduces a slight increase in API complexity, it is optional for most use cases and provides clear benefits to those needing resource management. For further context on the Dispose pattern, see Dispose Pattern in Wikipedia. Adding this method makes
coil
more versatile and adaptable for custom decoder use cases, enhancing its value as a flexible, developer-friendly library.