Skip to content

Instantly share code, notes, and snippets.

@akbsteam
Created September 24, 2017 12:20
Show Gist options
  • Select an option

  • Save akbsteam/103fa9dd301b71fe2db05ed558928aeb to your computer and use it in GitHub Desktop.

Select an option

Save akbsteam/103fa9dd301b71fe2db05ed558928aeb to your computer and use it in GitHub Desktop.
extension Bool {
public func trueSignal(or fail: Fail) -> Signal<Bool, Fail> {
guard self else { return Signal.failed(fail) }
return Signal.just(true)
}
}
extension Optional {
public func valueSignal(or fail: Fail) -> Signal<Wrapped, Fail>
{
switch self {
case .some(let value): return Signal.just(value)
case .none: return Signal.failed(fail)
}
}
}
public func TrySignal<T>(fun: @escaping () throws -> T) -> Signal<T, Fail>
{
return Signal { observer in
do {
let res = try fun()
observer.completed(with: res)
} catch let error as NSError {
observer.failed(error.toFail())
}
return NonDisposable.instance
}
}
extension SignalProtocol where Self.Element == Bool {
public func not() -> ReactiveKit.Signal<Bool, Self.Error> {
return self.map { $0 == false }
}
}
extension SignalProtocol {
/// Emit only first element that passes `include` test.
public func first(include: @escaping (Self.Element) -> Bool) -> ReactiveKit.Signal<Self.Element, Self.Error> {
return self.filter(include).take(first: 1)
}
/// Do side-effect upon various events.
public func onNextOrFailed(_ block: @escaping (() -> Void)) -> Signal<Element, Error>
{
return self.doOn(next: { _ in block() }, failed: { _ in block() })
}
/// 95% of the time, we only use doOn(next: {})
public func onNext(_ block: @escaping ((Element) -> Void)) -> Signal<Element, Error>
{
return self.doOn(next: block)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment