@@ -26,6 +26,7 @@ import {Node, Path, PathSegment, Relationship, UnboundRelationship} from '../gra
2626import { newError } from './../error' ;
2727import ChannelConfig from './ch-config' ;
2828import { parseHost , parsePort } from './util' ;
29+ import StreamObserver from './stream-observer' ;
2930
3031let Channel ;
3132if ( NodeChannel . available ) {
@@ -183,6 +184,8 @@ class Connection {
183184 this . _isHandlingFailure = false ;
184185 this . _currentFailure = null ;
185186
187+ this . _state = new ConnectionState ( this ) ;
188+
186189 // Set to true on fatal errors, to get this out of session pool.
187190 this . _isBroken = false ;
188191
@@ -330,7 +333,8 @@ class Connection {
330333 /** Queue an INIT-message to be sent to the database */
331334 initialize ( clientName , token , observer ) {
332335 log ( "C" , "INIT" , clientName , token ) ;
333- this . _queueObserver ( observer ) ;
336+ const initObserver = this . _state . wrap ( observer ) ;
337+ this . _queueObserver ( initObserver ) ;
334338 this . _packer . packStruct ( INIT , [ this . _packable ( clientName ) , this . _packable ( token ) ] ,
335339 ( err ) => this . _handleFatalError ( err ) ) ;
336340 this . _chunker . messageBoundary ( ) ;
@@ -416,6 +420,15 @@ class Connection {
416420 }
417421 }
418422
423+ /**
424+ * Get promise resolved when connection initialization succeed or rejected when it fails.
425+ * Connection is initialized using {@link initialize} function.
426+ * @return {Promise<Connection> } the result of connection initialization.
427+ */
428+ initializationCompleted ( ) {
429+ return this . _state . initializationCompleted ( ) ;
430+ }
431+
419432 /**
420433 * Synchronize - flush all queued outgoing messages and route their responses
421434 * to their respective handlers.
@@ -450,6 +463,59 @@ class Connection {
450463 }
451464}
452465
466+ class ConnectionState {
467+
468+ /**
469+ * @constructor
470+ * @param {Connection } connection the connection to track state for.
471+ */
472+ constructor ( connection ) {
473+ this . _connection = connection ;
474+ this . _resolvePromise = null ;
475+ this . _promise = new Promise ( resolve => {
476+ this . _resolvePromise = resolve ;
477+ } ) ;
478+ }
479+
480+ /**
481+ * Wrap the given observer to track connection's initialization state.
482+ * @param {StreamObserver } observer the observer used for INIT message.
483+ * @return {StreamObserver } updated observer.
484+ */
485+ wrap ( observer ) {
486+ return {
487+ onNext : record => {
488+ if ( observer && observer . onNext ) {
489+ observer . onNext ( record ) ;
490+ }
491+ } ,
492+ onError : error => {
493+ this . _resolvePromise ( Promise . reject ( error ) ) ;
494+ if ( observer && observer . onError ) {
495+ observer . onError ( error ) ;
496+ }
497+ } ,
498+ onCompleted : metaData => {
499+ if ( metaData && metaData . server ) {
500+ this . _connection . setServerVersion ( metaData . server ) ;
501+ }
502+ this . _resolvePromise ( this . _connection ) ;
503+ if ( observer && observer . onCompleted ) {
504+ observer . onCompleted ( metaData ) ;
505+ }
506+ }
507+ } ;
508+ }
509+
510+ /**
511+ * Get promise resolved when connection initialization succeed or rejected when it fails.
512+ * @return {Promise<Connection> } the result of connection initialization.
513+ */
514+ initializationCompleted ( ) {
515+ return this . _promise ;
516+ }
517+ }
518+
453519/**
454520 * Crete new connection to the provided url.
455521 * @access private
0 commit comments