File tree 3 files changed +71
-22
lines changed
neo4j-driver-deno/lib/bolt-connection/channel/browser
3 files changed +71
-22
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ export default class WebSocketChannel {
54
54
this . _receiveTimeout = null
55
55
this . _receiveTimeoutStarted = false
56
56
this . _receiveTimeoutId = null
57
+ this . _closingPromise = null
57
58
58
59
const { scheme, error } = determineWebSocketScheme ( config , protocolSupplier )
59
60
if ( error ) {
@@ -163,17 +164,23 @@ export default class WebSocketChannel {
163
164
* @returns {Promise } A promise that will be resolved after channel is closed
164
165
*/
165
166
close ( ) {
166
- return new Promise ( ( resolve , reject ) => {
167
- this . _clearConnectionTimeout ( )
168
- if ( this . _ws && this . _ws . readyState !== WS_CLOSED ) {
169
- this . _open = false
170
- this . stopReceiveTimeout ( )
171
- this . _ws . onclose = ( ) => resolve ( )
172
- this . _ws . close ( )
173
- } else {
174
- resolve ( )
175
- }
176
- } )
167
+ if ( this . _closingPromise === null ) {
168
+ this . _closingPromise = new Promise ( ( resolve , reject ) => {
169
+ this . _clearConnectionTimeout ( )
170
+ if ( this . _ws && this . _ws . readyState !== WS_CLOSED ) {
171
+ this . _open = false
172
+ this . stopReceiveTimeout ( )
173
+ this . _ws . onclose = ( ) => {
174
+ resolve ( )
175
+ }
176
+ this . _ws . close ( )
177
+ } else {
178
+ resolve ( )
179
+ }
180
+ } )
181
+ }
182
+
183
+ return this . _closingPromise
177
184
}
178
185
179
186
/**
Original file line number Diff line number Diff line change @@ -323,6 +323,41 @@ describe('WebSocketChannel', () => {
323
323
fakeSetTimeout . uninstall ( )
324
324
}
325
325
} )
326
+
327
+ it ( 'should return always the same promise' , async ( ) => {
328
+ const fakeSetTimeout = setTimeoutMock . install ( )
329
+ try {
330
+ // do not execute setTimeout callbacks
331
+ fakeSetTimeout . pause ( )
332
+ const address = ServerAddress . fromUrl ( 'bolt://localhost:8989' )
333
+ const driverConfig = { connectionTimeout : 4242 }
334
+ const channelConfig = new ChannelConfig (
335
+ address ,
336
+ driverConfig ,
337
+ SERVICE_UNAVAILABLE
338
+ )
339
+ webSocketChannel = new WebSocketChannel (
340
+ channelConfig ,
341
+ undefined ,
342
+ createWebSocketFactory ( WS_OPEN )
343
+ )
344
+
345
+ const promise1 = webSocketChannel . close ( )
346
+ const promise2 = webSocketChannel . close ( )
347
+
348
+ expect ( promise1 ) . toBe ( promise2 )
349
+
350
+ await Promise . all ( [ promise1 , promise2 ] )
351
+
352
+ const promise3 = webSocketChannel . close ( )
353
+
354
+ expect ( promise3 ) . toBe ( promise2 )
355
+
356
+ await promise3
357
+ } finally {
358
+ fakeSetTimeout . uninstall ( )
359
+ }
360
+ } )
326
361
} )
327
362
328
363
describe ( '.setupReceiveTimeout()' , ( ) => {
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ export default class WebSocketChannel {
54
54
this . _receiveTimeout = null
55
55
this . _receiveTimeoutStarted = false
56
56
this . _receiveTimeoutId = null
57
+ this . _closingPromise = null
57
58
58
59
const { scheme, error } = determineWebSocketScheme ( config , protocolSupplier )
59
60
if ( error ) {
@@ -163,17 +164,23 @@ export default class WebSocketChannel {
163
164
* @returns {Promise } A promise that will be resolved after channel is closed
164
165
*/
165
166
close ( ) {
166
- return new Promise ( ( resolve , reject ) => {
167
- this . _clearConnectionTimeout ( )
168
- if ( this . _ws && this . _ws . readyState !== WS_CLOSED ) {
169
- this . _open = false
170
- this . stopReceiveTimeout ( )
171
- this . _ws . onclose = ( ) => resolve ( )
172
- this . _ws . close ( )
173
- } else {
174
- resolve ( )
175
- }
176
- } )
167
+ if ( this . _closingPromise === null ) {
168
+ this . _closingPromise = new Promise ( ( resolve , reject ) => {
169
+ this . _clearConnectionTimeout ( )
170
+ if ( this . _ws && this . _ws . readyState !== WS_CLOSED ) {
171
+ this . _open = false
172
+ this . stopReceiveTimeout ( )
173
+ this . _ws . onclose = ( ) => {
174
+ resolve ( )
175
+ }
176
+ this . _ws . close ( )
177
+ } else {
178
+ resolve ( )
179
+ }
180
+ } )
181
+ }
182
+
183
+ return this . _closingPromise
177
184
}
178
185
179
186
/**
You can’t perform that action at this time.
0 commit comments