@@ -28,16 +28,16 @@ where Upstream.Element == ArraySlice<UInt8> {
28
28
/// The upstream sequence.
29
29
private let upstream : Upstream
30
30
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.
32
32
/// - Parameter: A byte chunk.
33
33
/// - 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
35
35
36
36
/// Creates a new sequence.
37
37
/// - Parameters:
38
38
/// - 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 ) {
41
41
self . upstream = upstream
42
42
self . predicate = predicate
43
43
}
@@ -56,14 +56,14 @@ extension ServerSentEventsDeserializationSequence: AsyncSequence {
56
56
var upstream : UpstreamIterator
57
57
58
58
/// The state machine of the iterator.
59
- var stateMachine : StateMachine = . init ( )
59
+ var stateMachine : StateMachine
60
60
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.
62
62
/// - Parameter: A byte chunk.
63
63
/// - Returns: `True` until the terminating byte sequence is received.
64
- let predicate : ( ( ArraySlice < UInt8 > ) -> Bool ) ?
64
+ let predicate : ( ArraySlice < UInt8 > ) -> Bool
65
65
66
- init ( upstream: any AsyncIteratorProtocol , while predicate: ( ( ArraySlice < UInt8 > ) -> Bool ) ? ) {
66
+ init ( upstream: any AsyncIteratorProtocol , while predicate: @escaping ( ( ArraySlice < UInt8 > ) -> Bool ) ) {
67
67
self . upstream = upstream as! UpstreamIterator
68
68
self . stateMachine = . init( while: predicate)
69
69
self . predicate = predicate
@@ -100,9 +100,9 @@ extension AsyncSequence where Element == ArraySlice<UInt8>, Self: Sendable {
100
100
/// Returns another sequence that decodes each event's data as the provided type using the provided decoder.
101
101
///
102
102
/// 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.
104
104
/// - 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 <
106
106
ServerSentEventsLineDeserializationSequence < Self >
107
107
> { . init( upstream: ServerSentEventsLineDeserializationSequence ( upstream: self ) , while: predicate) }
108
108
@@ -112,12 +112,12 @@ extension AsyncSequence where Element == ArraySlice<UInt8>, Self: Sendable {
112
112
/// - Parameters:
113
113
/// - dataType: The type to decode the JSON data into.
114
114
/// - 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.
116
116
/// - Returns: A sequence that provides the events with the decoded JSON data.
117
117
public func asDecodedServerSentEventsWithJSONData< JSONDataType: Decodable > (
118
118
of dataType: JSONDataType . Type = JSONDataType . self,
119
119
decoder: JSONDecoder = . init( ) ,
120
- while predicate: ( @ Sendable ( ArraySlice < UInt8 > ) -> Bool ) ? = nil
120
+ while predicate: @escaping @ Sendable ( ArraySlice < UInt8 > ) -> Bool = { _ in true }
121
121
) -> AsyncThrowingMapSequence <
122
122
ServerSentEventsDeserializationSequence < ServerSentEventsLineDeserializationSequence < Self > > ,
123
123
ServerSentEventWithJSONData < JSONDataType >
@@ -158,13 +158,13 @@ extension ServerSentEventsDeserializationSequence.Iterator {
158
158
private( set) var state : State
159
159
160
160
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.
162
162
/// - Parameter: A sequence of byte chunks.
163
163
/// - Returns: `True` until the terminating byte sequence is received.
164
- let predicate : ( ( ArraySlice < UInt8 > ) -> Bool ) ?
164
+ let predicate : ( ArraySlice < UInt8 > ) -> Bool
165
165
166
166
/// Creates a new state machine.
167
- init ( while predicate: ( ( ArraySlice < UInt8 > ) -> Bool ) ? = nil ) {
167
+ init ( while predicate: @escaping ( ArraySlice < UInt8 > ) -> Bool ) {
168
168
self . state = . accumulatingEvent( . init( ) , buffer: [ ] )
169
169
self . predicate = predicate}
170
170
@@ -198,11 +198,9 @@ extension ServerSentEventsDeserializationSequence.Iterator {
198
198
// If the last character of data is a newline, strip it.
199
199
if event. data? . hasSuffix ( " \n " ) ?? false { event. data? . removeLast ( ) }
200
200
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
206
204
}
207
205
}
208
206
return . emitEvent( event)
0 commit comments