11import Foundation
22
3- /// Type for a function that will be notified when EventSource encounters a connection failure.
4- /// This is different from onError in that it will not be called for other kinds of errors; also,
5- /// it has the ability to tell EventSource to stop reconnecting.
3+ /**
4+ Type for a function that will be notified when the `EventSource` client encounters a connection failure.
5+
6+ This is different from `EventHandler.onError(error:)` in that it will not be called for other kinds of errors; also,
7+ it has the ability to tell the client to stop reconnecting by returning a `ConnectionErrorAction.shutdown`.
8+ */
69public typealias ConnectionErrorHandler = ( Error ) -> ConnectionErrorAction
710
8- /// Type for a function that will take in the current http headers
9- /// and return a new set of http headers to be used when connecting
10- /// and reconnecting to a stream.
11+ /**
12+ Type for a function that will take in the current HTTP headers and return a new set of HTTP headers to be used when
13+ connecting and reconnecting to a stream.
14+ */
1115public typealias HeaderTransform = ( [ String : String ] ) -> [ String : String ]
1216
13- /// Potential actions a ConnectionErrorHandler can return
17+ /// Potential actions a ` ConnectionErrorHandler` can return
1418public enum ConnectionErrorAction {
15- /// Specifies that the error should be logged normally and dispatched to the EventHandler.
16- /// Connection retrying will proceed normally if appropriate.
19+ /**
20+ Specifies that the error should be logged normally and dispatched to the `EventHandler`. Connection retrying will
21+ proceed normally if appropriate.
22+ */
1723 case proceed
18- /// Specifies that the connection should be immediately shut down and not retried. The error
19- /// will not be dispatched to the EventHandler
24+ /**
25+ Specifies that the connection should be immediately shut down and not retried. The error will not be dispatched
26+ to the `EventHandler`
27+ */
2028 case shutdown
2129}
2230
2331/// Struct representing received event from the stream.
2432public struct MessageEvent : Equatable , Hashable {
25- /// Returns the event data.
33+ /// The event data of the event .
2634 public let data : String
2735 /// The last seen event id, or the event id set in the Config if none have been received.
2836 public let lastEventId : String ?
2937
38+ /**
39+ Constructor for a `MessageEvent`
40+
41+ - Parameter data: The `data` field of the `MessageEvent`.
42+ - Parameter eventType: The `lastEventId` field of the `MessageEvent`.
43+ */
3044 public init ( data: String , lastEventId: String ? = nil ) {
3145 self . data = data
3246 self . lastEventId = lastEventId
@@ -37,24 +51,30 @@ public struct MessageEvent: Equatable, Hashable {
3751public protocol EventHandler {
3852 /// EventSource calls this method when the stream connection has been opened.
3953 func onOpened( )
54+
4055 /// EventSource calls this method when the stream connection has been closed.
4156 func onClosed( )
42- /** EventSource calls this method when it has received a new event from the stream.
57+
58+ /**
59+ EventSource calls this method when it has received a new event from the stream.
4360
4461 - Parameter eventType: The type of the event.
4562 - Parameter messageEvent: The data for the event.
4663 */
4764 func onMessage( eventType: String , messageEvent: MessageEvent )
48- /** EventSource calls this method when it has received a comment line from the stream.
65+
66+ /**
67+ EventSource calls this method when it has received a comment line from the stream.
4968
5069 - Parameter comment: The comment received.
5170 */
5271 func onComment( comment: String )
72+
5373 /**
54- This method will be called for all exceptions that occur on the socket connection
55- (including an {@link UnsuccessfulResponseError} if the server returns an unexpected HTTP status),
56- but only after the ConnectionErrorHandler (if any) has processed it. If you need to
57- do anything that affects the state of the connection, use ConnectionErrorHandler.
74+ This method will be called for all exceptions that occur on the network connection (including an
75+ ` UnsuccessfulResponseError` if the server returns an unexpected HTTP status), but only after the
76+ ConnectionErrorHandler (if any) has processed it. If you need to do anything that affects the state of the
77+ connection, use ConnectionErrorHandler.
5878
5979 - Parameter error: The error received.
6080 */
@@ -63,23 +83,28 @@ public protocol EventHandler {
6383
6484/// Enum values representing the states of an EventSource
6585public enum ReadyState : String , Equatable {
66- /// The EventSource has not been started yet.
86+ /// The ` EventSource` client has not been started yet.
6787 case raw
68- /// The EventSource is attempting to make a connection.
88+ /// The ` EventSource` client is attempting to make a connection.
6989 case connecting
70- /// The EventSource is active and the EventSource is listening for events.
90+ /// The ` EventSource` client is active and listening for events.
7191 case open
72- /// The connection has been closed or has failed, and the EventSource will attempt to reconnect.
92+ /// The connection has been closed or has failed, and the ` EventSource` will attempt to reconnect.
7393 case closed
74- /// The connection has been permanently closed and will not reconnect.
94+ /// The connection has been permanently closed and the `EventSource` not reconnect.
7595 case shutdown
7696}
7797
78- /// Error class that means the remote server returned an HTTP error .
98+ /// Error class that indicates the remote server returned an unsuccessful HTTP response code .
7999public class UnsuccessfulResponseError : Error {
80- /// The HTTP response code received
100+ /// The HTTP response code received.
81101 public let responseCode : Int
82102
103+ /**
104+ Constructor for an `UnsuccessfulResponseError`.
105+
106+ - Parameter responseCode: The HTTP response code of the unsuccessful response.
107+ */
83108 public init ( responseCode: Int ) {
84109 self . responseCode = responseCode
85110 }
0 commit comments