2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
extension Publisher {
|
2020-08-02 07:02:03 +00:00
|
|
|
func assignErrorsToAlertItem<Root: AnyObject>(
|
2020-07-29 23:50:30 +00:00
|
|
|
to keyPath: ReferenceWritableKeyPath<Root, AlertItem?>,
|
|
|
|
on object: Root) -> AnyPublisher<Output, Never> {
|
2020-08-07 01:41:59 +00:00
|
|
|
self.catch { [weak object] error -> Empty<Output, Never> in
|
2020-07-29 23:50:30 +00:00
|
|
|
DispatchQueue.main.async {
|
2020-08-02 07:02:03 +00:00
|
|
|
object?[keyPath: keyPath] = AlertItem(error: error)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 01:41:59 +00:00
|
|
|
return Empty()
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-07 21:57:18 +00:00
|
|
|
|
|
|
|
func continuingIfWeakReferenceIsStillAlive<T: AnyObject>(to object: T) -> AnyPublisher<(Output, T), Error> {
|
|
|
|
tryMap { [weak object] in
|
|
|
|
guard let object = object else { throw WeakReferenceError.deallocated }
|
|
|
|
|
|
|
|
return ($0, object)
|
|
|
|
}
|
|
|
|
.tryCatch { error -> Empty<(Output, T), Never> in
|
|
|
|
if case WeakReferenceError.deallocated = error {
|
|
|
|
return Empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum WeakReferenceError: Error {
|
|
|
|
case deallocated
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|