Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 5eb059b

Browse files
Refactored HookupHandlers to make use of the task system
HookupHandlers has will disable the repository watcher and toggle the busy flag by default. Tasks that directly cause changes to the file system should toggle the stop watcher flag. The only tasks that are called from RepositoryManager and do not directly cause changes are: - GitStatusTask - GitFetchTask - GitPushTask - GitRemoteAdd - GitRemoteRemove - GitRemoteChange - GitDeleteBranch - GitCreateBranch - GitLockTask - GitUnlockTask - GitLogTask - GitListLocksTask Tasks that are exclusive or batches of exclusive operations should toggle the busy flag. The only tasks that are called from RepositoryManager and are not exclusive are: - GitLogTask - GitListLocksTask
1 parent 134558c commit 5eb059b

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

src/GitHub.Api/Git/RepositoryManager.cs

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

67
namespace GitHub.Unity
@@ -172,52 +173,50 @@ public int WaitForEvents()
172173

173174
public ITask CommitAllFiles(string message, string body)
174175
{
175-
var task = GitClient.AddAll()
176+
var task = GitClient
177+
.AddAll()
176178
.Then(GitClient.Commit(message, body));
177179

178-
HookupHandlers(task, true);
179-
return task;
180+
return HookupHandlers(task);
180181
}
181182

182183
public ITask CommitFiles(List<string> files, string message, string body)
183184
{
184-
var task = GitClient.Add(files)
185+
var task = GitClient
186+
.Add(files)
185187
.Then(GitClient.Commit(message, body));
186188

187-
HookupHandlers(task, true);
188-
return task;
189+
return HookupHandlers(task);
189190
}
190191

191192
public ITask<List<GitLogEntry>> Log()
192193
{
193194
var task = GitClient.Log();
194-
HookupHandlers(task);
195-
return task;
195+
return HookupHandlers(task, false, false);
196196
}
197197

198198
public ITask<GitStatus> Status()
199199
{
200200
var task = GitClient.Status();
201-
HookupHandlers(task);
202-
return task;
201+
return HookupHandlers(task, false);
203202
}
204203

205204
public ITask Fetch(string remote)
206205
{
207206
var task = GitClient.Fetch(remote);
208-
return HookupHandlers(task);
207+
return HookupHandlers(task, false);
209208
}
210209

211210
public ITask Pull(string remote, string branch)
212211
{
213212
var task = GitClient.Pull(remote, branch);
214-
return HookupHandlers(task, true);
213+
return HookupHandlers(task);
215214
}
216215

217216
public ITask Push(string remote, string branch)
218217
{
219218
var task = GitClient.Push(remote, branch);
220-
return HookupHandlers(task);
219+
return HookupHandlers(task, false);
221220
}
222221

223222
public ITask Revert(string changeset)
@@ -229,7 +228,7 @@ public ITask Revert(string changeset)
229228
public ITask RemoteAdd(string remote, string url)
230229
{
231230
var task = GitClient.RemoteAdd(remote, url);
232-
HookupHandlers(task);
231+
task = HookupHandlers(task, false);
233232
if (!platform.Environment.IsWindows)
234233
{
235234
task.Then(_ => {
@@ -242,7 +241,7 @@ public ITask RemoteAdd(string remote, string url)
242241
public ITask RemoteRemove(string remote)
243242
{
244243
var task = GitClient.RemoteRemove(remote);
245-
HookupHandlers(task);
244+
task = HookupHandlers(task, false);
246245
if (!platform.Environment.IsWindows)
247246
{
248247
task.Then(_ => {
@@ -255,44 +254,44 @@ public ITask RemoteRemove(string remote)
255254
public ITask RemoteChange(string remote, string url)
256255
{
257256
var task = GitClient.RemoteChange(remote, url);
258-
return HookupHandlers(task);
257+
return HookupHandlers(task, false);
259258
}
260259

261260
public ITask SwitchBranch(string branch)
262261
{
263262
var task = GitClient.SwitchBranch(branch);
264-
return HookupHandlers(task, true);
263+
return HookupHandlers(task);
265264
}
266265

267266
public ITask DeleteBranch(string branch, bool deleteUnmerged = false)
268267
{
269268
var task = GitClient.DeleteBranch(branch, deleteUnmerged);
270-
return HookupHandlers(task);
269+
return HookupHandlers(task, false);
271270
}
272271

273272
public ITask CreateBranch(string branch, string baseBranch)
274273
{
275274
var task = GitClient.CreateBranch(branch, baseBranch);
276-
return HookupHandlers(task);
275+
return HookupHandlers(task, false);
277276
}
278277

279278
public ITask<List<GitLock>> ListLocks(bool local)
280279
{
281280
var task = GitClient.ListLocks(local);
282-
HookupHandlers(task);
281+
HookupHandlers(task, false, false);
283282
return task;
284283
}
285284

286285
public ITask LockFile(string file)
287286
{
288287
var task = GitClient.Lock(file);
289-
return HookupHandlers(task);
288+
return HookupHandlers(task, false);
290289
}
291290

292291
public ITask UnlockFile(string file, bool force)
293292
{
294293
var task = GitClient.Unlock(file, force);
295-
return HookupHandlers(task);
294+
return HookupHandlers(task, false);
296295
}
297296

298297
public void UpdateConfigData()
@@ -329,29 +328,42 @@ private void UpdateHead()
329328
UpdateCurrentBranchAndRemote(head);
330329
}
331330

332-
private ITask HookupHandlers(ITask task, bool disableWatcher = false)
331+
private ITask<T> HookupHandlers<T>(ITask<T> task, bool disableWatcher = true, bool toggleBusyFlag = true)
333332
{
334-
task.OnStart += t => {
335-
Logger.Trace("Start " + task.Name);
336-
IsBusy = true;
333+
return new ActionTask(CancellationToken.None, () => {
334+
if (toggleBusyFlag)
335+
{
336+
Logger.Trace("Starting Operation - Setting Busy Flag");
337+
IsBusy = true;
338+
}
337339

338-
if (disableWatcher)
339-
{
340-
watcher.Stop();
341-
}
342-
};
340+
if (disableWatcher)
341+
{
342+
Logger.Trace("Starting Operation - Disable Watcher");
343+
watcher.Stop();
344+
}
345+
})
346+
.Then(task)
347+
.Finally((success, exception, result) => {
348+
if (disableWatcher)
349+
{
350+
Logger.Trace("Ended Operation - Enable Watcher");
351+
watcher.Start();
352+
}
343353

344-
task.OnEnd += t => {
345-
if (disableWatcher)
346-
{
347-
watcher.Start();
348-
}
354+
if (toggleBusyFlag)
355+
{
356+
Logger.Trace("Ended Operation - Clearing Busy Flag");
357+
IsBusy = false;
358+
}
349359

350-
IsBusy = false;
360+
if (success)
361+
{
362+
return result;
363+
}
351364

352-
Logger.Trace("Finish " + task.Name);
353-
};
354-
return task;
365+
throw exception;
366+
});
355367
}
356368

357369
private void Watcher_OnRemoteBranchDeleted(string remote, string name)

0 commit comments

Comments
 (0)