Skip to content

Commit 026c4cc

Browse files
authored
Add jazzy config, build docs in CI, and clean up documentation comments. (#17)
1 parent 9c2a1c4 commit 026c4cc

File tree

5 files changed

+97
-49
lines changed

5 files changed

+97
-49
lines changed

.circleci/config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ jobs:
5757
command: swift test -v 2>&1 | tee 'artifacts/raw-logs-swiftpm.txt' | xcpretty -r junit -o 'test-results/swiftpm/junit.xml'
5858
when: always
5959

60+
- run:
61+
name: Build Documentation
62+
command: |
63+
sudo gem install jazzy
64+
jazzy -o artifacts/docs
65+
6066
- store_test_results:
6167
path: test-results
6268

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/.build
66
xcuserdata/
77
IDEWorkspaceChecks.plist
8-
.swiftpm
8+
.swiftpm
9+
/docs

.jazzy.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module: LDSwiftEventSource
2+
author: LaunchDarkly
3+
author_url: https://launchdarkly.com
4+
github_url: https://github.com/launchdarkly/swift-eventsource
5+
clean: true
6+
swift_build_tool: spm
7+
readme: README.md
8+
documentation:
9+
- CHANGELOG.md
10+
- CONTRIBUTING.md
11+
- LICENSE.txt
12+
13+
copyright: 'Copyright © 2020 Catamorphic Co.'

Source/LDSwiftEventSource.swift

+26-23
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,68 @@ public class EventSource {
1717
private let esDelegate: EventSourceDelegate
1818

1919
/**
20-
Initialize an EventSource with the given configuration.
20+
Initialize the `EventSource` client with the given configuration.
2121

22-
- Parameter config: The configuration for initializing the EventSource.
22+
- Parameter config: The configuration for initializing the `EventSource` client.
2323
*/
2424
public init(config: Config) {
2525
esDelegate = EventSourceDelegate(config: config)
2626
}
2727

2828
/**
29-
Start the EventSource object.
29+
Start the `EventSource` client.
3030

31-
This will create a stream connection to the configured URL. The application will be informed of received events
32-
and state changes using the configured EventHandler.
31+
This will initiate a streaming connection to the configured URL. The application will be informed of received
32+
events and state changes using the configured `EventHandler`.
3333
*/
3434
public func start() {
3535
esDelegate.start()
3636
}
3737

38-
/// Shuts down the EventSource object. It is not valid to restart the EventSource after calling this function.
38+
/// Shuts down the `EventSource` client. It is not valid to restart the client after calling this function.
3939
public func stop() {
4040
esDelegate.stop()
4141
}
4242

43-
/**
44-
Get the most recently received event ID, or the configured `lastEventId` if no event IDs have been received.
45-
*/
43+
/// Get the most recently received event ID, or the value of `EventSource.Config.lastEventId` if no event IDs have
44+
/// been received.
4645
public func getLastEventId() -> String? { esDelegate.getLastEventId() }
4746

48-
/// Struct describing the configuration of the EventSource
47+
/// Struct for configuring the EventSource.
4948
public struct Config {
50-
/// The EventHandler called in response to activity on the stream.
49+
/// The `EventHandler` called in response to activity on the stream.
5150
public let handler: EventHandler
52-
/// The URL of the request used when connecting to the EventSource API.
51+
/// The `URL` of the request used when connecting to the EventSource API.
5352
public let url: URL
5453

55-
/// The method to use for the EventSource connection
54+
/// The HTTP method to use for the API request.
5655
public var method: String = "GET"
57-
/// Optional body to be sent with the initial request
56+
/// Optional HTTP body to be included in the API request.
5857
public var body: Data?
59-
/// Error handler that can determine whether to proceed or shutdown.
60-
public var connectionErrorHandler: ConnectionErrorHandler = { _ in .proceed }
61-
/// An initial value for the last-event-id to be set on the initial request
58+
/// An initial value for the last-event-id header to be sent on the initial request
6259
public var lastEventId: String?
63-
/// Additional headers to be set on the request
60+
/// Additional HTTP headers to be set on the request
6461
public var headers: [String: String] = [:]
65-
/// Provides the ability to add conditional headers
62+
/// Transform function to allow dynamically configuring the headers on each API request.
6663
public var headerTransform: HeaderTransform = { $0 }
6764
/// The minimum amount of time to wait before reconnecting after a failure
6865
public var reconnectTime: TimeInterval = 1.0
6966
/// The maximum amount of time to wait before reconnecting after a failure
7067
public var maxReconnectTime: TimeInterval = 30.0
71-
/// The minimum amount of time for an EventSource connection to remain open before allowing connection
68+
/// The minimum amount of time for an `EventSource` connection to remain open before allowing the connection
7269
/// backoff to reset.
7370
public var backoffResetThreshold: TimeInterval = 60.0
74-
/// The maximum amount of time between receiving any data before considering the connection to have
75-
/// timed out.
71+
/// The maximum amount of time between receiving any data before considering the connection to have timed out.
7672
public var idleTimeout: TimeInterval = 300.0
7773

78-
/// Create a new configuration with an EventHandler and a URL
74+
/**
75+
An error handler that is called when an error occurs and can shut down the client in response.
76+
77+
The default error handler will always attempt to reconnect on an error, unless `EventSource.stop()` is called.
78+
*/
79+
public var connectionErrorHandler: ConnectionErrorHandler = { _ in .proceed }
80+
81+
/// Create a new configuration with an `EventHandler` and a `URL`
7982
public init(handler: EventHandler, url: URL) {
8083
self.handler = handler
8184
self.url = url

Source/Types.swift

+50-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
import 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+
*/
69
public 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+
*/
1115
public typealias HeaderTransform = ([String: String]) -> [String: String]
1216

13-
/// Potential actions a ConnectionErrorHandler can return
17+
/// Potential actions a `ConnectionErrorHandler` can return
1418
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+
*/
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.
2432
public 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 {
3751
public 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
6585
public 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.
7999
public 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

Comments
 (0)