Observer.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Observer.swift
  3. // ReactiveCocoa
  4. //
  5. // Created by Andy Matuschak on 10/2/15.
  6. // Copyright © 2015 GitHub. All rights reserved.
  7. //
  8. /// A protocol for type-constrained extensions of `Observer`.
  9. public protocol ObserverType {
  10. associatedtype Value
  11. associatedtype Error: ErrorType
  12. /// Puts a `Next` event into `self`.
  13. func sendNext(value: Value)
  14. /// Puts a `Failed` event into `self`.
  15. func sendFailed(error: Error)
  16. /// Puts a `Completed` event into `self`.
  17. func sendCompleted()
  18. /// Puts an `Interrupted` event into `self`.
  19. func sendInterrupted()
  20. }
  21. /// An Observer is a simple wrapper around a function which can receive Events
  22. /// (typically from a Signal).
  23. public struct Observer<Value, Error: ErrorType> {
  24. public typealias Action = Event<Value, Error> -> Void
  25. /// An action that will be performed upon arrival of the event.
  26. public let action: Action
  27. /// An initializer that accepts a closure accepting an event for the
  28. /// observer.
  29. ///
  30. /// - parameters:
  31. /// - action: A closure to lift over received event.
  32. public init(_ action: Action) {
  33. self.action = action
  34. }
  35. /// An initializer that accepts closures for different event types.
  36. ///
  37. /// - parameters:
  38. /// - failed: Optional closure that accepts an `Error` parameter when a
  39. /// `Failed` event is observed.
  40. /// - completed: Optional closure executed when a `Completed` event is
  41. /// observed.
  42. /// - interruped: Optional closure executed when an `Interrupted` event is
  43. /// observed.
  44. /// - next: Optional closure executed when a `Next` event is observed.
  45. public init(failed: (Error -> Void)? = nil, completed: (() -> Void)? = nil, interrupted: (() -> Void)? = nil, next: (Value -> Void)? = nil) {
  46. self.init { event in
  47. switch event {
  48. case let .Next(value):
  49. next?(value)
  50. case let .Failed(error):
  51. failed?(error)
  52. case .Completed:
  53. completed?()
  54. case .Interrupted:
  55. interrupted?()
  56. }
  57. }
  58. }
  59. }
  60. extension Observer: ObserverType {
  61. /// Puts a `Next` event into `self`.
  62. ///
  63. /// - parameters:
  64. /// - value: A value sent with the `Next` event.
  65. public func sendNext(value: Value) {
  66. action(.Next(value))
  67. }
  68. /// Puts a `Failed` event into `self`.
  69. ///
  70. /// - parameters:
  71. /// - error: An error object sent with `Failed` event.
  72. public func sendFailed(error: Error) {
  73. action(.Failed(error))
  74. }
  75. /// Puts a `Completed` event into `self`.
  76. public func sendCompleted() {
  77. action(.Completed)
  78. }
  79. /// Puts an `Interrupted` event into `self`.
  80. public func sendInterrupted() {
  81. action(.Interrupted)
  82. }
  83. }