Skip to content

Commit b412c9a

Browse files
committed
enhance: cancel updating repository status when Welcome page is unloaded
Signed-off-by: leo <longshuang@msn.cn>
1 parent 02ecd58 commit b412c9a

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

src/ViewModels/RepositoryNode.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Text.Json.Serialization;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using CommunityToolkit.Mvvm.ComponentModel;
78

@@ -130,16 +131,19 @@ public void Delete()
130131
activePage.Popup = new DeleteRepositoryNode(this);
131132
}
132133

133-
public async Task UpdateStatusAsync(bool force)
134+
public async Task UpdateStatusAsync(bool force, CancellationToken? token)
134135
{
136+
if (token is { IsCancellationRequested: true })
137+
return;
138+
135139
if (!_isRepository || !Directory.Exists(_id))
136140
{
137141
Status = null;
138142

139143
if (SubNodes.Count > 0)
140144
{
141145
foreach (var subNode in SubNodes)
142-
await subNode.UpdateStatusAsync(force);
146+
await subNode.UpdateStatusAsync(force, token);
143147
}
144148

145149
return;

src/ViewModels/Welcome.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
using Avalonia.Collections;
@@ -52,10 +53,10 @@ public void Refresh()
5253
Rows.AddRange(rows);
5354
}
5455

55-
public async Task UpdateStatusAsync(bool force)
56+
public async Task UpdateStatusAsync(bool force, CancellationToken? token)
5657
{
5758
foreach (var node in Preferences.Instance.RepositoryNodes)
58-
await node.UpdateStatusAsync(force);
59+
await node.UpdateStatusAsync(force, token);
5960
}
6061

6162
public void ToggleNodeIsExpanded(RepositoryNode node)

src/Views/Launcher.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected override async void OnKeyDown(KeyEventArgs e)
269269
else if (vm.ActivePage.Data is ViewModels.Welcome welcome)
270270
{
271271
e.Handled = true;
272-
await welcome.UpdateStatusAsync(true);
272+
await welcome.UpdateStatusAsync(true, null);
273273
return;
274274
}
275275
}

src/Views/Welcome.axaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using Avalonia;
34
using Avalonia.Controls;
45
using Avalonia.Controls.Primitives;
@@ -64,7 +65,13 @@ public Welcome()
6465
protected override async void OnLoaded(RoutedEventArgs e)
6566
{
6667
base.OnLoaded(e);
67-
await ViewModels.Welcome.Instance.UpdateStatusAsync(false);
68+
await ViewModels.Welcome.Instance.UpdateStatusAsync(false, _cancellation.Token);
69+
}
70+
71+
protected override void OnUnloaded(RoutedEventArgs e)
72+
{
73+
_cancellation.Cancel();
74+
base.OnUnloaded(e);
6875
}
6976

7077
protected override void OnKeyDown(KeyEventArgs e)
@@ -361,5 +368,6 @@ private void OnDoubleTappedTreeNode(object sender, TappedEventArgs e)
361368
private Point _pressedTreeNodePosition = new Point();
362369
private bool _startDragTreeNode = false;
363370
private readonly DataFormat<string> _dndRepoNode = DataFormat.CreateStringApplicationFormat("sourcegit-dnd-repo-node");
371+
private CancellationTokenSource _cancellation = new CancellationTokenSource();
364372
}
365373
}

0 commit comments

Comments
 (0)