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

Commit ecc06c0

Browse files
committed
Invalidate caches as soon as we know we need to run commands
Unity might reload the domain before we're finished running commands, which means any data refresh coming from the file watcher might get lost and the cache never updated with it. This tries triggering the data updates by invalidating the cache, so that if Unity reloads we'll notice that the cache needs refreshing and issue the commands again as part of the normal cache validation process.
1 parent c5e9d8a commit ecc06c0

File tree

4 files changed

+151
-226
lines changed

4 files changed

+151
-226
lines changed

src/GitHub.Api/Cache/CacheInterfaces.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace GitHub.Unity
55
{
66
public enum CacheType
77
{
8-
RepositoryInfoCache,
9-
BranchCache,
10-
GitLogCache,
11-
GitTrackingStatusCache,
12-
GitStatusEntriesCache,
13-
GitLocksCache,
14-
GitUserCache
8+
RepositoryInfo,
9+
Branches,
10+
GitLog,
11+
GitAheadBehind,
12+
GitStatus,
13+
GitLocks,
14+
GitUser
1515
}
1616

1717
public interface ICacheContainer

src/GitHub.Api/Git/Repository.cs

Lines changed: 44 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void Initialize(IRepositoryManager initRepositoryManager)
5757
repositoryManager.GitLocksUpdated += RepositoryManagerOnGitLocksUpdated;
5858
repositoryManager.LocalBranchesUpdated += RepositoryManagerOnLocalBranchesUpdated;
5959
repositoryManager.RemoteBranchesUpdated += RepositoryManagerOnRemoteBranchesUpdated;
60+
repositoryManager.DataNeedsRefreshing += InvalidateCache;
6061
}
6162

6263
public void Start()
@@ -296,29 +297,31 @@ private void InvalidateCache(CacheType cacheType)
296297

