swift - Transform Reactive Cocoa SignalProducer into enum -
i'm trying transform api signalproducers enum loading, failed , loaded states. enum is:
enum dataloadstate<datatype>{ case loading case failed case loaded(datatype) }
to transform signalproducer noerror producer , start loading state:
extension signalproducertype{ func materializetoloadstate() -> signalproducer<dataloadstate<value>,noerror>{ let producer = self .map(dataloadstate.loaded) .startwithvalue(dataloadstate.loading) return producer.ignoreerrors(replacementvalue:dataloadstate<value>.failed) } } extension signalproducertype { func startwithvalue(value:value)->signalproducer<value,error>{ return signalproducer(value:value).concat(self.producer) } public func ignoreerrors(replacementvalue replacementvalue: self.value? = nil) -> signalproducer<self.value, noerror> { return self.flatmaperror { error in return replacementvalue.map(signalproducer.init) ?? .empty } } }
it works don't want ignore errors, instead want include in failed case:
enum dataloadstate<datatype>{ case loading case failed(apierror?) case loaded(datatype) }
any idea on how can use flatmaperror (like in ignoreerror) errors fired dataloadstate.failed(error) instead of being ignored? when error isn't display error nil. possible?
ok, fixed with:
producer.flatmaperror { error in signalproducer(value:.failed(error as? apierror)) }
Comments
Post a Comment