Skip to content

Commit 6f8940e

Browse files
committed
EventStreams: store predicate closure as an associated value in
`StateMachine`
1 parent f14b5e8 commit 6f8940e

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Sources/OpenAPIRuntime/EventStreams/ServerSentEventsDecoding.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ extension ServerSentEventsDeserializationSequence.Iterator {
146146
struct StateMachine {
147147

148148
/// The possible states of the state machine.
149-
enum State: Hashable {
149+
enum State {
150150

151151
/// Accumulating an event, which hasn't been emitted yet.
152-
case accumulatingEvent(ServerSentEvent, buffer: [ArraySlice<UInt8>])
152+
case accumulatingEvent(ServerSentEvent, buffer: [ArraySlice<UInt8>], predicate: (ArraySlice<UInt8>) -> Bool)
153153

154154
/// Finished, the terminal state.
155155
case finished
@@ -161,16 +161,9 @@ extension ServerSentEventsDeserializationSequence.Iterator {
161161
/// The current state of the state machine.
162162
private(set) var state: State
163163

164-
165-
/// A closure that determines whether the given byte sequence is the terminating byte sequence defined by the API.
166-
/// - Parameter: A sequence of byte chunks.
167-
/// - Returns: `True` until the terminating byte sequence is received.
168-
let predicate: (ArraySlice<UInt8>) -> Bool
169-
170164
/// Creates a new state machine.
171165
init(while predicate: @escaping (ArraySlice<UInt8>) -> Bool) {
172-
self.state = .accumulatingEvent(.init(), buffer: [])
173-
self.predicate = predicate
166+
self.state = .accumulatingEvent(.init(), buffer: [], predicate: predicate)
174167
}
175168

176169
/// An action returned by the `next` method.
@@ -193,7 +186,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
193186
/// - Returns: An action to perform.
194187
mutating func next() -> NextAction {
195188
switch state {
196-
case .accumulatingEvent(var event, var buffer):
189+
case .accumulatingEvent(var event, var buffer, let predicate):
197190
guard let line = buffer.first else { return .needsMore }
198191
state = .mutating
199192
buffer.removeFirst()
@@ -202,7 +195,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
202195
// If the last character of data is a newline, strip it.
203196
if event.data?.hasSuffix("\n") ?? false { event.data?.removeLast() }
204197

205-
state = .accumulatingEvent(.init(), buffer: buffer)
198+
state = .accumulatingEvent(.init(), buffer: buffer, predicate: predicate)
206199

207200
if let data = event.data, !predicate(ArraySlice(data.utf8)) {
208201
state = .finished
@@ -212,7 +205,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
212205
}
213206
if line.first! == ASCII.colon {
214207
// A comment, skip this line.
215-
state = .accumulatingEvent(event, buffer: buffer)
208+
state = .accumulatingEvent(event, buffer: buffer, predicate: predicate)
216209
return .noop
217210
}
218211
// Parse the field name and value.
@@ -236,7 +229,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
236229
}
237230
guard let value else {
238231
// An unknown type of event, skip.
239-
state = .accumulatingEvent(event, buffer: buffer)
232+
state = .accumulatingEvent(event, buffer: buffer, predicate: predicate)
240233
return .noop
241234
}
242235
// Process the field.
@@ -257,11 +250,11 @@ extension ServerSentEventsDeserializationSequence.Iterator {
257250
}
258251
default:
259252
// An unknown or invalid field, skip.
260-
state = .accumulatingEvent(event, buffer: buffer)
253+
state = .accumulatingEvent(event, buffer: buffer, predicate: predicate)
261254
return .noop
262255
}
263256
// Processed the field, continue.
264-
state = .accumulatingEvent(event, buffer: buffer)
257+
state = .accumulatingEvent(event, buffer: buffer, predicate: predicate)
265258
return .noop
266259
case .finished: return .returnNil
267260
case .mutating: preconditionFailure("Invalid state")
@@ -283,11 +276,11 @@ extension ServerSentEventsDeserializationSequence.Iterator {
283276
/// - Returns: An action to perform.
284277
mutating func receivedValue(_ value: ArraySlice<UInt8>?) -> ReceivedValueAction {
285278
switch state {
286-
case .accumulatingEvent(let event, var buffer):
279+
case .accumulatingEvent(let event, var buffer, let predicate):
287280
if let value {
288281
state = .mutating
289282
buffer.append(value)
290-
state = .accumulatingEvent(event, buffer: buffer)
283+
state = .accumulatingEvent(event, buffer: buffer, predicate: predicate)
291284
return .noop
292285
} else {
293286
// If no value is received, drop the existing event on the floor.

0 commit comments

Comments
 (0)