Event.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Event.swift
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2015-01-16.
  6. // Copyright (c) 2015 GitHub. All rights reserved.
  7. //
  8. /// Represents a signal event.
  9. ///
  10. /// Signals must conform to the grammar:
  11. /// `Next* (Failed | Completed | Interrupted)?`
  12. public enum Event<Value, Error: ErrorType> {
  13. /// A value provided by the signal.
  14. case Next(Value)
  15. /// The signal terminated because of an error. No further events will be
  16. /// received.
  17. case Failed(Error)
  18. /// The signal successfully terminated. No further events will be received.
  19. case Completed
  20. /// Event production on the signal has been interrupted. No further events
  21. /// will be received.
  22. ///
  23. /// - important: This event does not signify the successful or failed
  24. /// completion of the signal.
  25. case Interrupted
  26. /// Whether this event indicates signal termination (i.e., that no further
  27. /// events will be received).
  28. public var isTerminating: Bool {
  29. switch self {
  30. case .Next:
  31. return false
  32. case .Failed, .Completed, .Interrupted:
  33. return true
  34. }
  35. }
  36. /// Lift the given closure over the event's value.
  37. ///
  38. /// - important: The closure is called only on `Next` type events.
  39. ///
  40. /// - parameters:
  41. /// - f: A closure that accepts a value and returns a new value
  42. ///
  43. /// - returns: An event with function applied to a value in case `self` is a
  44. /// `Next` type of event.
  45. public func map<U>(f: Value -> U) -> Event<U, Error> {
  46. switch self {
  47. case let .Next(value):
  48. return .Next(f(value))
  49. case let .Failed(error):
  50. return .Failed(error)
  51. case .Completed:
  52. return .Completed
  53. case .Interrupted:
  54. return .Interrupted
  55. }
  56. }
  57. /// Lift the given closure over the event's error.
  58. ///
  59. /// - important: The closure is called only on `Failed` type event.
  60. ///
  61. /// - parameters:
  62. /// - f: A closure that accepts an error object and returns
  63. /// a new error object
  64. ///
  65. /// - returns: An event with function applied to an error object in case
  66. /// `self` is a `.Failed` type of event.
  67. public func mapError<F>(f: Error -> F) -> Event<Value, F> {
  68. switch self {
  69. case let .Next(value):
  70. return .Next(value)
  71. case let .Failed(error):
  72. return .Failed(f(error))
  73. case .Completed:
  74. return .Completed
  75. case .Interrupted:
  76. return .Interrupted
  77. }
  78. }
  79. /// Unwrap the contained `Next` value.
  80. public var value: Value? {
  81. if case let .Next(value) = self {
  82. return value
  83. } else {
  84. return nil
  85. }
  86. }
  87. /// Unwrap the contained `Error` value.
  88. public var error: Error? {
  89. if case let .Failed(error) = self {
  90. return error
  91. } else {
  92. return nil
  93. }
  94. }
  95. }
  96. public func == <Value: Equatable, Error: Equatable> (lhs: Event<Value, Error>, rhs: Event<Value, Error>) -> Bool {
  97. switch (lhs, rhs) {
  98. case let (.Next(left), .Next(right)):
  99. return left == right
  100. case let (.Failed(left), .Failed(right)):
  101. return left == right
  102. case (.Completed, .Completed):
  103. return true
  104. case (.Interrupted, .Interrupted):
  105. return true
  106. default:
  107. return false
  108. }
  109. }
  110. extension Event: CustomStringConvertible {
  111. public var description: String {
  112. switch self {
  113. case let .Next(value):
  114. return "NEXT \(value)"
  115. case let .Failed(error):
  116. return "FAILED \(error)"
  117. case .Completed:
  118. return "COMPLETED"
  119. case .Interrupted:
  120. return "INTERRUPTED"
  121. }
  122. }
  123. }
  124. /// Event protocol for constraining signal extensions
  125. public protocol EventType {
  126. /// The value type of an event.
  127. associatedtype Value
  128. /// The error type of an event. If errors aren't possible then `NoError` can
  129. /// be used.
  130. associatedtype Error: ErrorType
  131. /// Extracts the event from the receiver.
  132. var event: Event<Value, Error> { get }
  133. }
  134. extension Event: EventType {
  135. public var event: Event<Value, Error> {
  136. return self
  137. }
  138. }