mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-04-26 02:28:36 +02:00
Combine examples
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Publisher.Extension.swift
|
||||
// GitHubSearchWithSwiftUI
|
||||
//
|
||||
// Created by John Holdsworth on 09/07/2019.
|
||||
// Copyright © 2019 jp.marty-suzuki. All rights reserved.
|
||||
//
|
||||
|
||||
import Combine
|
||||
|
||||
extension Publisher {
|
||||
|
||||
/// - seealso: https://twitter.com/peres/status/1136132104020881408
|
||||
func flatMapLatest<T: Publisher>(_ transform: @escaping (Self.Output) -> T) -> Publishers.SwitchToLatest<T, Publishers.Map<Self, T>> where T.Failure == Self.Failure {
|
||||
map(transform).switchToLatest()
|
||||
}
|
||||
}
|
||||
|
||||
extension Publisher {
|
||||
|
||||
static func empty() -> AnyPublisher<Output, Failure> {
|
||||
return Publishers.Empty()
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
static func just(_ output: Output) -> AnyPublisher<Output, Failure> {
|
||||
return Just(output)
|
||||
.catch { _ in AnyPublisher<Output, Failure>.empty() }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
static func fail(_ error: Failure) -> AnyPublisher<Output, Failure> {
|
||||
return Publishers.Fail(error: error)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user