Skip to content

Commit 7fe1df2

Browse files
committed
refactor: delay starting background tasks
* start background task only it is needed * solve the problem that we can not use `ViewModels.Preference.Instance` until resource ready * remove avatar sever settings
1 parent e7921db commit 7fe1df2

File tree

13 files changed

+178
-190
lines changed

13 files changed

+178
-190
lines changed

src/App.axaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ private bool TryLaunchedAsAskpass(IClassicDesktopStyleApplicationLifetime deskto
528528
private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
529529
{
530530
Native.OS.SetupEnternalTools();
531+
Models.AvatarManager.Instance.Start();
532+
Models.AutoFetchManager.Instance.Start();
531533

532534
string startupRepo = null;
533535
if (desktop.Args != null && desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]))

src/Commands/Fetch.cs

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Fetch(string repo, string remote, bool prune, bool noTags, Action<string>
2626

2727
Args += remote;
2828

29-
AutoFetch.MarkFetched(repo);
29+
Models.AutoFetchManager.Instance.MarkFetched(repo);
3030
}
3131

3232
public Fetch(string repo, string remote, string localBranch, string remoteBranch, Action<string> outputHandler)
@@ -46,110 +46,4 @@ protected override void OnReadline(string line)
4646

4747
private readonly Action<string> _outputHandler;
4848
}
49-
50-
public class AutoFetch
51-
{
52-
public static bool IsEnabled
53-
{
54-
get;
55-
set;
56-
} = false;
57-
58-
public static int Interval
59-
{
60-
get => _interval;
61-
set
62-
{
63-
if (value < 1)
64-
return;
65-
_interval = value;
66-
lock (_lock)
67-
{
68-
foreach (var job in _jobs)
69-
{
70-
job.Value.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(_interval));
71-
}
72-
}
73-
}
74-
}
75-
76-
class Job
77-
{
78-
public Fetch Cmd = null;
79-
public DateTime NextRunTimepoint = DateTime.MinValue;
80-
}
81-
82-
static AutoFetch()
83-
{
84-
Task.Run(() =>
85-
{
86-
while (true)
87-
{
88-
if (!IsEnabled)
89-
{
90-
Thread.Sleep(10000);
91-
continue;
92-
}
93-
94-
var now = DateTime.Now;
95-
var uptodate = new List<Job>();
96-
lock (_lock)
97-
{
98-
foreach (var job in _jobs)
99-
{
100-
if (job.Value.NextRunTimepoint.Subtract(now).TotalSeconds <= 0)
101-
{
102-
uptodate.Add(job.Value);
103-
}
104-
}
105-
}
106-
107-
foreach (var job in uptodate)
108-
{
109-
job.Cmd.Exec();
110-
job.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
111-
}
112-
113-
Thread.Sleep(2000);
114-
}
115-
});
116-
}
117-
118-
public static void AddRepository(string repo)
119-
{
120-
var job = new Job
121-
{
122-
Cmd = new Fetch(repo, "--all", true, false, null) { RaiseError = false },
123-
NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval)),
124-
};
125-
126-
lock (_lock)
127-
{
128-
_jobs[repo] = job;
129-
}
130-
}
131-
132-
public static void RemoveRepository(string repo)
133-
{
134-
lock (_lock)
135-
{
136-
_jobs.Remove(repo);
137-
}
138-
}
139-
140-
public static void MarkFetched(string repo)
141-
{
142-
lock (_lock)
143-
{
144-
if (_jobs.TryGetValue(repo, out var value))
145-
{
146-
value.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
147-
}
148-
}
149-
}
150-
151-
private static readonly Dictionary<string, Job> _jobs = new Dictionary<string, Job>();
152-
private static readonly object _lock = new object();
153-
private static int _interval = 10;
154-
}
15549
}

