@@ -19,13 +19,10 @@ namespace Microsoft.DotNet.Watcher.Tools
1919{
2020 internal class BlazorWebAssemblyDeltaApplier : IDeltaApplier
2121 {
22- private static Task < ImmutableArray < string > > ? _cachedCapabilties ;
23- private static readonly ImmutableArray < string > _baselineCapabilities = ImmutableArray . Create < string > ( "Baseline" ) ;
22+ private static Task < ImmutableArray < string > > ? s_cachedCapabilties ;
2423 private readonly IReporter _reporter ;
2524 private int _sequenceId ;
2625
27- private static readonly TimeSpan VerifyDeltaTimeout = TimeSpan . FromSeconds ( 5 ) ;
28-
2926 public BlazorWebAssemblyDeltaApplier ( IReporter reporter )
3027 {
3128 _reporter = reporter ;
@@ -40,57 +37,48 @@ public ValueTask InitializeAsync(DotNetWatchContext context, CancellationToken c
4037
4138 public Task < ImmutableArray < string > > GetApplyUpdateCapabilitiesAsync ( DotNetWatchContext context , CancellationToken cancellationToken )
4239 {
43- _cachedCapabilties ??= GetApplyUpdateCapabilitiesCoreAsync ( ) ;
44- return _cachedCapabilties ;
40+ return s_cachedCapabilties ??= GetApplyUpdateCapabilitiesCoreAsync ( ) ;
4541
4642 async Task < ImmutableArray < string > > GetApplyUpdateCapabilitiesCoreAsync ( )
4743 {
4844 if ( context . BrowserRefreshServer is null )
4945 {
50- return _baselineCapabilities ;
46+ throw new ApplicationException ( "The browser refresh server is unavailable." ) ;
5147 }
5248
53- await context . BrowserRefreshServer . WaitForClientConnectionAsync ( cancellationToken ) ;
49+ _reporter . Verbose ( "Connecting to the browser." ) ;
5450
51+ await context . BrowserRefreshServer . WaitForClientConnectionAsync ( cancellationToken ) ;
5552 await context . BrowserRefreshServer . SendJsonSerlialized ( default ( BlazorRequestApplyUpdateCapabilities ) , cancellationToken ) ;
56- // 32k ought to be enough for anyone.
53+
5754 var buffer = ArrayPool < byte > . Shared . Rent ( 32 * 1024 ) ;
5855 try
5956 {
60- // We'll query the browser and ask it send capabilities. If the browser does not respond in a short duration, we'll assume something is amiss and return
61- // baseline capabilities.
62- var response = await context . BrowserRefreshServer . ReceiveAsync ( buffer , cancellationToken )
63- . AsTask ( )
64- . WaitAsync ( TimeSpan . FromSeconds ( 15 ) , cancellationToken ) ;
57+ // We'll query the browser and ask it send capabilities.
58+ var response = await context . BrowserRefreshServer . ReceiveAsync ( buffer , cancellationToken ) ;
6559 if ( ! response . HasValue || ! response . Value . EndOfMessage || response . Value . MessageType != WebSocketMessageType . Text )
6660 {
67- return _baselineCapabilities ;
61+ throw new ApplicationException ( "Unable to connect to the browser refresh server." ) ;
6862 }
6963
70- var values = Encoding . UTF8 . GetString ( buffer . AsSpan ( 0 , response . Value . Count ) ) ;
64+ var capabilities = Encoding . UTF8 . GetString ( buffer . AsSpan ( 0 , response . Value . Count ) ) ;
7165
72- // Capabilitiies are expressed a space-separated string.
66+ // Capabilities are expressed a space-separated string.
7367 // e.g. https://github.com/dotnet/runtime/blob/14343bdc281102bf6fffa1ecdd920221d46761bc/src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs#L87
74- var result = values . Split ( ' ' ) . ToImmutableArray ( ) ;
75- return result ;
76- }
77- catch ( TimeoutException )
78- {
68+ return capabilities . Split ( ' ' ) . ToImmutableArray ( ) ;
7969 }
8070 finally
8171 {
8272 ArrayPool < byte > . Shared . Return ( buffer ) ;
8373 }
84-
85- return _baselineCapabilities ;
8674 }
8775 }
8876
8977 public async ValueTask < bool > Apply ( DotNetWatchContext context , ImmutableArray < WatchHotReloadService . Update > solutionUpdate , CancellationToken cancellationToken )
9078 {
9179 if ( context . BrowserRefreshServer is null )
9280 {
93- _reporter . Verbose ( "Unable to send deltas because the refresh server is unavailable." ) ;
81+ _reporter . Verbose ( "Unable to send deltas because the browser refresh server is unavailable." ) ;
9482 return false ;
9583 }
9684
@@ -104,7 +92,7 @@ public async ValueTask<bool> Apply(DotNetWatchContext context, ImmutableArray<Wa
10492 } ) ;
10593
10694 await context . BrowserRefreshServer . SendJsonWithSecret ( sharedSecret => new UpdatePayload { SharedSecret = sharedSecret , Deltas = deltas } , cancellationToken ) ;
107- return await VerifyDeltaApplied ( context , cancellationToken ) . WaitAsync ( VerifyDeltaTimeout , cancellationToken ) ;
95+ return await VerifyDeltaApplied ( context , cancellationToken ) ;
10896 }
10997
11098 public async ValueTask ReportDiagnosticsAsync ( DotNetWatchContext context , IEnumerable < string > diagnostics , CancellationToken cancellationToken )
@@ -123,33 +111,18 @@ public async ValueTask ReportDiagnosticsAsync(DotNetWatchContext context, IEnume
123111 private async Task < bool > VerifyDeltaApplied ( DotNetWatchContext context , CancellationToken cancellationToken )
124112 {
125113 var _receiveBuffer = new byte [ 1 ] ;
126- try
114+ var result = await context . BrowserRefreshServer ! . ReceiveAsync ( _receiveBuffer , cancellationToken ) ;
115+ if ( result is null )
127116 {
128- // We want to give the client some time to ACK the deltas being applied. VerifyDeltaApplied is limited by a
129- // 5 second wait timeout enforced using a WaitAsync. However, WaitAsync only works reliably if the calling
130- // function is async. If BrowserRefreshServer.ReceiveAsync finishes synchronously, the WaitAsync would
131- // never have an opportunity to execute. Consequently, we'll give it some reasonable number of opportunities
132- // to loop before we decide that applying deltas failed.
133- for ( var i = 0 ; i < 100 ; i ++ )
134- {
135- var result = await context . BrowserRefreshServer ! . ReceiveAsync ( _receiveBuffer , cancellationToken ) ;
136- if ( result is null )
137- {
138- // A null result indicates no clients are connected. No deltas could have been applied in this state.
139- _reporter . Verbose ( "No client is connected to ack deltas" ) ;
140- return false ;
141- }
142-
143- if ( IsDeltaReceivedMessage ( result . Value ) )
144- {
145- // 1 indicates success.
146- return _receiveBuffer [ 0 ] == 1 ;
147- }
148- }
117+ // A null result indicates no clients are connected. No deltas could have been applied in this state.
118+ _reporter . Verbose ( "No client is connected to ack deltas" ) ;
119+ return false ;
149120 }
150- catch ( TaskCanceledException )
121+
122+ if ( IsDeltaReceivedMessage ( result . Value ) )
151123 {
152- _reporter . Verbose ( "Timed out while waiting to verify delta was applied." ) ;
124+ // 1 indicates success.
125+ return _receiveBuffer [ 0 ] == 1 ;
153126 }
154127
155128 return false ;
0 commit comments