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

Commit e492458

Browse files
committed
Fix start/end handlers and tweak Finally callback
It's useful to have the success of the state in the Finally handler that is called in-thread (the ITask-based Finally handlers already get this information), so add that to the callback. In order for the API not to be confusing, make the task affinity parameter mandatory on the Finally overloads that run on a separate thread. Fix the HookupHandlers method to use OnStart/Finally so that the callbacks get called on the same thread and not spin up new threads just to set flags. They're already running on non-main threads and doing very simple things so this should be fine.
1 parent e24a878 commit e492458

File tree

5 files changed

+176
-107
lines changed

5 files changed

+176
-107
lines changed

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public ITask CommitAllFiles(string message, string body)
183183
.AddAll()
184184
.Then(GitClient.Commit(message, body));
185185

186-
return HookupHandlers(task, true, true);
186+
return HookupHandlers(task, true);
187187
}
188188

189189
public ITask CommitFiles(List<string> files, string message, string body)
@@ -192,79 +192,79 @@ public ITask CommitFiles(List<string> files, string message, string body)
192192
.Add(files)
193193
.Then(GitClient.Commit(message, body));
194194

195-
return HookupHandlers(task, true, true);
195+
return HookupHandlers(task, true);
196196
}
197197

198198
public ITask Fetch(string remote)
199199
{
200200
var task = GitClient.Fetch(remote);
201-
return HookupHandlers(task, true, false);
201+
return HookupHandlers(task, false);
202202
}
203203

204204
public ITask Pull(string remote, string branch)
205205
{
206206
var task = GitClient.Pull(remote, branch);
207-
return HookupHandlers(task, true, true);
207+
return HookupHandlers(task, true);
208208
}
209209

210210
public ITask Push(string remote, string branch)
211211
{
212212
var task = GitClient.Push(remote, branch);
213-
return HookupHandlers(task, true, false);
213+
return HookupHandlers(task, false);
214214
}
215215

216216
public ITask Revert(string changeset)
217217
{
218218
var task = GitClient.Revert(changeset);
219-
return HookupHandlers(task, true, true);
219+
return HookupHandlers(task, true);
220220
}
221221

222222
public ITask RemoteAdd(string remote, string url)
223223
{
224224
var task = GitClient.RemoteAdd(remote, url);
225-
return HookupHandlers(task, true, false);
225+
return HookupHandlers(task, false);
226226
}
227227

228228
public ITask RemoteRemove(string remote)
229229
{
230230
var task = GitClient.RemoteRemove(remote);
231-
return HookupHandlers(task, true, false);
231+
return HookupHandlers(task, false);
232232
}
233233

234234
public ITask RemoteChange(string remote, string url)
235235
{
236236
var task = GitClient.RemoteChange(remote, url);
237-
return HookupHandlers(task, true, false);
237+
return HookupHandlers(task, false);
238238
}
239239

240240
public ITask SwitchBranch(string branch)
241241
{
242242
var task = GitClient.SwitchBranch(branch);
243-
return HookupHandlers(task, true, true);
243+
return HookupHandlers(task, true);
244244
}
245245

246246
public ITask DeleteBranch(string branch, bool deleteUnmerged = false)
247247
{
248248
var task = GitClient.DeleteBranch(branch, deleteUnmerged);
249-
return HookupHandlers(task, true, false);
249+
return HookupHandlers(task, false);
250250
}
251251

252252
public ITask CreateBranch(string branch, string baseBranch)
253253
{
254254
var task = GitClient.CreateBranch(branch, baseBranch);
255-
return HookupHandlers(task, true, false);
255+
return HookupHandlers(task, false);
256256
}
257257

258258
public ITask LockFile(string file)
259259
{
260260
var task = GitClient.Lock(file);
261-
return HookupHandlers(task, true, false).Then(UpdateLocks);
261+
return HookupHandlers(task, false).Then(UpdateLocks);
262262
}
263263

264264
public ITask UnlockFile(string file, bool force)
265265
{
266266
var task = GitClient.Unlock(file, force);
267-
return HookupHandlers(task, true, false).Then(UpdateLocks);
267+
return HookupHandlers(task, false).Then(UpdateLocks);
268268
}
269269

270270
public void UpdateGitLog()
@@ -278,7 +278,7 @@ public void UpdateGitLog()
278278
GitLogUpdated?.Invoke(logEntries);
279279
}
280280
});
281-
task = HookupHandlers(task, false, false);
281+
task = HookupHandlers(task, false);
282282
task.Start();
283283
}
284284

@@ -293,7 +293,7 @@ public void UpdateGitStatus()
293293
GitStatusUpdated?.Invoke(status);
294294
}
295295
});
296-
task = HookupHandlers(task, true, false);
296+
task = HookupHandlers(task, false);
297297
task.Start();
298298
}
299299