src/Models/AutoFetchManager.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace SourceGit.Models
7+
{
8+
public class AutoFetchManager
9+
{
10+
public static AutoFetchManager Instance
11+
{
12+
get
13+
{
14+
if (_instance == null)
15+
_instance = new AutoFetchManager();
16+
17+
return _instance;
18+
}
19+
}
20+
21+
public class Job
22+
{
23+
public Commands.Fetch Cmd = null;
24+
public DateTime NextRunTimepoint = DateTime.MinValue;
25+
}
26+
27+
public bool IsEnabled
28+
{
29+
get;
30+
set;
31+
} = false;
32+
33+
public int Interval
34+
{
35+
get => _interval;
36+
set
37+
{
38+
_interval = Math.Max(1, value);
39+
40+
lock (_lock)
41+
{
42+
foreach (var job in _jobs)
43+
job.Value.NextRunTimepoint = DateTime.Now.AddMinutes(_interval * 1.0);
44+
}
45+
}
46+
}
47+
48+
private static AutoFetchManager _instance = null;
49+
private Dictionary<string, Job> _jobs = new Dictionary<string, Job>();
50+
private object _lock = new object();
51+
private int _interval = 10;
52+
53+
public void Start()
54+
{
55+
Task.Run(() =>
56+
{
57+
while (true)
58+
{
59+
if (!IsEnabled)
60+
{
61+
Thread.Sleep(10000);
62+
continue;
63+
}
64+
65+
var now = DateTime.Now;
66+
var uptodate = new List<Job>();
67+
lock (_lock)
68+
{
69+
foreach (var job in _jobs)
70+
{
71+
if (job.Value.NextRunTimepoint.Subtract(now).TotalSeconds <= 0)
72+
uptodate.Add(job.Value);
73+
}
74+
}
75+
76+
foreach (var job in uptodate)
77+
{
78+
job.Cmd.Exec();
79+
job.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
80+
}
81+
82+
Thread.Sleep(2000);
83+
}
84+
85+
// ReSharper disable once FunctionNeverReturns
86+
});
87+
}
88+
89+
public void AddRepository(string repo)
90+
{
91+
var job = new Job
92+
{
93+
Cmd = new Commands.Fetch(repo, "--all", true, false, null) { RaiseError = false },
94+
NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval)),
95+
};
96+
97+
lock (_lock)
98+
{
99+
_jobs[repo] = job;
100+
}
101+
}
102+
103+
public void RemoveRepository(string repo)
104+
{
105+
lock (_lock)
106+
{
107+
_jobs.Remove(repo);
108+
}
109+
}
110+
111+
public void MarkFetched(string repo)
112+
{
113+
lock (_lock)
114+
{
115+
if (_jobs.TryGetValue(repo, out var value))
116+
value.NextRunTimepoint = DateTime.Now.AddMinutes(Interval * 1.0);
117+
}
118+
}
119+
}
120+
}

src/Models/AvatarManager.cs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,31 @@ public interface IAvatarHost
2020
void OnAvatarResourceChanged(string email);
2121
}
2222

23-
public static partial class AvatarManager
23+
public partial class AvatarManager
2424
{
25-
public static string SelectedServer
25+
public static AvatarManager Instance
2626
{
27-
get;
28-
set;
29-
} = "https://www.gravatar.com/avatar/";
27+
get
28+
{
29+
if (_instance == null)
30+
_instance = new AvatarManager();
31+
32+
return _instance;
33+
}
34+
}
35+
36+
private static AvatarManager _instance = null;
37+
38+
[GeneratedRegex(@"^(?:(\d+)\+)?(.+?)@users\.noreply\.github\.com$")]
39+
private static partial Regex REG_GITHUB_USER_EMAIL();
3040

31-
static AvatarManager()
41+
private object _synclock = new object();
42+
private string _storePath;
43+
private List<IAvatarHost> _avatars = new List<IAvatarHost>();
44+
private Dictionary<string, Bitmap> _resources = new Dictionary<string, Bitmap>();
45+
private HashSet<string> _requesting = new HashSet<string>();
46+
47+
public void Start()
3248
{
3349
_storePath = Path.Combine(Native.OS.DataDir, "avatars");
3450
if (!Directory.Exists(_storePath))
@@ -62,7 +78,7 @@ static AvatarManager()
6278
var matchGithubUser = REG_GITHUB_USER_EMAIL().Match(email);
6379
var url = matchGithubUser.Success ?
6480
$"https://avatars.githubusercontent.com/{matchGithubUser.Groups[2].Value}" :
65-
$"{SelectedServer}{md5}?d=404";
81+
$"https://www.gravatar.com/avatar/{md5}?d=404";
6682

6783
var localFile = Path.Combine(_storePath, md5);
6884
var img = null as Bitmap;
@@ -105,20 +121,22 @@ static AvatarManager()
105121
NotifyResourceChanged(email);
106122
});
107123
}
124+
125+
// ReSharper disable once FunctionNeverReturns
108126
});
109127
}
110128

111-
public static void Subscribe(IAvatarHost host)
129+
public void Subscribe(IAvatarHost host)
112130
{
113131
_avatars.Add(host);
114132
}
115133

