Publisher
public extension Publisher
-
Subscribes to a publisher with a closure that receives a
Result
value.Discussion: The
sinkToResult(_:)
function is an extension onPublisher
that allows subscribing to a publisher with a closure that receives aResult
value. It handles the received completion and value events and transforms them into aResult
object for easier result-based handling.The
result
closure is called with aResult
value, representing either a successful value or a failure. For successful values, the closure is called with.success(value)
. For failures, the closure is called with.failure(error)
.The
sinkToResult(_:)
function is useful when you want to handle the output of a publisher withResult
objects, making it easier to handle both success and failure cases in a unified way.Declaration
Swift
func sinkToResult(_ result: @escaping (Result<Output, Failure>) -> Void) -> AnyCancellable
Parameters
result
A closure to be called with the received
Result
value.Return Value
An
AnyCancellable
instance representing the subscription. -
Subscribes to a publisher with a closure that receives a
Loadable
value.Discussion: The
sinkToLoadable(_:)
function is an extension onPublisher
that allows subscribing to a Publisher with a closure that receives aLoadable
value. It handles the received completion and value events and transforms them into a Loadable object for representing the loading state of the value. The completion closure is called with aLoadable
value, which can have one of the following states:.notRequested
: The value has not been requested yet..isLoading
: The value is currently being loaded, along with the last loaded value and a CancelBag for cancellation..loaded
: The value has been successfully loaded..failed
: The loading process has failed with aNetworkError
.
Declaration
Swift
func sinkToLoadable(_ completion: @escaping (Loadable<Output>) -> Void) -> AnyCancellable
Parameters
completion
A closure to be called with the received
Loadable
value.Return Value
An
AnyCancellable
instance representing the subscription.