@@ -155,6 +155,7 @@ class Backend {
155
155
this . _sessions = { }
156
156
this . _resultObservers = { }
157
157
this . _errors = { }
158
+ this . _txs = { }
158
159
}
159
160
160
161
// Called whenever a new line is received.
@@ -190,7 +191,7 @@ class Backend {
190
191
_handleRequest ( request ) {
191
192
request = JSON . parse ( request )
192
193
const { name, data } = request
193
- console . log ( 'Got request ' + name )
194
+ console . log ( '> Got request ' + name , data )
194
195
switch ( name ) {
195
196
case 'NewDriver' :
196
197
{
@@ -294,7 +295,69 @@ class Backend {
294
295
} )
295
296
}
296
297
break
297
-
298
+ case 'SessionReadTransaction' :
299
+ {
300
+ const { sessionId } = data
301
+ const session = this . _sessions [ sessionId ]
302
+ session
303
+ . readTransaction (
304
+ tx =>
305
+ new Promise ( ( resolve , reject ) => {
306
+ const txId = this . _id ++
307
+ this . _txs [ txId ] = {
308
+ sessionId,
309
+ tx,
310
+ resolve,
311
+ reject,
312
+ txId
313
+ }
314
+ this . _writeResponse ( 'RetryableTry' , { id : txId } )
315
+ } )
316
+ )
317
+ . then ( _ => this . _writeResponse ( 'RetryableDone' , null ) )
318
+ . catch ( error => this . _writeError ( error ) )
319
+ }
320
+ break
321
+ case 'TransactionRun' :
322
+ {
323
+ const { txId, cypher, params } = data
324
+ const tx = this . _txs [ txId ]
325
+ if ( params ) {
326
+ for ( const [ key , value ] of Object . entries ( params ) ) {
327
+ params [ key ] = cypherToNative ( value )
328
+ }
329
+ }
330
+ const result = tx . tx . run ( cypher , params )
331
+ this . _id ++
332
+ const resultObserver = new ResultObserver ( )
333
+ result . subscribe ( resultObserver )
334
+ this . _resultObservers [ this . _id ] = resultObserver
335
+ this . _writeResponse ( 'Result' , {
336
+ id : this . _id
337
+ } )
338
+ }
339
+ break
340
+ case 'RetryablePositive' :
341
+ {
342
+ const { sessionId } = data
343
+ const tx = Object . values ( this . _txs ) . filter (
344
+ ( { sessionId : id } ) => sessionId === id
345
+ ) [ 0 ]
346
+ delete this . _txs [ tx . txId ]
347
+ tx . resolve ( )
348
+ }
349
+ break
350
+ case 'RetryableNegative' :
351
+ {
352
+ const { sessionId, errorId } = data
353
+ const tx = Object . values ( this . _txs ) . filter (
354
+ ( { sessionId : id } ) => sessionId === id
355
+ ) [ 0 ]
356
+ const error = this . _errors [ errorId ] || new Error ( 'Client error' )
357
+ delete this . _txs [ tx . txId ]
358
+ tx . reject ( error )
359
+ }
360
+ break
298
361
default :
299
362
this . _writeBackendError ( 'Unknown request: ' + name )
300
363
console . log ( 'Unknown request: ' + name )
@@ -303,6 +366,7 @@ class Backend {
303
366
}
304
367
305
368
_writeResponse ( name , data ) {
369
+ console . log ( '> writing response' , name , data )
306
370
let response = {
307
371
name : name ,
308
372
data : data
0 commit comments