-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
93 changed files
with
1,681 additions
and
1,530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// | ||
// Copyright © 2020 Jesús Alfredo Hernández Alarcón. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import EssentialFeed | ||
import Foundation | ||
|
||
public extension HTTPClient { | ||
typealias Publisher = AnyPublisher<(Data, HTTPURLResponse), Error> | ||
|
||
func getPublisher(from url: URL) -> Publisher { | ||
var task: HTTPClientTask? | ||
|
||
return Deferred { | ||
Future { completion in | ||
task = self.get(from: url, completion: completion) | ||
} | ||
} | ||
.handleEvents(receiveCancel: { task?.cancel() }) | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
public extension FeedImageDataLoader { | ||
typealias Publisher = AnyPublisher<Data, Swift.Error> | ||
|
||
func loadImageDataPublisher(from url: URL) -> Publisher { | ||
var task: FeedImageDataLoaderTask? | ||
|
||
return Deferred { | ||
Future { completion in | ||
task = loadImageData(from: url, completion: completion) | ||
} | ||
} | ||
.handleEvents(receiveCancel: { task?.cancel() }) | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
public extension Publisher where Output == Data { | ||
func catching(to cache: FeedImageDataCache, using url: URL) -> AnyPublisher<Output, Failure> { | ||
handleEvents(receiveOutput: { data in | ||
cache.saveIgnoringResult(data: data, for: url) | ||
}).eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
public extension FeedImageDataCache { | ||
func saveIgnoringResult(data: Data, for url: URL) { | ||
save(data, for: url, completion: { _ in }) | ||
} | ||
} | ||
|
||
public extension LocalFeedLoader { | ||
typealias Publisher = AnyPublisher<[FeedImage], Swift.Error> | ||
|
||
func loadPublisher() -> Publisher { | ||
Deferred { Future(self.load) }.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
public extension Publisher where Output == [FeedImage] { | ||
func catching(to cache: FeedCache) -> AnyPublisher<Output, Failure> { | ||
handleEvents(receiveOutput: { feed in | ||
cache.saveIgnoringResult(feed) | ||
}).eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
private extension FeedCache { | ||
func saveIgnoringResult(_ feed: [FeedImage]) { | ||
save(feed) { _ in } | ||
} | ||
} | ||
|
||
public extension Publisher { | ||
func fallback(to fallbackPublisher: @escaping () -> AnyPublisher<Output, Failure>) -> AnyPublisher<Output, Failure> { | ||
self.catch { _ in fallbackPublisher() }.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
extension Publisher { | ||
func dispatchOnMainQueue() -> AnyPublisher<Output, Failure> { | ||
receive(on: DispatchQueue.immediateWhenOnMainQueueScheduler).eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
extension DispatchQueue { | ||
static var immediateWhenOnMainQueueScheduler = ImmediateWhenOnMainQueueScheduler.shared | ||
|
||
struct ImmediateWhenOnMainQueueScheduler: Scheduler { | ||
typealias SchedulerTimeType = DispatchQueue.SchedulerTimeType | ||
typealias SchedulerOptions = DispatchQueue.SchedulerOptions | ||
var now: DispatchQueue.SchedulerTimeType { | ||
DispatchQueue.main.now | ||
} | ||
|
||
var minimumTolerance: DispatchQueue.SchedulerTimeType.Stride { | ||
DispatchQueue.main.minimumTolerance | ||
} | ||
|
||
static let shared = Self() | ||
|
||
private static let key = DispatchSpecificKey<UInt8>() | ||
private static let value = UInt8.max | ||
|
||
private init() { | ||
DispatchQueue.main.setSpecific(key: Self.key, value: Self.value) | ||
} | ||
|
||
private func isMainQueue() -> Bool { | ||
DispatchQueue.getSpecific(key: Self.key) == Self.value | ||
} | ||
|
||
func schedule(options: DispatchQueue.SchedulerOptions?, _ action: @escaping () -> Void) { | ||
guard isMainQueue() else { | ||
return DispatchQueue.main.schedule(options: options, action) | ||
} | ||
action() | ||
} | ||
|
||
func schedule( | ||
after date: DispatchQueue.SchedulerTimeType, | ||
tolerance: DispatchQueue.SchedulerTimeType.Stride, | ||
options: DispatchQueue.SchedulerOptions?, | ||
_ action: @escaping () -> Void | ||
) { | ||
DispatchQueue.main.schedule(after: date, tolerance: tolerance, options: options, action) | ||
} | ||
|
||
func schedule( | ||
after date: DispatchQueue.SchedulerTimeType, | ||
interval: DispatchQueue.SchedulerTimeType.Stride, | ||
tolerance: DispatchQueue.SchedulerTimeType.Stride, | ||
options: DispatchQueue.SchedulerOptions?, | ||
_ action: @escaping () -> Void | ||
) -> Cancellable { | ||
DispatchQueue.main.schedule(after: date, interval: interval, tolerance: tolerance, options: options, action) | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
EssentialApp/EssentialApp/FeedImageDataLoaderCacheDecorator.swift
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
EssentialApp/EssentialApp/FeedImageDataLoaderPresentationAdapter.swift
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
EssentialApp/EssentialApp/FeedImageDataLoaderWithFallbackComposite.swift
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
EssentialApp/EssentialApp/FeedLoaderWithFallbackComposite.swift
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.