Skip to content

feat: removed external dependencies (VPN, mutagen) connection management #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,22 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
}, CancellationToken.None);

// Initialize file sync.
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
_ = syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(t =>
{
if (t.IsCanceled || t.Exception != null)
{
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
#if DEBUG
Debugger.Break();
#endif
}
// We're adding a 5s delay here to avoid race conditions when loading the mutagen binary.

syncSessionCts.Dispose();
}, CancellationToken.None);
_ = Task.Delay(5000).ContinueWith((_) =>
{
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(
t =>
{
if (t.IsCanceled || t.Exception != null)
{
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
}
syncSessionCts.Dispose();
}, CancellationToken.None);
});

// Prevent the TrayWindow from closing, just hide it.
var trayWindow = _services.GetRequiredService<TrayWindow>();
Expand Down
2 changes: 1 addition & 1 deletion App/Services/RpcController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void SpeakerOnError(Exception e)
Debug.WriteLine($"Error: {e}");
try
{
Reconnect(CancellationToken.None).Wait();
using var _ = Reconnect(CancellationToken.None);
}
catch
{
Expand Down
21 changes: 17 additions & 4 deletions App/ViewModels/FileSyncListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public void Initialize(Window window, DispatcherQueue dispatcherQueue)

var rpcModel = _rpcController.GetState();
var credentialModel = _credentialManager.GetCachedCredentials();
MaybeSetUnavailableMessage(rpcModel, credentialModel);
var syncSessionState = _syncSessionController.GetState();
UpdateSyncSessionState(syncSessionState);
MaybeSetUnavailableMessage(rpcModel, credentialModel, syncSessionState);
}

private void RpcControllerStateChanged(object? sender, RpcModel rpcModel)
Expand All @@ -159,7 +159,8 @@ private void RpcControllerStateChanged(object? sender, RpcModel rpcModel)
}

var credentialModel = _credentialManager.GetCachedCredentials();
MaybeSetUnavailableMessage(rpcModel, credentialModel);
var syncSessionState = _syncSessionController.GetState();
MaybeSetUnavailableMessage(rpcModel, credentialModel, syncSessionState);
}

private void CredentialManagerCredentialsChanged(object? sender, CredentialModel credentialModel)
Expand All @@ -173,7 +174,8 @@ private void CredentialManagerCredentialsChanged(object? sender, CredentialModel
}

var rpcModel = _rpcController.GetState();
MaybeSetUnavailableMessage(rpcModel, credentialModel);
var syncSessionState = _syncSessionController.GetState();
MaybeSetUnavailableMessage(rpcModel, credentialModel, syncSessionState);
}

private void SyncSessionStateChanged(object? sender, SyncSessionControllerStateModel syncSessionState)
Expand All @@ -189,7 +191,7 @@ private void SyncSessionStateChanged(object? sender, SyncSessionControllerStateM
UpdateSyncSessionState(syncSessionState);
}

private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel credentialModel)
private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel credentialModel, SyncSessionControllerStateModel syncSessionState)
{
var oldMessage = UnavailableMessage;
if (rpcModel.RpcLifecycle != RpcLifecycle.Connected)
Expand All @@ -205,6 +207,10 @@ private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel crede
{
UnavailableMessage = "Please start Coder Connect from the tray window to access file sync.";
}
else if (syncSessionState.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
{
UnavailableMessage = "Sync session controller is not initialized. Please wait...";
}
else
{
UnavailableMessage = null;
Expand All @@ -219,6 +225,13 @@ private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel crede

private void UpdateSyncSessionState(SyncSessionControllerStateModel syncSessionState)
{
// This should never happen.
if (syncSessionState == null)
return;
if (syncSessionState.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
{
MaybeSetUnavailableMessage(_rpcController.GetState(), _credentialManager.GetCachedCredentials(), syncSessionState);
}
Error = syncSessionState.DaemonError;
Sessions = syncSessionState.SyncSessions.Select(s => new SyncSessionViewModel(this, s)).ToList();
}
Expand Down
17 changes: 15 additions & 2 deletions App/ViewModels/TrayWindowDisconnectedViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Threading.Tasks;
using Coder.Desktop.App.Models;
using Coder.Desktop.App.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Threading.Tasks;

namespace Coder.Desktop.App.ViewModels;

Expand All @@ -11,6 +12,8 @@ public partial class TrayWindowDisconnectedViewModel : ObservableObject
private readonly IRpcController _rpcController;

[ObservableProperty] public partial bool ReconnectButtonEnabled { get; set; } = true;
[ObservableProperty] public partial string ErrorMessage { get; set; } = string.Empty;
[ObservableProperty] public partial bool ReconnectFailed { get; set; } = false;

public TrayWindowDisconnectedViewModel(IRpcController rpcController)
{
Expand All @@ -26,6 +29,16 @@ private void UpdateFromRpcModel(RpcModel rpcModel)
[RelayCommand]
public async Task Reconnect()
{
await _rpcController.Reconnect();
try
{
ReconnectFailed = false;
ErrorMessage = string.Empty;
await _rpcController.Reconnect();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
ReconnectFailed = true;
}
}
}
13 changes: 13 additions & 0 deletions App/Views/Pages/TrayWindowDisconnectedPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@

<controls:HorizontalRule />

<TextBlock FontWeight="semibold"
TextWrapping="Wrap"
Foreground="Red"
Visibility="{x:Bind ViewModel.ReconnectFailed, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}"
Text="Reconnect failed"/>

<TextBlock
TextWrapping="Wrap"
Margin="0,0,0,10"
Foreground="Red"
Visibility="{x:Bind ViewModel.ReconnectFailed, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}"
Text="{x:Bind ViewModel.ErrorMessage, Mode=OneWay}" />

<HyperlinkButton
HorizontalContentAlignment="Left"
HorizontalAlignment="Stretch"
Expand Down
3 changes: 1 addition & 2 deletions App/Views/TrayWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public TrayWindow(IRpcController rpcController, ICredentialManager credentialMan
private void SetPageByState(RpcModel rpcModel, CredentialModel credentialModel,
SyncSessionControllerStateModel syncSessionModel)
{
if (credentialModel.State == CredentialState.Unknown ||
syncSessionModel.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
if (credentialModel.State == CredentialState.Unknown)
{
SetRootFrame(_loadingPage);
return;
Expand Down
Loading