1
1
import Foundation
2
2
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
+ */
6
9
public typealias ConnectionErrorHandler = ( Error ) -> ConnectionErrorAction
7
10
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
+ */
11
15
public typealias HeaderTransform = ( [ String : String ] ) -> [ String : String ]
12
16
13
- /// Potential actions a ConnectionErrorHandler can return
17
+ /// Potential actions a ` ConnectionErrorHandler` can return
14
18
public 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
+ */
17
23
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
+ */
20
28
case shutdown
21
29
}
22
30
23
31
/// Struct representing received event from the stream.
24
32
public struct MessageEvent : Equatable , Hashable {
25
- /// Returns the event data.
33
+ /// The event data of the event .
26
34
public let data : String
27
35
/// The last seen event id, or the event id set in the Config if none have been received.
28
36
public let lastEventId : String ?
29
37
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
+ */
30
44
public init ( data: String , lastEventId: String ? = nil ) {
31
45
self . data = data
32
46
self . lastEventId = lastEventId
@@ -37,24 +51,30 @@ public struct MessageEvent: Equatable, Hashable {
37
51
public protocol EventHandler {
38
52
/// EventSource calls this method when the stream connection has been opened.
39
53
func onOpened( )
54
+
40
55
/// EventSource calls this method when the stream connection has been closed.
41
56
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.
43
60
44
61
- Parameter eventType: The type of the event.
45
62
- Parameter messageEvent: The data for the event.
46
63
*/
47
64
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.
49
68
50
69
- Parameter comment: The comment received.
51
70
*/
52
71
func onComment( comment: String )
72
+
53
73
/**
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.
58
78
59
79
- Parameter error: The error received.
60
80
*/
@@ -63,23 +83,28 @@ public protocol EventHandler {
63
83
64
84
/// Enum values representing the states of an EventSource
65
85
public enum ReadyState : String , Equatable {
66
- /// The EventSource has not been started yet.
86
+ /// The ` EventSource` client has not been started yet.
67
87
case raw
68
- /// The EventSource is attempting to make a connection.
88
+ /// The ` EventSource` client is attempting to make a connection.
69
89
case connecting
70
- /// The EventSource is active and the EventSource is listening for events.
90
+ /// The ` EventSource` client is active and listening for events.
71
91
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.
73
93
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.
75
95
case shutdown
76
96
}
77
97
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 .
79
99
public class UnsuccessfulResponseError : Error {
80
- /// The HTTP response code received
100
+ /// The HTTP response code received.
81
101
public let responseCode : Int
82
102
103
+ /**
104
+ Constructor for an `UnsuccessfulResponseError`.
105
+
106
+ - Parameter responseCode: The HTTP response code of the unsuccessful response.
107
+ */
83
108
public init ( responseCode: Int ) {
84
109
self . responseCode = responseCode
85
110
}
0 commit comments