Skip to content

Commit

Permalink
chore: change OnMessageReceivedAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-rosa-knowcode committed Oct 17, 2024
1 parent bc8c8d2 commit 85e5d1e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
34 changes: 31 additions & 3 deletions src/Uno.UI.RemoteControl.VS/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,7 @@ private async Task EnsureServerAsync()
_process.BeginErrorReadLine();

_ideChannelClient = new IdeChannelClient(pipeGuid, new Logger(this));
_ideChannelClient.ForceHotReloadRequested += OnForceHotReloadRequestedAsync;
_ideChannelClient.OnMessageReceived += OnMessageReceivedAsync;
_ideChannelClient.OnAddMenuItemIdeMessageRequested += OnAddMenuItemRequestIdeMessageAsync;
_ideChannelClient.ConnectToHost();

// Set the port to the projects
Expand Down Expand Up @@ -422,7 +420,37 @@ async Task RestartAsync()
}
}

private async Task OnMessageReceivedAsync(object? sender, NotificationRequestIdeMessage message)
private async Task OnMessageReceivedAsync(object? sender, IdeMessage devServerMessage)
{
try
{
switch (devServerMessage)
{
case KeepAliveIdeMessage:
_debugAction?.Invoke($"Keep alive from Dev Server");
break;
case AddMenuItemRequestIdeMessage amir:
await OnAddMenuItemRequestIdeMessageAsync(sender, amir);
break;
case ForceHotReloadIdeMessage fhr:
await OnForceHotReloadRequestedAsync(sender, fhr);
break;
case NotificationRequestIdeMessage nr:
await NotificationRequestIdeMessageAsync(sender, nr);
break;
default:
_debugAction?.Invoke($"Unknown message type {devServerMessage?.GetType()} from DevServer");
break;
}
}
catch (Exception e) when (_ideChannelClient is not null)
{
_debugAction?.Invoke($"Failed to handle IdeMessage with message {e.Message}");
throw;
}
}

private async Task NotificationRequestIdeMessageAsync(object? sender, NotificationRequestIdeMessage message)
{
try
{
Expand Down
31 changes: 8 additions & 23 deletions src/Uno.UI.RemoteControl.VS/IDEChannel/IDEChannelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ internal class IdeChannelClient
private IIdeChannelServer? _devServer;
private readonly ILogger _logger;

public event AsyncEventHandler<ForceHotReloadIdeMessage>? ForceHotReloadRequested;
public event AsyncEventHandler<NotificationRequestIdeMessage>? OnMessageReceived;
public event AsyncEventHandler<AddMenuItemRequestIdeMessage>? OnAddMenuItemIdeMessageRequested;
public event AsyncEventHandler<IdeMessage>? OnMessageReceived;

public IdeChannelClient(Guid pipeGuid, ILogger logger)
{
Expand Down Expand Up @@ -91,27 +89,14 @@ private void ProcessDevServerMessage(object sender, IdeMessageEnvelope devServer
_logger.Verbose($"IDE: IDEChannel message received {devServerMessage}");

var process = Task.CompletedTask;
switch (devServerMessage)
if (devServerMessage is IdeMessage message)
{
case AddMenuItemRequestIdeMessage cr:
_logger.Debug("Command Ide Message Requested");
process = OnAddMenuItemIdeMessageRequested.InvokeAsync(this, cr);
break;

case ForceHotReloadIdeMessage forceHotReloadMessage when ForceHotReloadRequested is { } hrRequested:
_logger.Debug("Hot reload requested");
process = hrRequested.InvokeAsync(this, forceHotReloadMessage);
break;
case KeepAliveIdeMessage:
_logger.Verbose($"Keep alive from Dev Server");
break;
case NotificationRequestIdeMessage e when e is { } message:
_logger.Verbose($"Dev Server will open the Notification Message with message {e.Message}");
process = OnMessageReceived.InvokeAsync(this, message);
break;
default:
_logger.Verbose($"Unknown message type {devServerMessage?.GetType()} from DevServer");
break;
_logger.Verbose($"Dev Server Message {message.GetType()} requested");
process = OnMessageReceived.InvokeAsync(this, message);
}
else
{
_logger.Verbose($"Unknown message type {devServerMessage?.GetType()} from DevServer");
}

_ = process.ContinueWith(
Expand Down

0 comments on commit 85e5d1e

Please sign in to comment.