@@ -95,6 +95,7 @@ class ResultStreamObserver extends StreamObserver {
95
95
this . _discard = false
96
96
this . _fetchSize = fetchSize
97
97
this . _setState ( reactive ? _states . READY : _states . READY_STREAMING )
98
+ this . _setupAuoPull ( fetchSize )
98
99
}
99
100
100
101
/**
@@ -113,6 +114,9 @@ class ResultStreamObserver extends StreamObserver {
113
114
} )
114
115
} else {
115
116
this . _queuedRecords . push ( record )
117
+ if ( this . _queuedRecords . length > this . _highRecordWatermark ) {
118
+ this . _autoPull = false
119
+ }
116
120
}
117
121
}
118
122
@@ -182,6 +186,12 @@ class ResultStreamObserver extends StreamObserver {
182
186
if ( this . _queuedRecords . length > 0 && observer . onNext ) {
183
187
for ( let i = 0 ; i < this . _queuedRecords . length ; i ++ ) {
184
188
observer . onNext ( this . _queuedRecords [ i ] )
189
+ if ( this . _queuedRecords . length - i - 1 <= this . _lowRecordWatermark ) {
190
+ this . _autoPull = true
191
+ if ( this . _state === _states . READY ) {
192
+ this . _handleStreaming ( )
193
+ }
194
+ }
185
195
}
186
196
}
187
197
if ( this . _tail && observer . onCompleted ) {
@@ -334,15 +344,16 @@ class ResultStreamObserver extends StreamObserver {
334
344
if ( this . _head && this . _observers . some ( o => o . onNext || o . onCompleted ) ) {
335
345
if ( this . _discard ) {
336
346
this . _discardFunction ( this . _connection , this . _queryId , this )
337
- } else {
347
+ this . _setState ( _states . STREAMING )
348
+ } else if ( this . _autoPull ) {
338
349
this . _moreFunction (
339
350
this . _connection ,
340
351
this . _queryId ,
341
352
this . _fetchSize ,
342
353
this
343
354
)
355
+ this . _setState ( _states . STREAMING )
344
356
}
345
- this . _setState ( _states . STREAMING )
346
357
}
347
358
}
348
359
@@ -360,6 +371,17 @@ class ResultStreamObserver extends StreamObserver {
360
371
_setState ( state ) {
361
372
this . _state = state
362
373
}
374
+
375
+ _setupAuoPull ( fetchSize ) {
376
+ this . _autoPull = true
377
+ if ( fetchSize === ALL ) {
378
+ this . _lowRecordWatermark = Number . MAX_VALUE // we shall always lower than this number to enable auto pull
379
+ this . _highRecordWatermark = Number . MAX_VALUE // we shall never reach this number to disable auto pull
380
+ } else {
381
+ this . _lowRecordWatermark = 0.3 * fetchSize
382
+ this . _highRecordWatermark = 0.7 * fetchSize
383
+ }
384
+ }
363
385
}
364
386
365
387
class LoginObserver extends StreamObserver {
0 commit comments