297298
switch (cacheType)
298299
{
299-
case CacheType.BranchCache:
300+
case CacheType.Branches:
301+
repositoryManager?.UpdateBranches();
300302
break;
301303

302-
case CacheType.GitLogCache:
304+
case CacheType.GitLog:
303305
repositoryManager?.UpdateGitLog();
304306
break;
305307

306-
case CacheType.GitTrackingStatusCache:
308+
case CacheType.GitAheadBehind:
307309
repositoryManager?.UpdateGitAheadBehindStatus();
308310
break;
309311

310-
case CacheType.GitLocksCache:
311-
UpdateLocks();
312+
case CacheType.GitLocks:
313+
if (CurrentRemote != null)
314+
repositoryManager?.UpdateLocks();
312315
break;
313316

314-
case CacheType.GitUserCache:
317+
case CacheType.GitUser:
315318
break;
316319

317-
case CacheType.RepositoryInfoCache:
320+
case CacheType.RepositoryInfo:
318321
repositoryManager?.UpdateRepositoryInfo();
319322
break;
320323

321-
case CacheType.GitStatusEntriesCache:
324+
case CacheType.GitStatus:
322325
repositoryManager?.UpdateGitStatus();
323326
break;
324327

@@ -332,30 +335,30 @@ private void CacheContainer_OnCacheUpdated(CacheType cacheType, DateTimeOffset o
332335
var cacheUpdateEvent = new CacheUpdateEvent { UpdatedTime = offset };
333336
switch (cacheType)
334337
{
335-
case CacheType.BranchCache:
338+
case CacheType.Branches:
336339
HandleBranchCacheUpdatedEvent(cacheUpdateEvent);
337340
break;
338341

339-
case CacheType.GitLogCache:
342+
case CacheType.GitLog:
340343
HandleGitLogCacheUpdatedEvent(cacheUpdateEvent);
341344
break;
342345

343-
case CacheType.GitTrackingStatusCache:
346+
case CacheType.GitAheadBehind:
344347
HandleGitTrackingStatusCacheUpdatedEvent(cacheUpdateEvent);
345348
break;
346349

347-
case CacheType.GitLocksCache:
350+
case CacheType.GitLocks:
348351
HandleGitLocksCacheUpdatedEvent(cacheUpdateEvent);
349352
break;
350353

351-
case CacheType.GitUserCache:
354+
case CacheType.GitUser:
352355
break;
353356

354-
case CacheType.RepositoryInfoCache:
357+
case CacheType.RepositoryInfo:
355358
HandleRepositoryInfoCacheUpdatedEvent(cacheUpdateEvent);
356359
break;
357360

358-
case CacheType.GitStatusEntriesCache:
361+
case CacheType.GitStatus:
359362
HandleGitStatusEntriesCacheUpdatedEvent(cacheUpdateEvent);
360363
break;
361364

@@ -411,15 +414,15 @@ private void RepositoryManagerOnCurrentBranchUpdated(ConfigBranch? branch, Confi
411414
{
412415
var currentBranch = branch != null ? (GitBranch?)GetLocalGitBranch(branch.Value) : null;
413416

414-
CurrentConfigBranch = branch;
415-
CurrentBranch = currentBranch;
417+
cacheContainer.BranchCache.CurrentConfigBranch = branch;
418+
cacheContainer.RepositoryInfoCache.CurrentGitBranch = currentBranch;
416419
UpdateLocalBranches();
417420
}
418421

419422
if (!Nullable.Equals(CurrentConfigRemote, remote))
420423
{
421-
CurrentConfigRemote = remote;
422-
CurrentRemote = remote.HasValue ? (GitRemote?)GetGitRemote(remote.Value) : null;
424+
cacheContainer.BranchCache.CurrentConfigRemote = remote;
425+
cacheContainer.RepositoryInfoCache.CurrentGitRemote = remote.HasValue ? (GitRemote?)GetGitRemote(remote.Value) : null;
423426
ClearRepositoryInfo();
424427
}
425428
}) { Affinity = TaskAffinity.UI }.Start();
@@ -428,31 +431,31 @@ private void RepositoryManagerOnCurrentBranchUpdated(ConfigBranch? branch, Confi
428431
private void RepositoryManagerOnGitStatusUpdated(GitStatus gitStatus)
429432
{
430433
new ActionTask(TaskManager.Instance.Token, () => {
431-
CurrentChanges = gitStatus.Entries;
432-
CurrentAhead = gitStatus.Ahead;
433-
CurrentBehind = gitStatus.Behind;
434+
cacheContainer.GitStatusEntriesCache.Entries = gitStatus.Entries;
435+
cacheContainer.GitTrackingStatusCache.Ahead = gitStatus.Ahead;
436+
cacheContainer.GitTrackingStatusCache.Behind = gitStatus.Behind;
434437
}) { Affinity = TaskAffinity.UI }.Start();
435438
}
436439

437440
private void RepositoryManagerOnGitAheadBehindStatusUpdated(GitAheadBehindStatus aheadBehindStatus)
438441
{
439442
new ActionTask(TaskManager.Instance.Token, () => {
440-
CurrentAhead = aheadBehindStatus.Ahead;
441-
CurrentBehind = aheadBehindStatus.Behind;
443+
cacheContainer.GitTrackingStatusCache.Ahead = aheadBehindStatus.Ahead;
444+
cacheContainer.GitTrackingStatusCache.Behind = aheadBehindStatus.Behind;
442445
}) { Affinity = TaskAffinity.UI }.Start();
443446
}
444447

445448
private void RepositoryManagerOnGitLogUpdated(List<GitLogEntry> gitLogEntries)
446449
{
447450
new ActionTask(TaskManager.Instance.Token, () => {
448-
CurrentLog = gitLogEntries;
451+
cacheContainer.GitLogCache.Log = gitLogEntries;
449452
}) { Affinity = TaskAffinity.UI }.Start();
450453
}
451454

452455
private void RepositoryManagerOnGitLocksUpdated(List<GitLock> gitLocks)
453456
{
454457
new ActionTask(TaskManager.Instance.Token, () => {
455-
CurrentLocks = gitLocks;
458+
cacheContainer.GitLocksCache.GitLocks = gitLocks;
456459
})
457460
{ Affinity = TaskAffinity.UI }.Start();
458461
}
@@ -462,8 +465,8 @@ private void RepositoryManagerOnRemoteBranchesUpdated(Dictionary<string, ConfigR
462465
{
463466
new ActionTask(TaskManager.Instance.Token, () => {
464467
cacheContainer.BranchCache.SetRemotes(remotes, branches);
465-
Remotes = ConfigRemotes.Values.Select(GetGitRemote).ToArray();
466-
RemoteBranches = RemoteConfigBranches.Values.SelectMany(x => x.Values).Select(GetRemoteGitBranch).ToArray();
468+
cacheContainer.BranchCache.Remotes = ConfigRemotes.Values.Select(GetGitRemote).ToArray();
469+
cacheContainer.BranchCache.RemoteBranches = RemoteConfigBranches.Values.SelectMany(x => x.Values).Select(GetRemoteGitBranch).ToArray();
467470
}) { Affinity = TaskAffinity.UI }.Start();
468471
}
469472

@@ -475,17 +478,9 @@ private void RepositoryManagerOnLocalBranchesUpdated(Dictionary<string, ConfigBr
475478
}) { Affinity = TaskAffinity.UI }.Start();
476479
}
477480

478-
private void UpdateLocks()
479-
{
480-
if (CurrentRemote.HasValue)
481-
{
482-
repositoryManager?.UpdateLocks();
483-
}
484-
}
485-
486481
private void UpdateLocalBranches()
487482
{
488-
LocalBranches = LocalConfigBranches.Values.Select(GetLocalGitBranch).ToArray();
483+
cacheContainer.BranchCache.LocalBranches = LocalConfigBranches.Values.Select(GetLocalGitBranch).ToArray();
489484
}
490485

491486
private void ClearRepositoryInfo()
@@ -521,80 +516,20 @@ private static GitRemote GetGitRemote(ConfigRemote configRemote)
521516

522517
private ILocalConfigBranchDictionary LocalConfigBranches => cacheContainer.BranchCache.LocalConfigBranches;
523518

524-
public GitRemote[] Remotes
525-
{
526-
get { return cacheContainer.BranchCache.Remotes; }
527-
private set { cacheContainer.BranchCache.Remotes = value; }
528-
}
529-
530-
public GitBranch[] LocalBranches
531-
{
532-
get { return cacheContainer.BranchCache.LocalBranches; }
533-
private set { cacheContainer.BranchCache.LocalBranches = value; }
534-
}
535-
536-
public GitBranch[] RemoteBranches
537-
{
538-
get { return cacheContainer.BranchCache.RemoteBranches; }
539-
private set { cacheContainer.BranchCache.RemoteBranches = value; }
540-
}
541-
542-
private ConfigBranch? CurrentConfigBranch
543-
{
544-
get { return this.cacheContainer.BranchCache.CurrentConfigBranch; }
545-
set { cacheContainer.BranchCache.CurrentConfigBranch = value; }
546-
}
547-
548-
private ConfigRemote? CurrentConfigRemote
549-
{
550-
get { return this.cacheContainer.BranchCache.CurrentConfigRemote; }
551-
set { cacheContainer.BranchCache.CurrentConfigRemote = value; }
552-
}
553-
554-
public int CurrentAhead
555-
{
556-
get { return cacheContainer.GitTrackingStatusCache.Ahead; }
557-
private set { cacheContainer.GitTrackingStatusCache.Ahead = value; }
558-
}
559-
560-
public int CurrentBehind
561-
{
562-
get { return cacheContainer.GitTrackingStatusCache.Behind; }
563-
private set { cacheContainer.GitTrackingStatusCache.Behind = value; }
564-
}
565-
566-
public List<GitStatusEntry> CurrentChanges
567-
{
568-
get { return cacheContainer.GitStatusEntriesCache.Entries; }
569-
private set { cacheContainer.GitStatusEntriesCache.Entries = value; }
570-
}
571-
572-
public GitBranch? CurrentBranch
573-
{
574-
get { return cacheContainer.RepositoryInfoCache.CurrentGitBranch; }
575-
private set { cacheContainer.RepositoryInfoCache.CurrentGitBranch = value; }
576-
}
577-
519+
public GitRemote[] Remotes => cacheContainer.BranchCache.Remotes;
520+
public GitBranch[] LocalBranches => cacheContainer.BranchCache.LocalBranches;
521+
public GitBranch[] RemoteBranches => cacheContainer.BranchCache.RemoteBranches;
522+
private ConfigBranch? CurrentConfigBranch => cacheContainer.BranchCache.CurrentConfigBranch;
523+
private ConfigRemote? CurrentConfigRemote => cacheContainer.BranchCache.CurrentConfigRemote;
524+
public int CurrentAhead => cacheContainer.GitTrackingStatusCache.Ahead;
525+
public int CurrentBehind => cacheContainer.GitTrackingStatusCache.Behind;
526+
public List<GitStatusEntry> CurrentChanges => cacheContainer.GitStatusEntriesCache.Entries;
527+
public GitBranch? CurrentBranch => cacheContainer.RepositoryInfoCache.CurrentGitBranch;
578528
public string CurrentBranchName => CurrentConfigBranch?.Name;
579529

580-
public GitRemote? CurrentRemote
581-
{
582-
get { return cacheContainer.RepositoryInfoCache.CurrentGitRemote; }
583-
private set { cacheContainer.RepositoryInfoCache.CurrentGitRemote = value; }
584-
}
585-
586-
public List<GitLogEntry> CurrentLog
587-
{
588-
get { return cacheContainer.GitLogCache.Log; }
589-
private set { cacheContainer.GitLogCache.Log = value; }
590-
}
591-
592-
public List<GitLock> CurrentLocks
593-
{
594-
get { return cacheContainer.GitLocksCache.GitLocks; }
595-
private set { cacheContainer.GitLocksCache.GitLocks = value; }
596-
}
597-
530+
public GitRemote? CurrentRemote => cacheContainer.RepositoryInfoCache.CurrentGitRemote;
531+
public List<GitLogEntry> CurrentLog => cacheContainer.GitLogCache.Log;
532+
public List<GitLock> CurrentLocks => cacheContainer.GitLocksCache.GitLocks;
598533
public UriString CloneUrl
599534
{
600535
get

0 commit comments

Comments
 (0)