Observer.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Observer.swift
  3. // ReactiveSwift
  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 ObserverProtocol {
  10. associatedtype Value
  11. associatedtype Error: Swift.Error
  12. /// Puts a `value` event into `self`.
  13. func send(value: Value)
  14. /// Puts a failed event into `self`.
  15. func send(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 final class Observer<Value, Error: Swift.Error> {
  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: @escaping Action) {
  33. self.action = action
  34. }
  35. /// An initializer that accepts closures for different event types.
  36. ///
  37. /// - parameters:
  38. /// - value: Optional closure executed when a `value` event is observed.
  39. /// - failed: Optional closure that accepts an `Error` parameter when a
  40. /// failed event is observed.
  41. /// - completed: Optional closure executed when a `completed` event is
  42. /// observed.
  43. /// - interruped: Optional closure executed when an `interrupted` event is
  44. /// observed.
  45. public convenience init(
  46. value: ((Value) -> Void)? = nil,
  47. failed: ((Error) -> Void)? = nil,
  48. completed: (() -> Void)? = nil,
  49. interrupted: (() -> Void)? = nil
  50. ) {
  51. self.init { event in
  52. switch event {
  53. case let .value(v):
  54. value?(v)
  55. case let .failed(error):
  56. failed?(error)
  57. case .completed:
  58. completed?()
  59. case .interrupted:
  60. interrupted?()
  61. }
  62. }
  63. }
  64. }
  65. extension Observer: ObserverProtocol {
  66. /// Puts a `value` event into `self`.
  67. ///
  68. /// - parameters:
  69. /// - value: A value sent with the `value` event.
  70. public func send(value: Value) {
  71. action(.value(value))
  72. }
  73. /// Puts a failed event into `self`.
  74. ///
  75. /// - parameters:
  76. /// - error: An error object sent with failed event.
  77. public func send(error: Error) {
  78. action(.failed(error))
  79. }
  80. /// Puts a `completed` event into `self`.
  81. public func sendCompleted() {
  82. action(.completed)
  83. }
  84. /// Puts an `interrupted` event into `self`.
  85. public func sendInterrupted() {
  86. action(.interrupted)
  87. }
  88. }