-
Notifications
You must be signed in to change notification settings - Fork 114
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
3 changed files
with
141 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Deferred.swift | ||
// GlovoCourier | ||
// | ||
// Created by Ibrahim Koteish on 01/12/2019. | ||
// Copyright © 2019 Glovo. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A signal that awaits subscription before running the supplied closure | ||
/// to create a signal for the new subscriber. | ||
public struct Deferred<DeferredSignal: SignalProtocol>: SignalProtocol { | ||
|
||
/// The kind of values published by this signal. | ||
public typealias Element = DeferredSignal.Element | ||
|
||
/// The kind of errors this signal might publish. | ||
/// | ||
/// Use `Never` if this `signal` does not publish errors. | ||
public typealias Error = DeferredSignal.Error | ||
|
||
/// The closure to execute when it receives a subscription. | ||
/// | ||
/// The signal returned by this closure immediately | ||
/// receives the incoming subscription. | ||
public let signalFactory: () -> DeferredSignal | ||
|
||
/// Creates a deferred signal. | ||
/// | ||
/// - Parameter signalFactory: The closure to execute | ||
/// when calling `observe(with:)`. | ||
public init(signalFactory: @escaping () -> DeferredSignal) { | ||
self.signalFactory = signalFactory | ||
} | ||
|
||
/// This function is called to attach the specified `Observer` | ||
/// to this `Signal` by `observe(with:)` | ||
/// | ||
/// - Parameters: | ||
/// - observer: The observer to attach to this `Signal`. | ||
/// once attached it can begin to receive values. | ||
public func observe( | ||
with observer: @escaping (Signal<DeferredSignal.Element, DeferredSignal.Error>.Event) -> Void) | ||
-> Disposable | ||
{ | ||
let deferredSignal = signalFactory() | ||
return deferredSignal.observe(with: observer) | ||
} | ||
} |
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,63 @@ | ||
// | ||
// Empty.swift | ||
// ReactiveKit-iOS | ||
// | ||
// Created by Ibrahim Koteish on 06/03/2020. | ||
// Copyright © 2020 DeclarativeHub. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A signal that never publishes any values, and optionally finishes immediately. | ||
/// | ||
/// You can create a ”Never” signal — one which never sends values and never | ||
/// finishes or fails — with the initializer `Empty(completeImmediately: false)`. | ||
public struct Empty<Element, Error: Swift.Error>: SignalProtocol, Equatable { | ||
|
||
/// The kind of values published by this signal. | ||
public typealias Element = Element | ||
|
||
/// The kind of errors this signal might publish. | ||
/// | ||
/// Use `Never` if this `signal` does not publish errors. | ||
public typealias Error = Error | ||
|
||
/// Creates an empty signal. | ||
/// | ||
/// - Parameter completeImmediately: A Boolean value that indicates whether | ||
/// the signal should immediately finish. | ||
public init(completeImmediately: Bool = true) { | ||
self.completeImmediately = completeImmediately | ||
} | ||
|
||
/// Creates an empty signal with the given completion behavior and output and | ||
/// failure types. | ||
/// | ||
/// Use this initializer to connect the empty signal to observers or other | ||
/// signals that have specific output and failure types. | ||
/// - Parameters: | ||
/// - completeImmediately: A Boolean value that indicates whether the signal | ||
/// should immediately finish. | ||
/// - outputType: The output type exposed by this signal. | ||
/// - failureType: The failure type exposed by this signal. | ||
public init(completeImmediately: Bool = true, | ||
outputType: Element.Type, | ||
failureType: Error.Type) { | ||
self.init(completeImmediately: completeImmediately) | ||
} | ||
|
||
/// A Boolean value that indicates whether the signal immediately sends | ||
/// a completion. | ||
/// | ||
/// If `true`, the signal finishes immediately after sending an event | ||
/// to the observer. If `false`, it never completes. | ||
public let completeImmediately: Bool | ||
|
||
public func observe(with observer: @escaping (Signal<Element, Error>.Event) -> Void) -> Disposable { | ||
|
||
if completeImmediately { | ||
return Signal<Element, Error>.completed().observe(with: observer) | ||
} | ||
return Signal<Element, Error>.never().observe(with: observer) | ||
} | ||
} |