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

Reimplement data loading with closure

This commit is contained in:
kean 2024-08-17 20:05:09 -04:00
parent a43d235632
commit 8befa467e1
2 changed files with 23 additions and 37 deletions

View File

@ -4,6 +4,8 @@
import Foundation
// TODO: (nuke13) stop inhereting from AsyncPipelineTask
/// Fetches data using the publisher provided with the request.
/// Unlike `TaskFetchOriginalImageData`, there is no resumable data involved.
final class TaskFetchWithClosure: AsyncPipelineTask<(Data, URLResponse?)> {
@ -26,6 +28,8 @@ final class TaskFetchWithClosure: AsyncPipelineTask<(Data, URLResponse?)> {
}
}
// TODO: (nuke13) implement in TaskFetchOriginalData using the same protocol
// This methods gets called inside data loading operation (Operation).
private func loadData(finish: @escaping () -> Void) {
guard !isDisposed else {
@ -37,38 +41,22 @@ final class TaskFetchWithClosure: AsyncPipelineTask<(Data, URLResponse?)> {
return assertionFailure("This should never happen")
}
// TODO: (nuke13) reimplement
// let cancellable = closure.sink(receiveCompletion: { [weak self] result in
// finish() // Finish the operation!
// guard let self else { return }
// Task { @ImagePipelineActor in
// self.dataTaskDidFinish(result)
// }
// }, receiveValue: { [weak self] data in
// guard let self else { return }
// Task { @ImagePipelineActor in
// self.data.append(data)
// }
// })
//
// onCancelled = {
// finish()
// cancellable.cancel()
// }
let task = Task { @ImagePipelineActor in
do {
let data = try await closure()
guard !data.isEmpty else {
throw ImagePipeline.Error.dataIsEmpty
}
storeDataInCacheIfNeeded(data)
send(value: (data, nil), isCompleted: true)
} catch {
send(error: .dataLoadingFailed(error: error))
}
finish() // Finish the operation!
}
onCancelled = {
finish()
task.cancel()
}
}
//
// private func dataTaskDidFinish(_ result: PublisherCompletion) {
// switch result {
// case .finished:
// guard !data.isEmpty else {
// send(error: .dataIsEmpty)
// return
// }
// storeDataInCacheIfNeeded(data)
// send(value: (data, nil), isCompleted: true)
// case .failure(let error):
// send(error: .dataLoadingFailed(error: error))
// }
// }
}

View File

@ -279,8 +279,7 @@ class ImagePipelineAsyncAwaitTests: XCTestCase, @unchecked Sendable {
// MARK: - ImageRequest with Async/Await
// TODO: (nuke13) reimplemnet
func _testImageRequestWithAsyncAwaitSuccess() async throws {
func testImageRequestWithAsyncAwaitSuccess() async throws {
if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
// GIVEN
let localURL = Test.url(forResource: "fixture", extension: "jpeg")
@ -298,8 +297,7 @@ class ImagePipelineAsyncAwaitTests: XCTestCase, @unchecked Sendable {
}
}
// TODO: (nuke13) reimplemnet
func _testImageRequestWithAsyncAwaitFailure() async throws {
func testImageRequestWithAsyncAwaitFailure() async throws {
if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
// WHEN
let request = ImageRequest(id: "test", data: {