1
1
mirror of https://github.com/kean/Nuke.git synced 2024-11-28 12:04:01 +03:00

Rename ImagePipelineDelegate to ImagePipeline.Delegate

This commit is contained in:
kean 2024-08-17 17:37:39 -04:00
parent c56dbae0b6
commit 45c7fd6855
5 changed files with 60 additions and 54 deletions

View File

@ -8,69 +8,71 @@ import Foundation
///
/// - important: The delegate methods are performed on the pipeline queue in the
/// background.
public protocol ImagePipelineDelegate: AnyObject, Sendable {
// MARK: Configuration
extension ImagePipeline {
public protocol Delegate: AnyObject, Sendable {
// MARK: Configuration
/// Returns data loader for the given request.
func dataLoader(for request: ImageRequest, pipeline: ImagePipeline) -> any DataLoading
/// Returns data loader for the given request.
func dataLoader(for request: ImageRequest, pipeline: ImagePipeline) -> any DataLoading
/// Returns image decoder for the given context.
func imageDecoder(for context: ImageDecodingContext, pipeline: ImagePipeline) -> (any ImageDecoding)?
/// Returns image decoder for the given context.
func imageDecoder(for context: ImageDecodingContext, pipeline: ImagePipeline) -> (any ImageDecoding)?
/// Returns image encoder for the given context.
func imageEncoder(for context: ImageEncodingContext, pipeline: ImagePipeline) -> any ImageEncoding
/// Returns image encoder for the given context.
func imageEncoder(for context: ImageEncodingContext, pipeline: ImagePipeline) -> any ImageEncoding
// MARK: Caching
// MARK: Caching
/// Returns in-memory image cache for the given request. Return `nil` to prevent cache reads and writes.
func imageCache(for request: ImageRequest, pipeline: ImagePipeline) -> (any ImageCaching)?
/// Returns in-memory image cache for the given request. Return `nil` to prevent cache reads and writes.
func imageCache(for request: ImageRequest, pipeline: ImagePipeline) -> (any ImageCaching)?
/// Returns disk cache for the given request. Return `nil` to prevent cache
/// reads and writes.
func dataCache(for request: ImageRequest, pipeline: ImagePipeline) -> (any DataCaching)?
/// Returns disk cache for the given request. Return `nil` to prevent cache
/// reads and writes.
func dataCache(for request: ImageRequest, pipeline: ImagePipeline) -> (any DataCaching)?
/// Returns a cache key identifying the image produced for the given request
/// (including image processors). The key is used for both in-memory and
/// on-disk caches.
///
/// Return `nil` to use a default key.
func cacheKey(for request: ImageRequest, pipeline: ImagePipeline) -> String?
/// Returns a cache key identifying the image produced for the given request
/// (including image processors). The key is used for both in-memory and
/// on-disk caches.
///
/// Return `nil` to use a default key.
func cacheKey(for request: ImageRequest, pipeline: ImagePipeline) -> String?
/// Gets called when the pipeline is about to save data for the given request.
/// The implementation must call the completion closure passing `non-nil` data
/// to enable caching or `nil` to prevent it.
///
/// This method calls only if the request parameters and data caching policy
/// of the pipeline already allow caching.
///
/// - parameters:
/// - data: Either the original data or the encoded image in case of storing
/// a processed or re-encoded image.
/// - image: Non-nil in case storing an encoded image.
/// - request: The request for which image is being stored.
/// - completion: The implementation must call the completion closure
/// passing `non-nil` data to enable caching or `nil` to prevent it. You can
/// safely call it synchronously. The callback gets called on the background
/// thread.
func willCache(data: Data, image: ImageContainer?, for request: ImageRequest, pipeline: ImagePipeline, completion: @escaping (Data?) -> Void)
/// Gets called when the pipeline is about to save data for the given request.
/// The implementation must call the completion closure passing `non-nil` data
/// to enable caching or `nil` to prevent it.
///
/// This method calls only if the request parameters and data caching policy
/// of the pipeline already allow caching.
///
/// - parameters:
/// - data: Either the original data or the encoded image in case of storing
/// a processed or re-encoded image.
/// - image: Non-nil in case storing an encoded image.
/// - request: The request for which image is being stored.
/// - completion: The implementation must call the completion closure
/// passing `non-nil` data to enable caching or `nil` to prevent it. You can
/// safely call it synchronously. The callback gets called on the background
/// thread.
func willCache(data: Data, image: ImageContainer?, for request: ImageRequest, pipeline: ImagePipeline, completion: @escaping (Data?) -> Void)
// MARK: Decompression
// MARK: Decompression
func shouldDecompress(response: ImageResponse, for request: ImageRequest, pipeline: ImagePipeline) -> Bool
func shouldDecompress(response: ImageResponse, for request: ImageRequest, pipeline: ImagePipeline) -> Bool
func decompress(response: ImageResponse, request: ImageRequest, pipeline: ImagePipeline) -> ImageResponse
func decompress(response: ImageResponse, request: ImageRequest, pipeline: ImagePipeline) -> ImageResponse
// MARK: ImageTask
// MARK: ImageTask
/// Gets called when the task is created. Unlike other methods, it is called
/// immediately on the caller's queue.
func imageTaskCreated(_ task: ImageTask, pipeline: ImagePipeline)
/// Gets called when the task is created. Unlike other methods, it is called
/// immediately on the caller's queue.
func imageTaskCreated(_ task: ImageTask, pipeline: ImagePipeline)
/// Gets called when the task receives an event.
func imageTask(_ task: ImageTask, didReceiveEvent event: ImageTask.Event, pipeline: ImagePipeline)
/// Gets called when the task receives an event.
func imageTask(_ task: ImageTask, didReceiveEvent event: ImageTask.Event, pipeline: ImagePipeline)
}
}
extension ImagePipelineDelegate {
extension ImagePipeline.Delegate {
public func imageCache(for request: ImageRequest, pipeline: ImagePipeline) -> (any ImageCaching)? {
pipeline.configuration.imageCache
}
@ -114,4 +116,8 @@ extension ImagePipelineDelegate {
public func imageTask(_ task: ImageTask, didReceiveEvent event: ImageTask.Event, pipeline: ImagePipeline) {}
}
final class ImagePipelineDefaultDelegate: ImagePipelineDelegate {}
final class ImagePipelineDefaultDelegate: ImagePipeline.Delegate {}
// Deprecated in Nuke 13.0
@available(*, deprecated, renamed: "ImagePipeline.Delegate", message: "")
public typealias ImagePipelineDelegate = ImagePipeline.Delegate

View File

@ -30,7 +30,7 @@ public final class ImagePipeline {
/// Provides access to the underlying caching subsystems.
public nonisolated var cache: ImagePipeline.Cache { .init(pipeline: self) }
let delegate: any ImagePipelineDelegate
let delegate: any ImagePipeline.Delegate
private var tasks = [ImageTask: TaskSubscription]()
@ -57,7 +57,7 @@ public final class ImagePipeline {
/// - parameters:
/// - configuration: The pipeline configuration.
/// - delegate: Provides more ways to customize the pipeline behavior on per-request basis.
public nonisolated init(configuration: Configuration = Configuration(), delegate: (any ImagePipelineDelegate)? = nil) {
public nonisolated init(configuration: Configuration = Configuration(), delegate: (any ImagePipeline.Delegate)? = nil) {
self.configuration = configuration
self.rateLimiter = configuration.isRateLimiterEnabled ? RateLimiter() : nil
self.delegate = delegate ?? ImagePipelineDefaultDelegate()
@ -86,7 +86,7 @@ public final class ImagePipeline {
/// - parameters:
/// - configuration: The pipeline configuration.
/// - delegate: Provides more ways to customize the pipeline behavior on per-request basis.
public nonisolated convenience init(delegate: (any ImagePipelineDelegate)? = nil, _ configure: (inout ImagePipeline.Configuration) -> Void) {
public nonisolated convenience init(delegate: (any ImagePipeline.Delegate)? = nil, _ configure: (inout ImagePipeline.Configuration) -> Void) {
var configuration = ImagePipeline.Configuration()
configure(&configuration)
self.init(configuration: configuration, delegate: delegate)

View File

@ -5,7 +5,7 @@
import XCTest
@testable import Nuke
final class ImagePipelineObserver: ImagePipelineDelegate, @unchecked Sendable {
final class ImagePipelineObserver: ImagePipeline.Delegate, @unchecked Sendable {
var createdTaskCount = 0
var cancelledTaskCount = 0
var completedTaskCount = 0

View File

@ -117,7 +117,7 @@ private func checkAccessCachedImages07() {
_ = pipeline.cache.makeDataCacheKey(for: request)
}
private final class CheckAccessCachedImages08: ImagePipelineDelegate {
private final class CheckAccessCachedImages08: ImagePipeline.Delegate {
func cacheKey(for request: ImageRequest, pipeline: ImagePipeline) -> String? {
request.userInfo["imageId"] as? String
}

View File

@ -89,7 +89,7 @@ class ImagePipelineDelegateTests: XCTestCase {
}
}
private final class MockImagePipelineDelegate: ImagePipelineDelegate, @unchecked Sendable {
private final class MockImagePipelineDelegate: ImagePipeline.Delegate, @unchecked Sendable {
var isCacheEnabled = true
func cacheKey(for request: ImageRequest, pipeline: ImagePipeline) -> String? {