Skip to content

Commit e0f8024

Browse files
Add IBrowserRefreshService to project-system so it can be shared in C#DK/webtools (#9818)
* Add IBrowserRefreshService interface and update PublicAPI for hot reload functionality * Update parameter nullability in IBrowserRefreshService methods * add scope contract * Refactor IBrowserRefreshService namespace and update PublicAPI references
1 parent b2ecd5f commit e0f8024

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
2+
3+
namespace Microsoft.VisualStudio.ProjectSystem.HotReload;
4+
5+
/// <summary>
6+
/// Implements a web socket server which allows connections from js code injected in the response stream by asp.net core middleware. This
7+
/// enables sending a refresh command to the browser when code changes.
8+
/// </summary>
9+
[ProjectSystemContract(ProjectSystemContractScope.ConfiguredProject, ProjectSystemContractProvider.System, Cardinality = ImportCardinality.ExactlyOne)]
10+
public interface IBrowserRefreshService
11+
{
12+
/// <summary>
13+
/// Returns true if the current running app has browserLink injected
14+
/// </summary>
15+
bool BrowserLinkInjected { get; }
16+
17+
/// <summary>
18+
/// Starts the server if it hasn't been started yet.
19+
/// It is safe to call this multiple times.
20+
/// </summary>
21+
/// <param name="cancellationToken"></param>
22+
/// <returns></returns>
23+
ValueTask StartServerAsync(CancellationToken cancellationToken);
24+
25+
/// <summary>
26+
/// Configures the launch environment to enable browser refresh.
27+
/// Must be called after <see cref="StartServerAsync(CancellationToken)"/> is called.
28+
/// </summary>
29+
ValueTask ConfigureLaunchEnvironmentAsync(IDictionary<string, string> builder, bool enableHotReload, CancellationToken cancellationToken);
30+
31+
/// <summary>
32+
/// Sends a message to the browser to that updates were applied. This is preferable to the refresh messages since is knowledgeable about
33+
/// whether blazor is loaded in the page and will do the right thing. Note that this function throws on failure
34+
/// </summary>
35+
ValueTask RefreshBrowserAsync(CancellationToken cancellationToken);
36+
37+
/// <summary>
38+
/// Sends a ping message to all connected browsers.
39+
/// It will throw InvalidOperationException if the browser refresh server is not started.
40+
/// </summary>
41+
/// <param name="cancellationToken"></param>
42+
ValueTask SendPingMessageAsync(CancellationToken cancellationToken);
43+
44+
/// <summary>
45+
/// Sends a reload message to all connected browsers.
46+
/// It will throw InvalidOperationException if the browser refresh server is not started.
47+
/// </summary>
48+
/// <returns></returns>
49+
ValueTask SendReloadMessageAsync(CancellationToken cancellationToken);
50+
51+
/// <summary>
52+
/// Sends a wait message to all connected browsers.
53+
/// It will throw InvalidOperationException if the browser refresh server is not started.
54+
/// </summary>
55+
ValueTask SendWaitMessageAsync(CancellationToken cancellationToken);
56+
57+
/// <summary>
58+
/// Asynchronously updates the static assets referenced by the specified URLs.
59+
/// </summary>
60+
/// <remarks>If the operation is canceled via the provided cancellation token, the returned task will be
61+
/// in a canceled state. The method does not guarantee the order in which assets are updated.</remarks>
62+
/// <param name="assetUrls">A collection of URLs identifying the static assets to update. Each URL must be a valid, non-empty string.</param>
63+
/// <param name="cancellationToken">A token that can be used to cancel the update operation.</param>
64+
/// <returns>A task that represents the asynchronous update operation.</returns>
65+
ValueTask UpdateStaticAssetsAsync(IEnumerable<string> assetUrls, CancellationToken cancellationToken);
66+
}

src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/HotReload/Contracts/ICssHotReloadService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.VisualStudio.Debugger.Contracts.HotReload;
44
namespace Microsoft.VisualStudio.ProjectSystem.HotReload;
55

6+
[ProjectSystemContract(ProjectSystemContractScope.ConfiguredProject, ProjectSystemContractProvider.System, Cardinality = ImportCardinality.ExactlyOne)]
67
public interface ICssHotReloadService
78
{
89
/// <summary>

src/Microsoft.VisualStudio.ProjectSystem.Managed/PublicAPI/net472/PublicAPI.Shipped.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,13 @@ Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService
237237
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.ApplyUpdatesAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<Microsoft.VisualStudio.Debugger.Contracts.HotReload.HotReloadResult>
238238
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.CssHotReloadEnabled.get -> bool
239239
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.CssHotReloadEnabled.set -> void
240-
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.GetScopedCSSFilenameForProject() -> string!
240+
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.GetScopedCSSFilenameForProject() -> string!
241+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService
242+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.BrowserLinkInjected.get -> bool
243+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.ConfigureLaunchEnvironmentAsync(System.Collections.Generic.IDictionary<string!, string!>! builder, bool enableHotReload, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
244+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.RefreshBrowserAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
245+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.SendPingMessageAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
246+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.SendReloadMessageAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
247+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.SendWaitMessageAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
248+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.StartServerAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
249+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.UpdateStaticAssetsAsync(System.Collections.Generic.IEnumerable<string!>! assetUrls, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask

src/Microsoft.VisualStudio.ProjectSystem.Managed/PublicAPI/net9.0/PublicAPI.Shipped.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,13 @@ Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService
231231
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.ApplyUpdatesAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<Microsoft.VisualStudio.Debugger.Contracts.HotReload.HotReloadResult>
232232
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.CssHotReloadEnabled.get -> bool
233233
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.CssHotReloadEnabled.set -> void
234-
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.GetScopedCSSFilenameForProject() -> string
234+
Microsoft.VisualStudio.ProjectSystem.HotReload.ICssHotReloadService.GetScopedCSSFilenameForProject() -> string
235+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService
236+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.BrowserLinkInjected.get -> bool
237+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.ConfigureLaunchEnvironmentAsync(System.Collections.Generic.IDictionary<string, string> builder, bool enableHotReload, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
238+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.RefreshBrowserAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
239+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.SendPingMessageAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
240+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.SendReloadMessageAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
241+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.SendWaitMessageAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
242+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.StartServerAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask
243+
Microsoft.VisualStudio.ProjectSystem.HotReload.IBrowserRefreshService.UpdateStaticAssetsAsync(System.Collections.Generic.IEnumerable<string> assetUrls, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask

0 commit comments

Comments
 (0)