@@ -531,7 +531,7 @@ class QUICConnection extends EventTarget {
531531 this . stopKeepAliveIntervalTimer ( ) ;
532532
533533 // Trigger closing connection in the background and await close later.
534- void utils . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
534+ void this . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
535535 await mon . withF ( this . lockCode , async ( mon ) => {
536536 // If this is already closed, then `Done` will be thrown
537537 // Otherwise it can send `CONNECTION_CLOSE` frame
@@ -663,7 +663,7 @@ class QUICConnection extends EventTarget {
663663 remoteInfo : RemoteInfo ,
664664 mon ?: Monitor < RWLockWriter > ,
665665 ) : Promise < void > {
666- await utils . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
666+ await this . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
667667 if ( ! mon . isLocked ( this . lockCode ) ) {
668668 return mon . withF ( this . lockCode , async ( mon ) => {
669669 return this . recv ( data , remoteInfo , mon ) ;
@@ -773,7 +773,7 @@ class QUICConnection extends EventTarget {
773773 * @internal
774774 */
775775 public async send ( mon ?: Monitor < RWLockWriter > ) : Promise < void > {
776- await utils . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
776+ await this . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
777777 if ( ! mon . isLocked ( this . lockCode ) ) {
778778 return mon . withF ( this . lockCode , async ( mon ) => {
779779 return this . send ( mon ) ;
@@ -1081,6 +1081,26 @@ class QUICConnection extends EventTarget {
10811081 return quicStream ;
10821082 } ) ;
10831083 }
1084+
1085+ /**
1086+ * Used as a clean way to create a new monitor if it doesn't exist, otherwise uses the existing one.
1087+ */
1088+ protected async withMonitor < T > (
1089+ mon : Monitor < RWLockWriter > | undefined ,
1090+ lockBox : LockBox < RWLockWriter > ,
1091+ lockConstructor : { new ( ) : RWLockWriter } ,
1092+ f : ( mon : Monitor < RWLockWriter > ) => Promise < T > ,
1093+ locksPending ?: Map < string , { count : number } > ,
1094+ ) : Promise < T > {
1095+ if ( mon == null ) {
1096+ return await withF (
1097+ [ contextsUtils . monitor ( lockBox , lockConstructor , locksPending ) ] ,
1098+ ( [ mon ] ) => f ( mon ) ,
1099+ ) ;
1100+ } else {
1101+ return f ( mon ) ;
1102+ }
1103+ }
10841104}
10851105
10861106export default QUICConnection ;
0 commit comments