@@ -66,9 +66,9 @@ private[flow] final class StreamSubscriber[F[_], A] private (
66
66
* since they are always done on the effect run after the state update took place.
67
67
* Meaning this should be correct if the Producer is well-behaved.
68
68
*/
69
- private var inOnNextLoop : Boolean = _
69
+ private var inOnNextLoop : Boolean = false
70
70
private var buffer : Array [Any ] = null
71
- private var index : Int = _
71
+ private var index : Int = 0
72
72
73
73
/** Receives the next record from the upstream reactive-streams system. */
74
74
override final def onNext (a : A ): Unit = {
@@ -164,10 +164,10 @@ private[flow] final class StreamSubscriber[F[_], A] private (
164
164
state -> run {
165
165
// We do the updates here,
166
166
// to ensure they happen after we have secured the state.
167
- inOnNextLoop = true
168
- index = 1
169
167
buffer = new Array (chunkSize)
170
168
buffer(0 ) = a
169
+ index = 1
170
+ inOnNextLoop = true
171
171
}
172
172
}
173
173
@@ -188,15 +188,18 @@ private[flow] final class StreamSubscriber[F[_], A] private (
188
188
Idle (s) -> run {
189
189
// We do the updates here,
190
190
// to ensure they happen after we have secured the state.
191
- cb.apply( Right ( Some ( Chunk .array(buffer))) )
191
+ val chunk = Chunk .array(buffer)
192
192
inOnNextLoop = false
193
193
buffer = null
194
+ cb.apply(Right (Some (chunk)))
194
195
}
195
196
196
197
case state =>
197
198
Failed (
198
199
new InvalidStateException (operation = s " Received record [ ${buffer.last}] " , state)
199
200
) -> run {
201
+ // We do the updates here,
202
+ // to ensure they happen after we have secured the state.
200
203
inOnNextLoop = false
201
204
buffer = null
202
205
}
@@ -206,19 +209,15 @@ private[flow] final class StreamSubscriber[F[_], A] private (
206
209
case Uninitialized (Some (cb)) =>
207
210
Terminal -> run {
208
211
cb.apply(Left (ex))
209
- // We do the updates here,
210
- // to ensure they happen after we have secured the state.
211
- inOnNextLoop = false
212
- buffer = null
213
212
}
214
213
215
214
case WaitingOnUpstream (cb, _) =>
216
215
Terminal -> run {
217
- cb.apply(Left (ex))
218
216
// We do the updates here,
219
217
// to ensure they happen after we have secured the state.
220
218
inOnNextLoop = false
221
219
buffer = null
220
+ cb.apply(Left (ex))
222
221
}
223
222
224
223
case _ =>
@@ -240,17 +239,21 @@ private[flow] final class StreamSubscriber[F[_], A] private (
240
239
241
240
case WaitingOnUpstream (cb, s) =>
242
241
Terminal -> run {
242
+ // We do the updates here,
243
+ // to ensure they happen after we have secured the state.
243
244
if (canceled) {
244
245
s.cancel()
246
+ inOnNextLoop = false
247
+ buffer = null
245
248
cb.apply(Right (None ))
246
- } else if (index == 0 ) {
249
+ } else if (buffer eq null ) {
250
+ inOnNextLoop = false
247
251
cb.apply(Right (None ))
248
252
} else {
249
- cb.apply(Right (Some (Chunk .array(buffer, offset = 0 , length = index))))
250
- // We do the updates here,
251
- // to ensure they happen after we have secured the state.
253
+ val chunk = Chunk .array(buffer, offset = 0 , length = index)
252
254
inOnNextLoop = false
253
255
buffer = null
256
+ cb.apply(Right (Some (chunk)))
254
257
}
255
258
}
256
259
@@ -268,10 +271,6 @@ private[flow] final class StreamSubscriber[F[_], A] private (
268
271
case Idle (s) =>
269
272
WaitingOnUpstream (cb, s) -> run {
270
273
s.request(chunkSize.toLong)
271
- // We do the updates here,
272
- // to ensure they happen after we have secured the state.
273
- inOnNextLoop = false
274
- index = 0
275
274
}
276
275
277
276
case state @ Uninitialized (Some (otherCB)) =>
@@ -283,14 +282,14 @@ private[flow] final class StreamSubscriber[F[_], A] private (
283
282
284
283
case state @ WaitingOnUpstream (otherCB, s) =>
285
284
Terminal -> run {
286
- s.cancel()
287
- val ex = Left (new InvalidStateException (operation = " Received request" , state))
288
- otherCB.apply(ex)
289
- cb.apply(ex)
290
285
// We do the updates here,
291
286
// to ensure they happen after we have secured the state.
292
287
inOnNextLoop = false
293
288
buffer = null
289
+ s.cancel()
290
+ val ex = Left (new InvalidStateException (operation = " Received request" , state))
291
+ otherCB.apply(ex)
292
+ cb.apply(ex)
294
293
}
295
294
296
295
case Failed (ex) =>
0 commit comments