@@ -27,25 +27,29 @@ import {InvalidOperationError} from '../errors';
2727import { determineConnectionHosts } from './driver.utils' ;
2828
2929export default abstract class DriverBase < Rec = any > {
30+ protected isReady = false ;
31+ protected isShutDown = false ;
32+ protected isSlave = false ;
3033 protected discoveryTables : IDiscoveryTable [ ] = [ ] ;
31- protected readonly connectionConfig : IConnectionConfig ;
3234 protected connections : Connection [ ] = [ ] ;
3335 protected available : Connection [ ] = [ ] ;
34- protected readySubject = new AsyncSubject < void > ( ) ;
35- protected availableConnections : BehaviorSubject < Connection [ ] > = new BehaviorSubject < Connection [ ] > ( [ ] ) ;
36- protected requestQueue : Subject < IRequest > = new Subject ( ) ;
37- protected processing : Observable < [ IRequest , Connection ] > = this . requestQueue . pipe (
36+ protected readonly connectionConfig : IConnectionConfig ;
37+ protected readonly connectionSubject : BehaviorSubject < Connection [ ] > = new BehaviorSubject < Connection [ ] > ( [ ] ) ;
38+ protected readonly readySubject = new AsyncSubject < void > ( ) ;
39+ protected readonly availableConnections : BehaviorSubject < Connection [ ] > = new BehaviorSubject < Connection [ ] > ( [ ] ) ;
40+ protected readonly requestQueue : Subject < IRequest > = new Subject ( ) ;
41+ protected readonly processing : Observable < [ IRequest , Connection ] > = this . requestQueue . pipe (
3842 flatMap ( ( request ) => this . requestAvailableConnection ( request . meta ) . pipe (
3943 map ( ( connections ) : [ IRequest , Connection ] => [ request , connections [ 0 ] ] )
4044 ) ) ,
4145 ) ;
42- protected isReady = false ;
43- protected isShutDown = false ;
44- protected isBound = false ;
4546
4647 constructor ( protected readonly config : IDriverConfig ) {
4748 this . connectionConfig = _ . merge ( { } , DEFAULT_CONNECTION_CONFIG , this . config . connectionConfig ) ;
4849
50+ // @boundMethod does not like inheritance
51+ this . query = this . query . bind ( this ) ;
52+
4953 if ( ! this . config . useRouting ) {
5054 this . isReady = true ;
5155
@@ -55,7 +59,7 @@ export default abstract class DriverBase<Rec = any> {
5559 }
5660 }
5761
58- //@boundMethod
62+ // @boundMethod does not like inheritance
5963 query < Res = Rec > ( cypher : string , params : any = { } , meta : IQueryMeta = { } ) : Observable < Res > {
6064 const { pullN = - 1 , db} = meta ; // @todo : transaction session ID
6165
@@ -66,7 +70,7 @@ export default abstract class DriverBase<Rec = any> {
6670 data : [ cypher , params ] ,
6771 additionalData : ( protocol ) => {
6872 if ( protocol < BOLT_PROTOCOLS . V3 ) {
69- return [ ]
73+ return [ ] ;
7074 }
7175
7276 return db && protocol > BOLT_PROTOCOLS . V3
@@ -90,26 +94,26 @@ export default abstract class DriverBase<Rec = any> {
9094 throw new InvalidOperationError ( 'Transaction pending' ) ;
9195 } ;
9296
93- commit ( ) {
97+ commit < Res = Rec > ( ) : Observable < Res > {
9498 throw new InvalidOperationError ( 'No transaction pending' ) ;
9599 }
96100
97- rollback ( ) {
101+ rollback < Res = Rec > ( ) : Observable < Res > {
98102 throw new InvalidOperationError ( 'No transaction pending' ) ;
99103 }
100104
101- abstract shutDown ( ) : Promise < this> ;
105+ abstract shutDown ( ) : Observable < this> ;
102106
103107 @boundMethod
104108 protected sendMessages < Res > ( messages : IClientMessage [ ] = [ ] , meta ?: IRequestMeta ) : Observable < Res > {
105109 return this . readySubject . pipe (
106- flatMap ( ( ) => this . getConnectionForRequest ( messages , meta ) ) ,
110+ flatMap ( ( ) => this . waitForConnection ( messages , meta ) ) ,
107111 flatMap ( ( [ request , connection ] ) => this . executeRequests < Res > ( request , connection ) )
108112 ) ;
109113 }
110114
111115 @boundMethod
112- protected getConnectionForRequest ( messages : IClientMessage [ ] , meta ?: IRequestMeta ) : Observable < [ IRequest , Connection ] > {
116+ protected waitForConnection ( messages : IClientMessage [ ] , meta ?: IRequestMeta ) : Observable < [ IRequest , Connection ] > {
113117 if ( this . isShutDown ) {
114118 throw new InvalidOperationError ( 'Driver is shut down' ) ;
115119 }
@@ -130,7 +134,6 @@ export default abstract class DriverBase<Rec = any> {
130134 take ( 1 ) ,
131135 tap ( ( [ , connection ] ) => this . occupyConnection ( connection ) ) ,
132136 ) ;
133-
134137 }
135138
136139 @boundMethod
@@ -277,8 +280,8 @@ export default abstract class DriverBase<Rec = any> {
277280
278281 const validAvailable = _ . filter ( this . available , connectionPredicate ) ;
279282
280- if ( this . isBound || arrayHasItems ( validAvailable ) ) {
281- return this . availableConnections
283+ if ( this . isSlave || arrayHasItems ( validAvailable ) ) {
284+ return this . availableConnections ;
282285 }
283286
284287 if ( this . connections . length < this . config . maxPoolSize ) {
@@ -290,19 +293,21 @@ export default abstract class DriverBase<Rec = any> {
290293 ] ;
291294
292295 this . releaseConnection ( connection ) ;
296+ this . connectionSubject . next ( this . connections ) ;
293297 this . availableConnections . next ( this . available ) ;
294298 }
295299
296300 return this . availableConnections . pipe (
297301 skipWhile ( ( connections ) => ! _ . some ( connections , connectionPredicate ) )
298- )
302+ ) ;
299303 }
300304
301305 @boundMethod
302306 protected terminateConnection ( connection : Connection ) : Promise < Connection > {
303307 this . occupyConnection ( connection ) ;
304308
305309 this . connections = _ . filter ( this . connections , ( { id} ) => id !== connection . id ) ;
310+ this . connectionSubject . next ( this . connections ) ;
306311
307312 return connection . terminate ( ) ;
308313 }
0 commit comments