File tree Expand file tree Collapse file tree 3 files changed +31
-6
lines changed Expand file tree Collapse file tree 3 files changed +31
-6
lines changed Original file line number Diff line number Diff line change 2020import Session from './session' ;
2121import { Pool } from './internal/pool' ;
2222import { connect } from "./internal/connector" ;
23+ import StreamObserver from './internal/stream-observer' ;
2324
2425/**
2526 * A Driver instance is used for mananging {@link Session}s.
@@ -52,8 +53,9 @@ class Driver {
5253 */
5354 _createConnection ( release ) {
5455 let sessionId = this . _sessionIdGenerator ++ ;
56+ let streamObserver = new _ConnectionStreamObserver ( this ) ;
5557 let conn = connect ( this . _url ) ;
56- conn . initialize ( this . _userAgent , this . _token ) ;
58+ conn . initialize ( this . _userAgent , this . _token , streamObserver ) ;
5759 conn . _id = sessionId ;
5860 conn . _release = ( ) => release ( conn ) ;
5961
@@ -121,4 +123,22 @@ class Driver {
121123 }
122124}
123125
126+ /** Internal stream observer used for connection state */
127+ class _ConnectionStreamObserver extends StreamObserver {
128+ constructor ( driver ) {
129+ super ( ) ;
130+ this . _driver = driver ;
131+ this . _hasFailed = false ;
132+ }
133+ onError ( error ) {
134+ if ( ! this . _hasFailed ) {
135+ super . onError ( error ) ;
136+ if ( this . _driver . onError ) {
137+ this . _driver . onError ( error ) ;
138+ }
139+ this . _hasFailed = true ;
140+ }
141+ }
142+ }
143+
124144export default Driver
Original file line number Diff line number Diff line change @@ -55,9 +55,9 @@ class WebSocketChannel {
5555 }
5656 } ;
5757
58- this . _ws . onerror = ( ) => {
58+ this . _ws . onerror = ( err ) => {
5959 if ( self . onerror ) {
60- self . onerror ( ) ;
60+ self . onerror ( err ) ;
6161 }
6262 }
6363 }
Original file line number Diff line number Diff line change @@ -207,7 +207,12 @@ class Connection {
207207 }
208208 } ;
209209
210- this . _ch . onerror = ( ) => self . _isBroken = true ;
210+ this . _ch . onerror = ( error ) => {
211+ self . _isBroken = true ;
212+ if ( this . _currentObserver . onError ) {
213+ this . _currentObserver . onError ( error ) ;
214+ }
215+ }
211216
212217 this . _dechunker . onmessage = ( buf ) => {
213218 self . _handleMessage ( self . _unpacker . unpack ( buf ) ) ;
@@ -266,9 +271,9 @@ class Connection {
266271 break ;
267272 case IGNORED :
268273 try {
269- if ( this . _errorMsg )
274+ if ( this . _errorMsg && this . _currentObserver . onError )
270275 this . _currentObserver . onError ( this . _errorMsg ) ;
271- else
276+ else if ( this . _currentObserver . onError )
272277 this . _currentObserver . onError ( msg ) ;
273278 } finally {
274279 this . _currentObserver = this . _pendingObservers . shift ( ) ;
You can’t perform that action at this time.
0 commit comments