@@ -146,10 +146,10 @@ extension ServerSentEventsDeserializationSequence.Iterator {
146
146
struct StateMachine {
147
147
148
148
/// The possible states of the state machine.
149
- enum State : Hashable {
149
+ enum State {
150
150
151
151
/// 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 )
153
153
154
154
/// Finished, the terminal state.
155
155
case finished
@@ -161,16 +161,9 @@ extension ServerSentEventsDeserializationSequence.Iterator {
161
161
/// The current state of the state machine.
162
162
private( set) var state : State
163
163
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
-
170
164
/// Creates a new state machine.
171
165
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)
174
167
}
175
168
176
169
/// An action returned by the `next` method.
@@ -193,7 +186,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
193
186
/// - Returns: An action to perform.
194
187
mutating func next( ) -> NextAction {
195
188
switch state {
196
- case . accumulatingEvent( var event, var buffer) :
189
+ case . accumulatingEvent( var event, var buffer, let predicate ) :
197
190
guard let line = buffer. first else { return . needsMore }
198
191
state = . mutating
199
192
buffer. removeFirst ( )
@@ -202,7 +195,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
202
195
// If the last character of data is a newline, strip it.
203
196
if event. data? . hasSuffix ( " \n " ) ?? false { event. data? . removeLast ( ) }
204
197
205
- state = . accumulatingEvent( . init( ) , buffer: buffer)
198
+ state = . accumulatingEvent( . init( ) , buffer: buffer, predicate : predicate )
206
199
207
200
if let data = event. data, !predicate( ArraySlice ( data. utf8) ) {
208
201
state = . finished
@@ -212,7 +205,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
212
205
}
213
206
if line. first! == ASCII . colon {
214
207
// A comment, skip this line.
215
- state = . accumulatingEvent( event, buffer: buffer)
208
+ state = . accumulatingEvent( event, buffer: buffer, predicate : predicate )
216
209
return . noop
217
210
}
218
211
// Parse the field name and value.
@@ -236,7 +229,7 @@ extension ServerSentEventsDeserializationSequence.Iterator {
236
229
}
237
230
guard let value else {
238
231
// An unknown type of event, skip.
239
- state = . accumulatingEvent( event, buffer: buffer)
232
+ state = . accumulatingEvent( event, buffer: buffer, predicate : predicate )
240
233
return . noop
241
234
}
242
235
// Process the field.
@@ -257,11 +250,11 @@ extension ServerSentEventsDeserializationSequence.Iterator {
257
250
}
258
251
default :
259
252
// An unknown or invalid field, skip.
260
- state = . accumulatingEvent( event, buffer: buffer)
253
+ state = . accumulatingEvent( event, buffer: buffer, predicate : predicate )
261
254
return . noop
262
255
}
263
256
// Processed the field, continue.
264
- state = . accumulatingEvent( event, buffer: buffer)
257
+ state = . accumulatingEvent( event, buffer: buffer, predicate : predicate )
265
258
return . noop
266
259
case . finished: return . returnNil
267
260
case . mutating: preconditionFailure ( " Invalid state " )
@@ -283,11 +276,11 @@ extension ServerSentEventsDeserializationSequence.Iterator {
283
276
/// - Returns: An action to perform.
284
277
mutating func receivedValue( _ value: ArraySlice < UInt8 > ? ) -> ReceivedValueAction {
285
278
switch state {
286
- case . accumulatingEvent( let event, var buffer) :
279
+ case . accumulatingEvent( let event, var buffer, let predicate ) :
287
280
if let value {
288
281
state = . mutating
289
282
buffer. append ( value)
290
- state = . accumulatingEvent( event, buffer: buffer)
283
+ state = . accumulatingEvent( event, buffer: buffer, predicate : predicate )
291
284
return . noop
292
285
} else {
293
286
// If no value is received, drop the existing event on the floor.
0 commit comments