Skip to content

Instantly share code, notes, and snippets.

@JaviSoto
Created April 30, 2016 00:05
Show Gist options
  • Save JaviSoto/cff1da9049ebdd3ae92e98c5d45b7f15 to your computer and use it in GitHub Desktop.
Save JaviSoto/cff1da9049ebdd3ae92e98c5d45b7f15 to your computer and use it in GitHub Desktop.
RAC logEvents
public enum ReactiveCocoaEventType {
case Started
case Next
case Completed
case Failed
case Terminated
case Disposed
case Interrupted
public static let allEvents: Set<ReactiveCocoaEventType> = {
return [.Started, .Next, .Completed, .Failed, .Terminated, .Disposed, .Interrupted]
}()
}
extension SignalProducerType {
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func logEvents(events: Set<ReactiveCocoaEventType> = ReactiveCocoaEventType.allEvents, withMessage message: String, onLogger logger: XCGLogger = log, logLevel: XCGLogger.LogLevel = .Debug, fileName: String = #file, functionName: String = #function, lineNumber: Int = #line) -> SignalProducer<Self.Value, Self.Error> {
let logEvent = { (text: String) in
logger.logln("[\(message)] \(text)", logLevel: logLevel, functionName: functionName, fileName: fileName, lineNumber: lineNumber)
}
typealias OptionalClosure = (() -> ())?
let started: OptionalClosure = events.contains(.Started) ? {
logEvent("Started")
} : nil
let failed: ((Self.Error) -> ())? = events.contains(.Failed) ? { error in
logEvent("Failed with error: \(error)")
} : nil
let next: ((Self.Value) -> ())? = events.contains(.Next) ? { value in
logEvent("Next: \(value)")
} : nil
let completed: OptionalClosure = events.contains(.Completed) ? {
logEvent("Completed")
} : nil
let interrupted: OptionalClosure = events.contains(.Interrupted) ? {
logEvent("Interrupted")
} : nil
let terminated: OptionalClosure = events.contains(.Terminated) ? {
logEvent("Terminated")
} : nil
let disposed: OptionalClosure = events.contains(.Disposed) ? {
logEvent("Disposed")
} : nil
return self.on(started: started, failed: failed, completed: completed, interrupted: interrupted, terminated: terminated, disposed: disposed, next: next)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment