Skip to content

Commit 774ca8e

Browse files
committed
feat: removed blocking wait for reconnect, mutagen initializing improvements
1 parent 22c9bcd commit 774ca8e

File tree

7 files changed

+59
-19
lines changed

7 files changed

+59
-19
lines changed

App/App.xaml.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,21 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
165165
}, CancellationToken.None);
166166

167167
// Initialize file sync.
168-
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
169-
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
170-
_ = syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(t =>
171-
{
172-
if (t.IsCanceled || t.Exception != null)
173-
{
174-
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
175-
#if DEBUG
176-
Debugger.Break();
177-
#endif
178-
}
179168

180-
syncSessionCts.Dispose();
181-
}, CancellationToken.None);
169+
_ = Task.Delay(20000).ContinueWith((_) =>
170+
{
171+
var syncSessionCts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
172+
var syncSessionController = _services.GetRequiredService<ISyncSessionController>();
173+
syncSessionController.RefreshState(syncSessionCts.Token).ContinueWith(
174+
t =>
175+
{
176+
if (t.IsCanceled || t.Exception != null)
177+
{
178+
_logger.LogError(t.Exception, "failed to refresh sync state (canceled = {canceled})", t.IsCanceled);
179+
}
180+
syncSessionCts.Dispose();
181+
}, CancellationToken.None);
182+
});
182183

183184
// Prevent the TrayWindow from closing, just hide it.
184185
var trayWindow = _services.GetRequiredService<TrayWindow>();

App/Services/RpcController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private void SpeakerOnError(Exception e)
313313
Debug.WriteLine($"Error: {e}");
314314
try
315315
{
316-
Reconnect(CancellationToken.None).Wait();
316+
using var _ = Reconnect(CancellationToken.None);
317317
}
318318
catch
319319
{

App/ViewModels/FileSyncListViewModel.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private void SyncSessionStateChanged(object? sender, SyncSessionControllerStateM
189189
UpdateSyncSessionState(syncSessionState);
190190
}
191191

192-
private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel credentialModel)
192+
private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel credentialModel, SyncSessionControllerStateModel? syncSessionState = null)
193193
{
194194
var oldMessage = UnavailableMessage;
195195
if (rpcModel.RpcLifecycle != RpcLifecycle.Connected)
@@ -204,6 +204,9 @@ private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel crede
204204
else if (rpcModel.VpnLifecycle != VpnLifecycle.Started)
205205
{
206206
UnavailableMessage = "Please start Coder Connect from the tray window to access file sync.";
207+
} else if(syncSessionState != null && syncSessionState.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
208+
{
209+
UnavailableMessage = "Sync session controller is not initialized. Please wait...";
207210
}
208211
else
209212
{
@@ -219,6 +222,13 @@ private void MaybeSetUnavailableMessage(RpcModel rpcModel, CredentialModel crede
219222

220223
private void UpdateSyncSessionState(SyncSessionControllerStateModel syncSessionState)
221224
{
225+
// This should never happen.
226+
if (syncSessionState == null)
227+
return;
228+
if (syncSessionState.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
229+
{
230+
MaybeSetUnavailableMessage(_rpcController.GetState(), _credentialManager.GetCachedCredentials(), syncSessionState);
231+
}
222232
Error = syncSessionState.DaemonError;
223233
Sessions = syncSessionState.SyncSessions.Select(s => new SyncSessionViewModel(this, s)).ToList();
224234
}

App/ViewModels/TrayWindowDisconnectedViewModel.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
using System.Threading.Tasks;
21
using Coder.Desktop.App.Models;
32
using Coder.Desktop.App.Services;
3+
using Coder.Desktop.App.Views.Pages;
44
using CommunityToolkit.Mvvm.ComponentModel;
55
using CommunityToolkit.Mvvm.Input;
6+
using Microsoft.UI.Xaml;
7+
using Microsoft.UI.Xaml.Controls;
8+
using System;
9+
using System.Diagnostics;
10+
using System.Threading.Tasks;
611

712
namespace Coder.Desktop.App.ViewModels;
813

@@ -11,6 +16,8 @@ public partial class TrayWindowDisconnectedViewModel : ObservableObject
1116
private readonly IRpcController _rpcController;
1217

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

1522
public TrayWindowDisconnectedViewModel(IRpcController rpcController)
1623
{
@@ -26,6 +33,16 @@ private void UpdateFromRpcModel(RpcModel rpcModel)
2633
[RelayCommand]
2734
public async Task Reconnect()
2835
{
29-
await _rpcController.Reconnect();
36+
try
37+
{
38+
ReconnectFailed = false;
39+
ErrorMessage = string.Empty;
40+
await _rpcController.Reconnect();
41+
}
42+
catch (Exception ex)
43+
{
44+
ErrorMessage = ex.Message;
45+
ReconnectFailed = true;
46+
}
3047
}
3148
}

App/Views/Pages/TrayWindowDisconnectedPage.xaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030

3131
<controls:HorizontalRule />
3232

33+
<TextBlock TextWrapping="Wrap" Foreground="Red" Visibility="{x:Bind ViewModel.ReconnectFailed, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}">
34+
<Bold>Reconnect failed</Bold>
35+
</TextBlock>
36+
37+
<TextBlock
38+
TextWrapping="Wrap"
39+
Margin="0,0,0,10"
40+
Foreground="Red"
41+
Visibility="{x:Bind ViewModel.ReconnectFailed, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}"
42+
Text="{x:Bind ViewModel.ErrorMessage, Mode=OneWay}" />
43+
3344
<HyperlinkButton
3445
HorizontalContentAlignment="Left"
3546
HorizontalAlignment="Stretch"

App/Views/Pages/TrayWindowDisconnectedPage.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Coder.Desktop.App.ViewModels;
2+
using Microsoft.UI.Xaml;
23
using Microsoft.UI.Xaml.Controls;
4+
using System;
35

46
namespace Coder.Desktop.App.Views.Pages;
57

App/Views/TrayWindow.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ public TrayWindow(IRpcController rpcController, ICredentialManager credentialMan
122122
private void SetPageByState(RpcModel rpcModel, CredentialModel credentialModel,
123123
SyncSessionControllerStateModel syncSessionModel)
124124
{
125-
if (credentialModel.State == CredentialState.Unknown ||
126-
syncSessionModel.Lifecycle == SyncSessionControllerLifecycle.Uninitialized)
125+
if (credentialModel.State == CredentialState.Unknown)
127126
{
128127
SetRootFrame(_loadingPage);
129128
return;

0 commit comments

Comments
 (0)