Skip to content

Commit 9957e84

Browse files
committed
EventStreams: rename "terminate" to "while"
Use `predicate` when referring to `while` internally, use `while` in the public API
1 parent 59bf51e commit 9957e84

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift

+22-22
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ where Upstream.Element == ArraySlice<UInt8> {
3030

3131
/// An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
3232
/// - Parameter: A byte chunk.
33-
/// - Returns: `True` if the given byte sequence is the terminating byte sequence defined by the API.
34-
private let terminate: (@Sendable (ArraySlice<UInt8>) -> Bool)?
33+
/// - Returns: `True` until the terminating byte sequence is received.
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-
/// - terminate: An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
40-
public init(upstream: Upstream, terminate: (@Sendable (ArraySlice<UInt8>) -> Bool)?) {
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)?) {
4141
self.upstream = upstream
42-
self.terminate = terminate
42+
self.predicate = predicate
4343
}
4444
}
4545

@@ -60,13 +60,13 @@ extension ServerSentEventsDeserializationSequence: AsyncSequence {
6060

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

66-
init(upstream: any AsyncIteratorProtocol, terminate: ((ArraySlice<UInt8>) -> Bool)?) {
66+
init(upstream: any AsyncIteratorProtocol, while predicate: ((ArraySlice<UInt8>) -> Bool)?) {
6767
self.upstream = upstream as! UpstreamIterator
68-
self.stateMachine = .init(terminate: terminate)
69-
self.terminate = terminate
68+
self.stateMachine = .init(while: predicate)
69+
self.predicate = predicate
7070
}
7171

7272
/// Asynchronously advances to the next element and returns it, or ends the
@@ -91,7 +91,7 @@ extension ServerSentEventsDeserializationSequence: AsyncSequence {
9191
/// Creates the asynchronous iterator that produces elements of this
9292
/// asynchronous sequence.
9393
public func makeAsyncIterator() -> Iterator<Upstream.AsyncIterator> {
94-
Iterator(upstream: upstream.makeAsyncIterator(), terminate: terminate)
94+
Iterator(upstream: upstream.makeAsyncIterator(), while: predicate)
9595
}
9696
}
9797

@@ -102,27 +102,27 @@ extension AsyncSequence where Element == ArraySlice<UInt8>, Self: Sendable {
102102
/// Use this method if the event's `data` field is not JSON, or if you don't want to parse it using `asDecodedServerSentEventsWithJSONData`.
103103
/// - Parameter: An optional 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(terminate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil) -> ServerSentEventsDeserializationSequence<
105+
public func asDecodedServerSentEvents(while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil) -> ServerSentEventsDeserializationSequence<
106106
ServerSentEventsLineDeserializationSequence<Self>
107-
> { .init(upstream: ServerSentEventsLineDeserializationSequence(upstream: self), terminate: terminate) }
107+
> { .init(upstream: ServerSentEventsLineDeserializationSequence(upstream: self), while: predicate) }
108108

109109
/// Returns another sequence that decodes each event's data as the provided type using the provided decoder.
110110
///
111111
/// Use this method if the event's `data` field is JSON.
112112
/// - Parameters:
113113
/// - dataType: The type to decode the JSON data into.
114114
/// - decoder: The JSON decoder to use.
115-
/// - terminate: An optional closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
115+
/// - while: An optional 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-
terminate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
120+
while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
121121
) -> AsyncThrowingMapSequence<
122122
ServerSentEventsDeserializationSequence<ServerSentEventsLineDeserializationSequence<Self>>,
123123
ServerSentEventWithJSONData<JSONDataType>
124124
> {
125-
asDecodedServerSentEvents(terminate: terminate)
125+
asDecodedServerSentEvents(while: predicate)
126126
.map { event in
127127
ServerSentEventWithJSONData(
128128
event: event.event,
@@ -160,13 +160,13 @@ extension ServerSentEventsDeserializationSequence.Iterator {
160160

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

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

171171
/// An action returned by the `next` method.
172172
enum NextAction {
@@ -198,9 +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 terminate = terminate {
201+
if let predicate = predicate {
202202
if let data = event.data {
203-
if terminate(ArraySlice(Data(data.utf8))) {
203+
if predicate(ArraySlice(Data(data.utf8))) {
204204
return .returnNil
205205
}
206206
}

Tests/OpenAPIRuntimeTests/EventStreams/Test_ServerSentEventsDecoding.swift

+5-5
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, terminate: ((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)? = nil, eventCountOffset: Int = 0)
2020
async throws
2121
{
2222
let sequence = asOneBytePerElementSequence(ArraySlice(input.utf8)).asDecodedServerSentEvents()
@@ -101,7 +101,7 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
101101
output: [
102102
.init(data: "hello\nworld")
103103
],
104-
terminate: { incomingData in
104+
while: { incomingData in
105105
incomingData == ArraySlice<UInt8>(Data("[DONE]".utf8))
106106
},
107107
eventCountOffset: -2
@@ -112,10 +112,10 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
112112
output: [ServerSentEventWithJSONData<JSONType>],
113113
file: StaticString = #filePath,
114114
line: UInt = #line,
115-
terminate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
115+
while predicate: (@Sendable (ArraySlice<UInt8>) -> Bool)? = nil
116116
) async throws {
117117
let sequence = asOneBytePerElementSequence(ArraySlice(input.utf8))
118-
.asDecodedServerSentEventsWithJSONData(of: JSONType.self, terminate: terminate)
118+
.asDecodedServerSentEventsWithJSONData(of: JSONType.self, while: predicate)
119119
let events = try await [ServerSentEventWithJSONData<JSONType>](collecting: sequence)
120120
XCTAssertEqual(events.count, output.count, file: file, line: line)
121121
for (index, linePair) in zip(events, output).enumerated() {
@@ -171,7 +171,7 @@ final class Test_ServerSentEventsDecoding: Test_Runtime {
171171
.init(event: "event1", data: TestEvent(index: 1), id: "1"),
172172
.init(event: "event2", data: TestEvent(index: 2), id: "2"),
173173
],
174-
terminate: { incomingData in
174+
while: { incomingData in
175175
incomingData == ArraySlice<UInt8>(Data("[DONE]".utf8))
176176
}
177177
)

0 commit comments

Comments
 (0)