metatext-app-ios-iphone-ipad/ViewModels/Sources/ViewModels/Extensions/Publisher+Extensions.swift

22 lines
612 B
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-09-05 04:31:43 +02:00
import Foundation
extension Publisher {
2020-08-02 09:02:03 +02:00
func assignErrorsToAlertItem<Root: AnyObject>(
to keyPath: ReferenceWritableKeyPath<Root, AlertItem?>,
on object: Root) -> AnyPublisher<Output, Never> {
2020-08-07 03:41:59 +02:00
self.catch { [weak object] error -> Empty<Output, Never> in
2020-09-09 03:02:55 +02:00
if let object = object {
DispatchQueue.main.async {
object[keyPath: keyPath] = AlertItem(error: error)
}
}
2020-08-07 03:41:59 +02:00
return Empty()
}
.eraseToAnyPublisher()
}
}