Skip to content

Commit 625b013

Browse files
committed
EventStreams: make closure non-optional
Default initialise with `{ _ in true }`
1 parent b329be3 commit 625b013

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ where Upstream.Element == ArraySlice<UInt8> {
2828
/// The upstream sequence.
2929
private let upstream: Upstream
3030

31-
/// An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
31+
/// A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
3232
/// - Parameter: A byte chunk.
3333
/// - Returns: `True` until the terminating byte sequence is received.
34-
private let predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)?
34+
private let predicate: @Sendable (ArraySlice<UInt8>) -> Bool
3535

3636
/// Creates a new sequence.
3737
/// - Parameters:
3838
/// - upstream: The upstream sequence of arbitrary byte chunks.
39-
/// - while: An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
40-
public init(upstream: Upstream, while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)?) {
39+
/// - while: A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
40+
public init(upstream: Upstream, while predicate: @escaping @Sendable (ArraySlice<UInt8>) -> Bool) {
4141
self.upstream = upstream
4242
self.predicate = predicate
4343
}
@@ -56,14 +56,14 @@ extension ServerSentEventsDeserializationSequence: AsyncSequence {
5656
var upstream: UpstreamIterator
5757

5858
/// The state machine of the iterator.
59-
var stateMachine: StateMachine = .init()
59+
var stateMachine: StateMachine
6060

61-
/// An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
61+
/// A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
6262
/// - Parameter: A byte chunk.
6363
/// - Returns: `True` until the terminating byte sequence is received.
64-
let predicate: ((ArraySlice<UInt8>) -> Bool)?
64+
let predicate: (ArraySlice<UInt8>) -> Bool
6565

66-
init(upstream: any AsyncIteratorProtocol, while predicate: ((ArraySlice<UInt8>) -> Bool)?) {
66+
init(upstream: any AsyncIteratorProtocol, while predicate: @escaping ((ArraySlice<UInt8>) -> Bool)) {
6767
self.upstream = upstream as! UpstreamIterator
6868
self.stateMachine = .init(while: predicate)
6969
self.predicate = predicate
@@ -100,9 +100,9 @@ extension AsyncSequence where Element == ArraySlice<UInt8>, Self: Sendable {
100100
/// Returns another sequence that decodes each event's data as the provided type using the provided decoder.
101101
///
102102
/// Use this method if the event's `data` field is not JSON, or if you don't want to parse it using `asDecodedServerSentEventsWithJSONData`.
103-
/// - Parameter: An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
103+
/// - Parameter: A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
104104
/// - Returns: A sequence that provides the events.
105-
public func asDecodedServerSentEvents(while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil) -> ServerSentEventsDeserializationSequence<
105+
public func asDecodedServerSentEvents(while predicate: @escaping @Sendable (ArraySlice<UInt8>) -> Bool = { _ in true }) -> ServerSentEventsDeserializationSequence<
106106
ServerSentEventsLineDeserializationSequence<Self>
107107
> { .init(upstream: ServerSentEventsLineDeserializationSequence(upstream: self), while: predicate) }
108108

@@ -112,12 +112,12 @@ extension AsyncSequence where Element == ArraySlice<UInt8>, Self: Sendable {
112112
/// - Parameters:
113113
/// - dataType: The type to decode the JSON data into.
114114
/// - decoder: The JSON decoder to use.
115-
/// - while: An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
115+
/// - while: A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
116116
/// - Returns: A sequence that provides the events with the decoded JSON data.
117117
public func asDecodedServerSentEventsWithJSONData<JSONDataType: Decodable>(
118118
of dataType: JSONDataType.Type = JSONDataType.self,
119119
decoder: JSONDecoder = .init(),
120-
while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
120+
while predicate: @escaping @Sendable (ArraySlice<UInt8>) -> Bool = { _ in true }
121121
) -> AsyncThrowingMapSequence<
122122
ServerSentEventsDeserializationSequence<ServerSentEventsLineDeserializationSequence<Self>>,
123123
ServerSentEventWithJSONData<JSONDataType>
@@ -158,13 +158,13 @@ extension ServerSentEventsDeserializationSequence.Iterator {
158158
private(set) var state: State
159159

160160

161-
/// An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
161+
/// A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
162162
/// - Parameter: A sequence of byte chunks.
163163
/// - Returns: `True` until the terminating byte sequence is received.
164-
let predicate: ((ArraySlice<UInt8>) -> Bool)?
164+
let predicate: (ArraySlice<UInt8>) -> Bool
165165

166166
/// Creates a new state machine.
167-
init(while predicate: ((ArraySlice<UInt8>) -> Bool)? = nil) {
167+
init(while predicate: @escaping (ArraySlice<UInt8>) -> Bool) {
168168
self.state = .accumulatingEvent(.init(), buffer: [])
169169
self.predicate = predicate}
170170

@@ -198,11 +198,9 @@ extension ServerSentEventsDeserializationSequence.Iterator {
198198
// If the last character of data is a newline, strip it.
199199
if event.data?.hasSuffix("\n") ?? false { event.data?.removeLast() }
200200

201-
if let predicate = predicate {
202-
if let data = event.data {
203-
if !predicate(ArraySlice(Data(data.utf8))) {
204-
return .returnNil
205-
}
201+
if let data = event.data {
202+
if !predicate(ArraySlice(Data(data.utf8))) {
203+
return .returnNil
206204
}
207205
}
208206
return .emitEvent(event)

Tests/OpenAPIRuntimeTests/EventStreams/Test_ServerSentEventsDecoding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import XCTest
1616
import Foundation
1717

1818
final class Test_ServerSentEventsDecoding: Test_Runtime {
19-
func _test(input: String, output: [ServerSentEvent], file: StaticString = #filePath, line: UInt = #line, while predicate: ((ArraySlice<UInt8>) -> Bool)? = nil, eventCountOffset: Int = 0)
19+
func _test(input: String, output: [ServerSentEvent], file: StaticString = #filePath, line: UInt = #line, while predicate: (ArraySlice<UInt8>) -> Bool = { _ in true }, eventCountOffset: Int = 0)
2020
async throws
2121
{
2222
let sequence = asOneBytePerElementSequence(ArraySlice(input.utf8)).asDecodedServerSentEvents()
@@ -112,7 +112,7 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
112112
output: [ServerSentEventWithJSONData<JSONType>],
113113
file: StaticString = #filePath,
114114
line: UInt = #line,
115-
while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
115+
while predicate: @escaping @Sendable (ArraySlice<UInt8>) -> Bool = { _ in true }
116116
) async throws {
117117
let sequence = asOneBytePerElementSequence(ArraySlice(input.utf8))
118118
.asDecodedServerSentEventsWithJSONData(of: JSONType.self, while: predicate)

0 commit comments

Comments
 (0)