@@ -531,7 +531,7 @@ class QUICConnection extends EventTarget {
531
531
this . stopKeepAliveIntervalTimer ( ) ;
532
532
533
533
// 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 ) => {
535
535
await mon . withF ( this . lockCode , async ( mon ) => {
536
536
// If this is already closed, then `Done` will be thrown
537
537
// Otherwise it can send `CONNECTION_CLOSE` frame
@@ -663,7 +663,7 @@ class QUICConnection extends EventTarget {
663
663
remoteInfo : RemoteInfo ,
664
664
mon ?: Monitor < RWLockWriter > ,
665
665
) : Promise < void > {
666
- await utils . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
666
+ await this . withMonitor ( mon , this . lockbox , RWLockWriter , async ( mon ) => {
667
667
if ( ! mon . isLocked ( this . lockCode ) ) {
668
668
return mon . withF ( this . lockCode , async ( mon ) => {
669
669
return this . recv ( data , remoteInfo , mon ) ;
@@ -773,7 +773,7 @@ class QUICConnection extends EventTarget {
773
773
* @internal
774
774
*/
775
775
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 ) => {
777
777
if ( ! mon . isLocked ( this . lockCode ) ) {
778
778
return mon . withF ( this . lockCode , async ( mon ) => {
779
779
return this . send ( mon ) ;
@@ -1081,6 +1081,26 @@ class QUICConnection extends EventTarget {
1081
1081
return quicStream ;
1082
1082
} ) ;
1083
1083
}
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
+ }
1084
1104
}
1085
1105
1086
1106
export default QUICConnection ;
0 commit comments