DeclarativeHub / ReactiveKit

A Swift Reactive Programming Kit
MIT License
1.24k stars 114 forks source link

Replace errors with nil? #250

Closed kevinrenskers closed 4 years ago

kevinrenskers commented 4 years ago

How do I turn a Signal<SomeThing, Error> into Signal<SomeThing?, Never>?

kevinrenskers commented 4 years ago

Never mind :)

extension Signal {
  func errorToOptional() -> Signal<Element?, Never> {
    return Signal<Element?, Never> { observer in
      return self.observe { event in
        switch event {
        case .next(let element):
          observer.receive(element)
        case .failed:
          observer.receive(nil)
        case .completed:
          observer.receive(completion: .finished)
        }
      }
    }
  }
}