@@ -331,13 +331,21 @@ public ITask DiscardChanges(GitStatusEntry[] gitStatusEntries)
331331
if (itemsToRevert.Any())
332332
{
333333
gitDiscardTask = GitClient.Discard(itemsToRevert);
334-
task.Then(gitDiscardTask);
334+
task
335+
.Then(gitDiscardTask)
336+
// we're appending a new continuation, we need to reset the finally handler
337+
// so it runs after the discard task
338+
.Finally(s =>
339+
{
340+
watcher.Start();
341+
isBusy = false;
342+
});
335343
}
336344
}
337345
, () => gitStatusEntries);
338346

339347

340-
return HookupHandlers(task, true, true);
348+
return HookupHandlers(task, true);
341349
}
342350

343351
public void UpdateGitAheadBehindStatus()
@@ -351,16 +359,15 @@ public void UpdateGitAheadBehindStatus()
351359
var name = configBranch.Value.Name;
352360
var trackingName = configBranch.Value.IsTracking ? configBranch.Value.Remote.Value.Name + "/" + name : "[None]";
353361

354-
var task = GitClient
355-
.AheadBehindStatus(name, trackingName)
362+
var task = GitClient.AheadBehindStatus(name, trackingName)
356363
.Then((success, status) =>
357364
{
358365
if (success)
359366
{
360367
GitAheadBehindStatusUpdated?.Invoke(status);
361368
}
362369
});
363-
task = HookupHandlers(task, true, false);
370+
task = HookupHandlers(task, false);
364371
task.Start();
365372
}
366373
else
@@ -371,51 +378,55 @@ public void UpdateGitAheadBehindStatus()
371378

372379
public void UpdateLocks()
373380
{
374-
var task = GitClient.ListLocks(false);
375-
HookupHandlers(task, false, false);
376-
task.Then((success, locks) =>
377-
{
378-
if (success)
381+
GitClient.ListLocks(false)
382+
.Finally((success, locks) =>
379383
{
380-
GitLocksUpdated?.Invoke(locks);
381-
}
382-
}).Start();
384+
if (success)
385+
{
386+
GitLocksUpdated?.Invoke(locks);
387+
}
388+
})
389+
.Start();
383390
}
384391

385-
private ITask HookupHandlers(ITask task, bool isExclusive, bool filesystemChangesExpected)
392+
private ITask<T> HookupHandlers<T>(ITask<T> task, bool filesystemChangesExpected)
386393
{
387-
return new ActionTask(token, () => {
388-
if (isExclusive)
389-
{
390-
Logger.Trace("Starting Operation - Setting Busy Flag");
391-
IsBusy = true;
392-
}
394+
return (ITask<T>)HookupHandlers((ITask)task, filesystemChangesExpected);
395+
}
393396

394-
if (filesystemChangesExpected)
395-
{
396-
Logger.Trace("Starting Operation - Disable Watcher");
397-
watcher.Stop();
398-
}
399-
})
400-
.Then(task)
401-
.Finally((success, exception) => {
402-
if (filesystemChangesExpected)
403-
{
404-
Logger.Trace("Ended Operation - Enable Watcher");
405-
watcher.Start();
406-
}
397+
private ITask HookupHandlers(ITask task, bool filesystemChangesExpected)
398+
{
399+
var isExclusive = task.IsChainExclusive();
400+
task.GetTopOfChain().OnStart += t =>
401+
{
402+
if (t.Affinity == TaskAffinity.Exclusive)
403+
{
404+
Logger.Trace("Starting Operation - Setting Busy Flag");
405+
IsBusy = true;
406+
}
407407

408-
if (isExclusive)
409-
{
410-
Logger.Trace("Ended Operation - Clearing Busy Flag");
411-
IsBusy = false;
412-
}
408+
if (filesystemChangesExpected)
409+
{
410+
Logger.Trace("Starting Operation - Disable Watcher");
411+
watcher.Stop();
412+
}
413+
};
413414

414-
if (!success)
415-
{
416-
throw exception;
417-
}
418-
});
415+
task.Finally(success =>
416+
{
417+
if (filesystemChangesExpected)
418+
{
419+
Logger.Trace("Ended Operation - Enable Watcher");
420+
watcher.Start();
421+
}
422+
423+
if (isExclusive)
424+
{
425+
Logger.Trace("Ended Operation - Clearing Busy Flag");
426+
IsBusy = false;
427+
}
428+
});
429+
return task;
419430
}
420431

421432
private void SetupWatcher()

0 commit comments

Comments
 (0)