116-
public static void Unsubscribe(IAvatarHost host)
134+
public void Unsubscribe(IAvatarHost host)
117135
{
118136
_avatars.Remove(host);
119137
}
120138

121-
public static Bitmap Request(string email, bool forceRefetch)
139+
public Bitmap Request(string email, bool forceRefetch)
122140
{
123141
if (forceRefetch)
124142
{
@@ -167,7 +185,7 @@ public static Bitmap Request(string email, bool forceRefetch)
167185
return null;
168186
}
169187

170-
private static string GetEmailHash(string email)
188+
private string GetEmailHash(string email)
171189
{
172190
var lowered = email.ToLower(CultureInfo.CurrentCulture).Trim();
173191
var hash = MD5.Create().ComputeHash(Encoding.Default.GetBytes(lowered));
@@ -177,21 +195,12 @@ private static string GetEmailHash(string email)
177195
return builder.ToString();
178196
}
179197

180-
private static void NotifyResourceChanged(string email)
198+
private void NotifyResourceChanged(string email)
181199
{
182200
foreach (var avatar in _avatars)
183201
{
184202
avatar.OnAvatarResourceChanged(email);
185203
}
186204
}
187-
188-
private static readonly object _synclock = new object();
189-
private static readonly string _storePath;
190-
private static readonly List<IAvatarHost> _avatars = new List<IAvatarHost>();
191-
private static readonly Dictionary<string, Bitmap> _resources = new Dictionary<string, Bitmap>();
192-
private static readonly HashSet<string> _requesting = new HashSet<string>();
193-
194-
[GeneratedRegex(@"^(?:(\d+)\+)?(.+?)@users\.noreply\.github\.com$")]
195-
private static partial Regex REG_GITHUB_USER_EMAIL();
196205
}
197206
}

src/Resources/Locales/de_DE.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@
378378
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">Design</x:String>
379379
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">Design-Anpassungen</x:String>
380380
<x:String x:Key="Text.Preference.General" xml:space="preserve">ALLGEMEIN</x:String>
381-
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">Avatar Server</x:String>
382381
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">Beim Starten nach Updates suchen</x:String>
383382
<x:String x:Key="Text.Preference.General.Locale" xml:space="preserve">Sprache</x:String>
384383
<x:String x:Key="Text.Preference.General.MaxHistoryCommits" xml:space="preserve">Commit-Historie</x:String>

src/Resources/Locales/en_US.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@
380380
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">Theme</x:String>
381381
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">Theme Overrides</x:String>
382382
<x:String x:Key="Text.Preference.General" xml:space="preserve">GENERAL</x:String>
383-
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">Avatar Server</x:String>
384383
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">Check for updates on startup</x:String>
385384
<x:String x:Key="Text.Preference.General.Locale" xml:space="preserve">Language</x:String>
386385
<x:String x:Key="Text.Preference.General.MaxHistoryCommits" xml:space="preserve">History Commits</x:String>

src/Resources/Locales/pt_BR.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@
383383
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">Tema</x:String>
384384
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">Sobrescrever Tema</x:String>
385385
<x:String x:Key="Text.Preference.General" xml:space="preserve">GERAL</x:String>
386-
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">Servidor de Avatar</x:String>
387386
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">Verificar atualizações na inicialização</x:String>
388387
<x:String x:Key="Text.Preference.General.Locale" xml:space="preserve">Idioma</x:String>
389388
<x:String x:Key="Text.Preference.General.MaxHistoryCommits" xml:space="preserve">Commits do Histórico</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@
383383
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">主题</x:String>
384384
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">主题自定义</x:String>
385385
<x:String x:Key="Text.Preference.General" xml:space="preserve">通用配置</x:String>
386-
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">头像服务</x:String>
387386
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">启动时检测软件更新</x:String>
388387
<x:String x:Key="Text.Preference.General.Locale" xml:space="preserve">显示语言</x:String>
389388
<x:String x:Key="Text.Preference.General.MaxHistoryCommits" xml:space="preserve">最大历史提交数</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@
383383
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">主題</x:String>
384384
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">主題自訂</x:String>
385385
<x:String x:Key="Text.Preference.General" xml:space="preserve">通用配置</x:String>
386-
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">頭像服務</x:String>
387386
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">啟動時檢測軟體更新</x:String>
388387
<x:String x:Key="Text.Preference.General.Locale" xml:space="preserve">顯示語言</x:String>
389388
<x:String x:Key="Text.Preference.General.MaxHistoryCommits" xml:space="preserve">最大歷史提交數</x:String>

0 commit comments

Comments
 (0)