From f13e85f37dd6c3e99b640274cb769eefa533ca7d Mon Sep 17 00:00:00 2001 From: Amol Prabhu Date: Thu, 8 Aug 2024 16:06:34 -0400 Subject: [PATCH] revert --- Sources/Nuke/Pipeline/ImagePipeline.swift | 16 ++++++++-------- Sources/Nuke/Tasks/TaskFetchOriginalData.swift | 10 +++++----- Sources/Nuke/Tasks/TaskFetchOriginalImage.swift | 2 +- Sources/Nuke/Tasks/TaskLoadImage.swift | 15 +++++++-------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/Sources/Nuke/Pipeline/ImagePipeline.swift b/Sources/Nuke/Pipeline/ImagePipeline.swift index 1072ab2b2..1b8c88029 100644 --- a/Sources/Nuke/Pipeline/ImagePipeline.swift +++ b/Sources/Nuke/Pipeline/ImagePipeline.swift @@ -29,7 +29,7 @@ public final class ImagePipeline: @unchecked Sendable { /// Provides access to the underlying caching subsystems. public var cache: ImagePipeline.Cache { .init(pipeline: self) } - weak var delegate: (any ImagePipelineDelegate)? + let delegate: any ImagePipelineDelegate private var tasks = [ImageTask: TaskSubscription]() @@ -289,7 +289,7 @@ public final class ImagePipeline: @unchecked Sendable { let task = ImageTask(taskId: nextTaskId, request: request, isDataTask: isDataTask, pipeline: self, onEvent: onEvent) // Important to call it before `imageTaskStartCalled` if !isDataTask { - delegate?.imageTaskCreated(task, pipeline: self) + delegate.imageTaskCreated(task, pipeline: self) } task._task = Task { try await withUnsafeThrowingContinuation { continuation in @@ -317,7 +317,7 @@ public final class ImagePipeline: @unchecked Sendable { tasks[task] = worker.subscribe(priority: task.priority.taskPriority, subscriber: task) { [weak task] in task?._process($0) } - delegate?.imageTaskDidStart(task, pipeline: self) + delegate.imageTaskDidStart(task, pipeline: self) onTaskStarted?(task) } @@ -346,16 +346,16 @@ public final class ImagePipeline: @unchecked Sendable { } if !isDataTask { - delegate?.imageTask(task, didReceiveEvent: event, pipeline: self) + delegate.imageTask(task, didReceiveEvent: event, pipeline: self) switch event { case .progress(let progress): - delegate?.imageTask(task, didUpdateProgress: progress, pipeline: self) + delegate.imageTask(task, didUpdateProgress: progress, pipeline: self) case .preview(let response): - delegate?.imageTask(task, didReceivePreview: response, pipeline: self) + delegate.imageTask(task, didReceivePreview: response, pipeline: self) case .cancelled: - delegate?.imageTaskDidCancel(task, pipeline: self) + delegate.imageTaskDidCancel(task, pipeline: self) case .finished(let result): - delegate?.imageTask(task, didCompleteWithResult: result, pipeline: self) + delegate.imageTask(task, didCompleteWithResult: result, pipeline: self) } } } diff --git a/Sources/Nuke/Tasks/TaskFetchOriginalData.swift b/Sources/Nuke/Tasks/TaskFetchOriginalData.swift index b1f60f97e..3e21d66df 100644 --- a/Sources/Nuke/Tasks/TaskFetchOriginalData.swift +++ b/Sources/Nuke/Tasks/TaskFetchOriginalData.swift @@ -80,8 +80,8 @@ final class TaskFetchOriginalData: AsyncPipelineTask<(Data, URLResponse?)> { signpost(self, "LoadImageData", .begin, "URL: \(urlRequest.url?.absoluteString ?? ""), resumable data: \(Formatter.bytes(resumableData?.data.count ?? 0))") - let dataLoader = pipeline.delegate?.dataLoader(for: request, pipeline: pipeline) - let dataTask = dataLoader?.loadData(with: urlRequest, didReceiveData: { [weak self] data, response in + let dataLoader = pipeline.delegate.dataLoader(for: request, pipeline: pipeline) + let dataTask = dataLoader.loadData(with: urlRequest, didReceiveData: { [weak self] data, response in guard let self else { return } self.pipeline.queue.async { self.dataTask(didReceiveData: data, response: response) @@ -99,7 +99,7 @@ final class TaskFetchOriginalData: AsyncPipelineTask<(Data, URLResponse?)> { guard let self else { return } signpost(self, "LoadImageData", .end, "Cancelled") - dataTask?.cancel() + dataTask.cancel() finish() // Finish the operation! self.tryToSaveResumableData() @@ -170,11 +170,11 @@ final class TaskFetchOriginalData: AsyncPipelineTask<(Data, URLResponse?)> { extension AsyncPipelineTask where Value == (Data, URLResponse?) { func storeDataInCacheIfNeeded(_ data: Data) { let request = makeSanitizedRequest() - guard let dataCache = pipeline.delegate?.dataCache(for: request, pipeline: pipeline), shouldStoreDataInDiskCache() else { + guard let dataCache = pipeline.delegate.dataCache(for: request, pipeline: pipeline), shouldStoreDataInDiskCache() else { return } let key = pipeline.cache.makeDataCacheKey(for: request) - pipeline.delegate?.willCache(data: data, image: nil, for: request, pipeline: pipeline) { + pipeline.delegate.willCache(data: data, image: nil, for: request, pipeline: pipeline) { guard let data = $0 else { return } // Important! Storing directly ignoring `ImageRequest.Options`. dataCache.storeData(data, for: key) diff --git a/Sources/Nuke/Tasks/TaskFetchOriginalImage.swift b/Sources/Nuke/Tasks/TaskFetchOriginalImage.swift index 062e91b36..385a23508 100644 --- a/Sources/Nuke/Tasks/TaskFetchOriginalImage.swift +++ b/Sources/Nuke/Tasks/TaskFetchOriginalImage.swift @@ -62,7 +62,7 @@ final class TaskFetchOriginalImage: AsyncPipelineTask { if let decoder { return decoder } - let decoder = pipeline.delegate?.imageDecoder(for: context, pipeline: pipeline) + let decoder = pipeline.delegate.imageDecoder(for: context, pipeline: pipeline) self.decoder = decoder return decoder } diff --git a/Sources/Nuke/Tasks/TaskLoadImage.swift b/Sources/Nuke/Tasks/TaskLoadImage.swift index 6d4a38ed4..3e0f9b0be 100644 --- a/Sources/Nuke/Tasks/TaskLoadImage.swift +++ b/Sources/Nuke/Tasks/TaskLoadImage.swift @@ -27,7 +27,7 @@ final class TaskLoadImage: AsyncPipelineTask { private func decodeCachedData(_ data: Data) { let context = ImageDecodingContext(request: request, data: data, cacheType: .disk) - guard let decoder = pipeline.delegate?.imageDecoder(for: context, pipeline: pipeline) else { + guard let decoder = pipeline.delegate.imageDecoder(for: context, pipeline: pipeline) else { return didFinishDecoding(with: nil) } decode(context, decoder: decoder) { [weak self] in @@ -116,10 +116,9 @@ final class TaskLoadImage: AsyncPipelineTask { operation = pipeline.configuration.imageDecompressingQueue.add { [weak self] in guard let self else { return } let response = signpost(isCompleted ? "DecompressImage" : "DecompressProgressiveImage") { - self.pipeline.delegate?.decompress(response: response, request: self.request, pipeline: self.pipeline) + self.pipeline.delegate.decompress(response: response, request: self.request, pipeline: self.pipeline) } - guard let response else { return } self.pipeline.queue.async { [weak self] in guard let self else { return } self.operation = nil @@ -132,7 +131,7 @@ final class TaskLoadImage: AsyncPipelineTask { ImageDecompression.isDecompressionNeeded(for: response) && !request.options.contains(.skipDecompression) && hasDirectSubscribers && - pipeline.delegate?.shouldDecompress(response: response, for: request, pipeline: pipeline) ?? false + pipeline.delegate.shouldDecompress(response: response, for: request, pipeline: pipeline) } private func didReceiveDecompressedImage(_ response: ImageResponse, isCompleted: Bool) { @@ -153,19 +152,19 @@ final class TaskLoadImage: AsyncPipelineTask { } private func storeImageInDataCache(_ response: ImageResponse) { - guard let dataCache = pipeline.delegate?.dataCache(for: request, pipeline: pipeline) else { + guard let dataCache = pipeline.delegate.dataCache(for: request, pipeline: pipeline) else { return } let context = ImageEncodingContext(request: request, image: response.image, urlResponse: response.urlResponse) - let encoder = pipeline.delegate?.imageEncoder(for: context, pipeline: pipeline) + let encoder = pipeline.delegate.imageEncoder(for: context, pipeline: pipeline) let key = pipeline.cache.makeDataCacheKey(for: request) pipeline.configuration.imageEncodingQueue.addOperation { [weak pipeline, request] in guard let pipeline else { return } let encodedData = signpost("EncodeImage") { - encoder?.encode(response.container, context: context) + encoder.encode(response.container, context: context) } guard let data = encodedData, !data.isEmpty else { return } - pipeline.delegate?.willCache(data: data, image: response.container, for: request, pipeline: pipeline) { + pipeline.delegate.willCache(data: data, image: response.container, for: request, pipeline: pipeline) { guard let data = $0, !data.isEmpty else { return } // Important! Storing directly ignoring `ImageRequest.Options`. dataCache.storeData(data, for: key) // This is instant, writes are async