Skip to content

Commit

Permalink
Cleanup TaskLoadImage
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Apr 20, 2024
1 parent 1422578 commit a1a640b
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions Sources/Nuke/Tasks/TaskLoadImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {

private func didFinishDecoding(with response: ImageResponse?) {
if let response {
didGenerateResponse(response, isCompleted: true, isFromDiskCache: true)
didReceiveResponse(response, isCompleted: true)
} else {
fetchImage()
}
Expand All @@ -59,7 +59,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
}
} else {
dependency = pipeline.makeTaskFetchDecodedImage(for: request).subscribe(self) { [weak self] in
self?.didGenerateResponse($0, isCompleted: $1)
self?.didReceiveResponse($0, isCompleted: $1)
}
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {

switch result {
case .success(let response):
didGenerateResponse(response, isCompleted: context.isCompleted, isFromDiskCache: false)
didReceiveResponse(response, isCompleted: context.isCompleted)
case .failure(let error):
if context.isCompleted {
self.send(error: .processingFailed(processor: processor, context: context, error: error))
Expand All @@ -110,9 +110,9 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {

// MARK: Decompression

private func didGenerateResponse(_ response: ImageResponse, isCompleted: Bool, isFromDiskCache: Bool = false) {
private func didReceiveResponse(_ response: ImageResponse, isCompleted: Bool) {
guard isDecompressionNeeded(for: response) else {
storeImageInCaches(response, isFromDiskCache: isFromDiskCache)
storeImageInCaches(response)
send(value: response, isCompleted: isCompleted)
return
}
Expand All @@ -133,7 +133,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
}

self.pipeline.queue.async {
self.storeImageInCaches(response, isFromDiskCache: isFromDiskCache)
self.storeImageInCaches(response)
self.send(value: response, isCompleted: isCompleted)
}
}
Expand All @@ -148,23 +148,21 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {

// MARK: Caching

private func storeImageInCaches(_ response: ImageResponse, isFromDiskCache: Bool) {
private func storeImageInCaches(_ response: ImageResponse) {
guard !isEphemeral else {
return // Only store for direct requests
}
// Memory cache (ImageCaching)
pipeline.cache[request] = response.container
// Disk cache (DataCaching)
if !isFromDiskCache {
storeImageInDataCache(response)
}
storeImageInDataCache(response)
}

private func storeImageInDataCache(_ response: ImageResponse) {
guard !response.container.isPreview else {
return
}
guard let dataCache = pipeline.delegate.dataCache(for: request, pipeline: pipeline), shouldStoreImageInDiskCache() else {
guard !response.container.isPreview,
response.cacheType != .disk,
let dataCache = pipeline.delegate.dataCache(for: request, pipeline: pipeline),
shouldStoreImageInDataCache() else {
return
}
let context = ImageEncodingContext(request: request, image: response.image, urlResponse: response.urlResponse)
Expand All @@ -187,7 +185,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
}
}

private func shouldStoreImageInDiskCache() -> Bool {
private func shouldStoreImageInDataCache() -> Bool {
guard !(request.url?.isLocalResource ?? false) else {
return false
}
Expand Down

0 comments on commit a1a640b

Please sign in to comment.