@@ -20,8 +20,9 @@ import Record from '../record'
20
20
import Connection from './connection'
21
21
import { newError , PROTOCOL_ERROR } from '../error'
22
22
import { isString } from './util'
23
+ import Integer from '../integer'
23
24
24
- const DefaultBatchSize = 100
25
+ const DefaultBatchSize = 50
25
26
26
27
class StreamObserver {
27
28
onNext ( rawRecord ) { }
@@ -45,10 +46,11 @@ class ResultStreamObserver extends StreamObserver {
45
46
/**
46
47
*
47
48
* @param {Object } param
48
- * @param {Connection } connection
49
- * @param { } param.moreFunction -
50
- * @param { } param.discardFunction -
51
- * @param { } param.batchSize -
49
+ * @param {Connection } param.connection
50
+ * @param {boolean } param.reactive
51
+ * @param {function(connection: Connection, stmtId: number|Integer, n: number|Integer, observer: StreamObserver) } param.moreFunction -
52
+ * @param {function(connection: Connection, stmtId: number|Integer, observer: StreamObserver) } param.discardFunction -
53
+ * @param {number|Integer } param.batchSize -
52
54
* @param {function(err: Error): Promise|void } param.beforeError -
53
55
* @param {function(err: Error): Promise|void } param.afterError -
54
56
* @param {function(keys: string[]): Promise|void } param.beforeKeys -
@@ -58,6 +60,7 @@ class ResultStreamObserver extends StreamObserver {
58
60
*/
59
61
constructor ( {
60
62
connection,
63
+ reactive = false ,
61
64
moreFunction,
62
65
discardFunction,
63
66
batchSize = DefaultBatchSize ,
@@ -71,6 +74,8 @@ class ResultStreamObserver extends StreamObserver {
71
74
super ( )
72
75
73
76
this . _connection = connection
77
+ this . _reactive = reactive
78
+ this . _streaming = false
74
79
75
80
this . _fieldKeys = null
76
81
this . _fieldLookup = null
@@ -105,7 +110,11 @@ class ResultStreamObserver extends StreamObserver {
105
110
onNext ( rawRecord ) {
106
111
let record = new Record ( this . _fieldKeys , rawRecord , this . _fieldLookup )
107
112
if ( this . _observers . some ( o => o . onNext ) ) {
108
- this . _observers . forEach ( o => ( o . onNext ? o . onNext ( record ) : { } ) )
113
+ this . _observers . forEach ( o => {
114
+ if ( o . onNext ) {
115
+ o . onNext ( record )
116
+ }
117
+ } )
109
118
} else {
110
119
this . _queuedRecords . push ( record )
111
120
}
@@ -149,14 +158,20 @@ class ResultStreamObserver extends StreamObserver {
149
158
this . _head = this . _fieldKeys
150
159
151
160
if ( this . _observers . some ( o => o . onKeys ) ) {
152
- this . _observers . forEach ( o =>
153
- o . onKeys ? o . onKeys ( this . _fieldKeys ) : { }
154
- )
161
+ this . _observers . forEach ( o => {
162
+ if ( o . onKeys ) {
163
+ o . onKeys ( this . _fieldKeys )
164
+ }
165
+ } )
155
166
}
156
167
157
168
if ( this . _afterKeys ) {
158
169
this . _afterKeys ( this . _fieldKeys )
159
170
}
171
+
172
+ if ( this . _reactive ) {
173
+ this . _handleStreaming ( )
174
+ }
160
175
}
161
176
162
177
if ( beforeHandlerResult ) {
@@ -165,22 +180,13 @@ class ResultStreamObserver extends StreamObserver {
165
180
continuation ( )
166
181
}
167
182
} else {
183
+ this . _streaming = false
184
+
168
185
if ( meta . has_more ) {
169
186
// We've consumed current batch and server notified us that there're more
170
187
// records to stream. Let's invoke more or discard function based on whether
171
188
// the user wants to discard streaming or not
172
- if ( this . _discard ) {
173
- this . _discardFunction ( {
174
- connection : this . _connection ,
175
- statementId : this . _statementId
176
- } )
177
- } else {
178
- this . _moreFunction ( {
179
- connection : this . _connection ,
180
- statementId : this . _statementId ,
181
- n : this . _batchSize
182
- } )
183
- }
189
+ this . _handleStreaming ( )
184
190
185
191
delete meta . has_more
186
192
} else {
@@ -200,9 +206,11 @@ class ResultStreamObserver extends StreamObserver {
200
206
this . _tail = completionMetadata
201
207
202
208
if ( this . _observers . some ( o => o . onCompleted ) ) {
203
- this . _observers . forEach ( o =>
204
- o . onCompleted ? o . onCompleted ( completionMetadata ) : { }
205
- )
209
+ this . _observers . forEach ( o => {
210
+ if ( o . onCompleted ) {
211
+ o . onCompleted ( completionMetadata )
212
+ }
213
+ } )
206
214
}
207
215
208
216
if ( this . _afterComplete ) {
@@ -219,6 +227,28 @@ class ResultStreamObserver extends StreamObserver {
219
227
}
220
228
}
221
229
230
+ _handleStreaming ( ) {
231
+ if (
232
+ this . _reactive &&
233
+ this . _head &&
234
+ this . _observers . some ( o => o . onNext || o . onCompleted ) &&
235
+ ! this . _streaming
236
+ ) {
237
+ this . _streaming = true
238
+
239
+ if ( this . _discard ) {
240
+ this . _discardFunction ( this . _connection , this . _statementId , this )
241
+ } else {
242
+ this . _moreFunction (
243
+ this . _connection ,
244
+ this . _statementId ,
245
+ this . _batchSize ,
246
+ this
247
+ )
248
+ }
249
+ }
250
+ }
251
+
222
252
_storeMetadataForCompletion ( meta ) {
223
253
const keys = Object . keys ( meta )
224
254
let index = keys . length
@@ -282,9 +312,11 @@ class ResultStreamObserver extends StreamObserver {
282
312
283
313
const continuation = ( ) => {
284
314
if ( this . _observers . some ( o => o . onError ) ) {
285
- this . _observers . forEach ( o =>
286
- o . onError ? o . onError ( error ) : console . log ( error )
287
- )
315
+ this . _observers . forEach ( o => {
316
+ if ( o . onError ) {
317
+ o . onError ( error )
318
+ }
319
+ } )
288
320
}
289
321
290
322
if ( this . _afterError ) {
@@ -324,6 +356,10 @@ class ResultStreamObserver extends StreamObserver {
324
356
observer . onCompleted ( this . _tail )
325
357
}
326
358
this . _observers . push ( observer )
359
+
360
+ if ( this . _reactive ) {
361
+ this . _handleStreaming ( )
362
+ }
327
363
}
328
364
329
365
hasFailed ( ) {
0 commit comments