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

Commit 0c934a2

Browse files
committed
Fix raising cache update events when a view starts up
1 parent 9b187e5 commit 0c934a2

File tree

10 files changed

+105
-140
lines changed

10 files changed

+105
-140
lines changed

src/GitHub.Api/Cache/CacheContainer.cs

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using GitHub.Logging;
45

56
namespace GitHub.Unity
@@ -47,11 +48,11 @@ public IManagedCache GetCache(CacheType cacheType)
4748
return caches[cacheType].Value;
4849
}
4950

50-
public void CheckAndRaiseEventsIfCacheNewer(CacheUpdateEvent cacheUpdateEvent)
51+
public void CheckAndRaiseEventsIfCacheNewer(CacheType cacheType, CacheUpdateEvent cacheUpdateEvent)
5152
{
52-
var cache = GetCache(cacheUpdateEvent.cacheType);
53+
var cache = GetCache(cacheType);
5354
var needsInvalidation = cache.ValidateData();
54-
if (!needsInvalidation || cache.LastUpdatedAt != cacheUpdateEvent.UpdatedTime)
55+
if (!cacheUpdateEvent.IsInitialized || !needsInvalidation || cache.LastUpdatedAt != cacheUpdateEvent.UpdatedTime)
5556
{
5657
OnCacheUpdated(cache.CacheType, cache.LastUpdatedAt);
5758
}
@@ -99,4 +100,89 @@ public void Dispose()
99100
public IGitUserCache GitUserCache { get { return (IGitUserCache)caches[CacheType.GitUser].Value; } }
100101
public IRepositoryInfoCache RepositoryInfoCache { get { return (IRepositoryInfoCache)caches[CacheType.RepositoryInfo].Value; } }
101102
}
103+
104+
[Serializable]
105+
public struct CacheUpdateEvent
106+
{
107+
[NonSerialized] private DateTimeOffset? updatedTimeValue;
108+
public string updatedTimeString;
109+
public CacheType cacheType;
110+
111+
public CacheUpdateEvent(CacheType type, DateTimeOffset when)
112+
{
113+
if (type == CacheType.None) throw new ArgumentOutOfRangeException(nameof(type));
114+
115+
cacheType = type;
116+
updatedTimeValue = when;
117+
updatedTimeString = when.ToString(Constants.Iso8601Format);
118+
}
119+
120+
public override int GetHashCode()
121+
{
122+
int hash = 17;
123+
hash = hash * 23 + cacheType.GetHashCode();
124+
hash = hash * 23 + (updatedTimeString?.GetHashCode() ?? 0);
125+
return hash;
126+
}
127+
128+
public override bool Equals(object other)
129+
{
130+
if (other is CacheUpdateEvent)
131+
return Equals((CacheUpdateEvent)other);
132+
return false;
133+
}
134+
135+
public bool Equals(CacheUpdateEvent other)
136+
{
137+
return
138+
cacheType == other.cacheType &&
139+
String.Equals(updatedTimeString, other.updatedTimeString)
140+
;
141+
}
142+
143+
public static bool operator ==(CacheUpdateEvent lhs, CacheUpdateEvent rhs)
144+
{
145+
// If both are null, or both are same instance, return true.
146+
if (ReferenceEquals(lhs, rhs))
147+
return true;
148+
149+
// If one is null, but not both, return false.
150+
if (((object)lhs == null) || ((object)rhs == null))
151+
return false;
152+
153+
// Return true if the fields match:
154+
return lhs.Equals(rhs);
155+
}
156+
157+
public static bool operator !=(CacheUpdateEvent lhs, CacheUpdateEvent rhs)
158+
{
159+
return !(lhs == rhs);
160+
}
161+
162+
public DateTimeOffset UpdatedTime
163+
{
164+
get
165+
{
166+
if (!updatedTimeValue.HasValue)
167+
{
168+
DateTimeOffset result;
169+
if (DateTimeOffset.TryParseExact(updatedTimeString, Constants.Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
170+
{
171+
updatedTimeValue = result;
172+
}
173+
else
174+
{
175+
updatedTimeValue = DateTimeOffset.MinValue;
176+
updatedTimeString = updatedTimeValue.Value.ToString(Constants.Iso8601Format);
177+
}
178+
}
179+
180+
return updatedTimeValue.Value;
181+
}
182+
}
183+
184+
public bool IsInitialized => cacheType != CacheType.None;
185+
186+
public string UpdatedTimeString => updatedTimeString;
187+
}
102188
}

src/GitHub.Api/Cache/CacheInterfaces.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface ICacheContainer : IDisposable
3030
void ValidateAll();
3131
void InvalidateAll();
3232
IManagedCache GetCache(CacheType cacheType);
33-
void CheckAndRaiseEventsIfCacheNewer(CacheUpdateEvent cacheUpdateEvent);
33+
void CheckAndRaiseEventsIfCacheNewer(CacheType cacheType, CacheUpdateEvent cacheUpdateEvent);
3434
}
3535

3636
public interface IManagedCache

src/GitHub.Api/Git/IRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IRepository : IEquatable<IRepository>, IDisposable
2121
ITask RequestLock(string file);
2222
ITask ReleaseLock(string file, bool force);
2323
ITask DiscardChanges(GitStatusEntry[] discardEntries);
24-
void CheckAndRaiseEventsIfCacheNewer(CacheUpdateEvent cacheUpdateEvent);
24+
void CheckAndRaiseEventsIfCacheNewer(CacheType cacheType, CacheUpdateEvent cacheUpdateEvent);
2525

2626
/// <summary>
2727
/// Gets the name of the repository.

src/GitHub.Api/Git/Repository.cs

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public ITask SetupRemote(string remote, string remoteUrl)
120120
public ITask ReleaseLock(string file, bool force) => repositoryManager.UnlockFile(file, force);
121121
public ITask DiscardChanges(GitStatusEntry[] gitStatusEntry) => repositoryManager.DiscardChanges(gitStatusEntry);
122122

123-
public void CheckAndRaiseEventsIfCacheNewer(CacheUpdateEvent cacheUpdateEvent) => cacheContainer.CheckAndRaiseEventsIfCacheNewer(cacheUpdateEvent);
123+
public void CheckAndRaiseEventsIfCacheNewer(CacheType cacheType, CacheUpdateEvent cacheUpdateEvent) => cacheContainer.CheckAndRaiseEventsIfCacheNewer(cacheType, cacheUpdateEvent);
124124

125125

126126
/// <summary>
@@ -405,17 +405,7 @@ public User(ICacheContainer cacheContainer)
405405
cacheContainer.CacheUpdated += (type, dt) => { if (type == CacheType.GitUser) CacheHasBeenUpdated(dt); };
406406
}
407407

408-
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent)
409-
{
410-
var managedCache = cacheContainer.GitUserCache;
411-
var raiseEvent = !cacheUpdateEvent.IsInitialized || managedCache.LastUpdatedAt != cacheUpdateEvent.UpdatedTime;
412-
413-
Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
414-
cacheUpdateEvent.UpdatedTime, raiseEvent);
415-
416-
if (raiseEvent)
417-
CacheHasBeenUpdated(managedCache.LastUpdatedAt);
418-
}
408+
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent) => cacheContainer.CheckAndRaiseEventsIfCacheNewer(CacheType.GitUser, cacheUpdateEvent);
419409

420410
public void Initialize(IGitClient client)
421411
{
@@ -493,89 +483,4 @@ public string Email
493483

494484
protected static ILogging Logger { get; } = LogHelper.GetLogger<User>();
495485
}
496-
497-
[Serializable]
498-
public struct CacheUpdateEvent
499-
{
500-
[NonSerialized] private DateTimeOffset? updatedTimeValue;
501-
public string updatedTimeString;
502-
public CacheType cacheType;
503-
504-
public CacheUpdateEvent(CacheType type, DateTimeOffset when)
505-
{
506-
if (type == CacheType.None) throw new ArgumentOutOfRangeException(nameof(type));
507-
508-
cacheType = type;
509-
updatedTimeValue = when;
510-
updatedTimeString = when.ToString(Constants.Iso8601Format);
511-
}
512-
513-
public override int GetHashCode()
514-
{
515-
int hash = 17;
516-
hash = hash * 23 + cacheType.GetHashCode();
517-
hash = hash * 23 + (updatedTimeString?.GetHashCode() ?? 0);
518-
return hash;
519-
}
520-
521-
public override bool Equals(object other)
522-
{
523-
if (other is CacheUpdateEvent)
524-
return Equals((CacheUpdateEvent)other);
525-
return false;
526-
}
527-
528-
public bool Equals(CacheUpdateEvent other)
529-
{
530-
return
531-
cacheType == other.cacheType &&
532-
String.Equals(updatedTimeString, other.updatedTimeString)
533-
;
534-
}
535-
536-
public static bool operator ==(CacheUpdateEvent lhs, CacheUpdateEvent rhs)
537-
{
538-
// If both are null, or both are same instance, return true.
539-
if (ReferenceEquals(lhs, rhs))
540-
return true;
541-
542-
// If one is null, but not both, return false.
543-
if (((object)lhs == null) || ((object)rhs == null))
544-
return false;
545-
546-
// Return true if the fields match:
547-
return lhs.Equals(rhs);
548-
}
549-
550-
public static bool operator !=(CacheUpdateEvent lhs, CacheUpdateEvent rhs)
551-
{
552-
return !(lhs == rhs);
553-
}
554-
555-
public DateTimeOffset UpdatedTime
556-
{
557-
get
558-
{
559-
if (!updatedTimeValue.HasValue)
560-
{
561-
DateTimeOffset result;
562-
if (DateTimeOffset.TryParseExact(updatedTimeString, Constants.Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
563-
{
564-
updatedTimeValue = result;
565-
}
566-
else
567-
{
568-
updatedTimeValue = DateTimeOffset.MinValue;
569-
updatedTimeString = updatedTimeValue.Value.ToString(Constants.Iso8601Format);
570-
}
571-
}
572-
573-
return updatedTimeValue.Value;
574-
}
575-
}
576-
577-
public bool IsInitialized => cacheType != CacheType.None;
578-
579-
public string UpdatedTimeString => updatedTimeString;
580-
}
581486
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ private void DetachHandlers(IRepository repository)
152152

153153
private void ValidateCachedData(IRepository repository)
154154
{
155-
if (lastLocalAndRemoteBranchListChangedEvent.cacheType == CacheType.None)
156-
lastLocalAndRemoteBranchListChangedEvent = new CacheUpdateEvent(CacheType.Branches, DateTimeOffset.MinValue);
157-
repository.CheckAndRaiseEventsIfCacheNewer(lastLocalAndRemoteBranchListChangedEvent);
155+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.Branches, lastLocalAndRemoteBranchListChangedEvent);
158156
}
159157

160158
private void Render()

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ChangesView.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,9 @@ private void DetachHandlers(IRepository repository)
241241

242242
private void ValidateCachedData(IRepository repository)
243243
{
244-
if (lastCurrentBranchChangedEvent.cacheType == CacheType.None)
245-
lastCurrentBranchChangedEvent = new CacheUpdateEvent(CacheType.RepositoryInfo, DateTimeOffset.MinValue);
246-
if (lastStatusEntriesChangedEvent.cacheType == CacheType.None)
247-
lastStatusEntriesChangedEvent = new CacheUpdateEvent(CacheType.GitStatus, DateTimeOffset.MinValue);
248-
if (lastLocksChangedEvent.cacheType == CacheType.None)
249-
lastLocksChangedEvent = new CacheUpdateEvent(CacheType.GitLocks, DateTimeOffset.MinValue);
250-
251-
repository.CheckAndRaiseEventsIfCacheNewer(lastCurrentBranchChangedEvent);
252-
repository.CheckAndRaiseEventsIfCacheNewer(lastStatusEntriesChangedEvent);
253-
repository.CheckAndRaiseEventsIfCacheNewer(lastLocksChangedEvent);
244+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.RepositoryInfo, lastCurrentBranchChangedEvent);
245+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitStatus, lastStatusEntriesChangedEvent);
246+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitLocks, lastLocksChangedEvent);
254247
}
255248

256249
private void MaybeUpdateData()

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,9 @@ private void DetachHandlers(IRepository repository)
602602

603603
private void ValidateCachedData(IRepository repository)
604604
{
605-
if (lastLogChangedEvent.cacheType == CacheType.None)
606-
lastLogChangedEvent = new CacheUpdateEvent(CacheType.GitLog, DateTimeOffset.MinValue);
607-
if (lastAheadBehindChangedEvent.cacheType == CacheType.None)
608-
lastAheadBehindChangedEvent = new CacheUpdateEvent(CacheType.GitAheadBehind, DateTimeOffset.MinValue);
609-
if (lastCurrentRemoteChangedEvent.cacheType == CacheType.None)
610-
lastCurrentRemoteChangedEvent = new CacheUpdateEvent(CacheType.RepositoryInfo, DateTimeOffset.MinValue);
611-
repository.CheckAndRaiseEventsIfCacheNewer(lastLogChangedEvent);
612-
repository.CheckAndRaiseEventsIfCacheNewer(lastAheadBehindChangedEvent);
613-
repository.CheckAndRaiseEventsIfCacheNewer(lastCurrentRemoteChangedEvent);
605+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitLog, lastLogChangedEvent);
606+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitAheadBehind, lastAheadBehindChangedEvent);
607+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.RepositoryInfo, lastCurrentRemoteChangedEvent);
614608
}
615609

616610
private void MaybeUpdateData()

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ProjectWindowInterface.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ public static void Initialize(IRepository repo)
4242

4343
private static void ValidateCachedData(IRepository repository)
4444
{
45-
if (lastRepositoryStatusChangedEvent.cacheType == CacheType.None)
46-
lastRepositoryStatusChangedEvent = new CacheUpdateEvent(CacheType.GitStatus, DateTimeOffset.MinValue);
47-
if (lastLocksChangedEvent.cacheType == CacheType.None)
48-
lastLocksChangedEvent = new CacheUpdateEvent(CacheType.GitLocks, DateTimeOffset.MinValue);
49-
repository.CheckAndRaiseEventsIfCacheNewer(lastRepositoryStatusChangedEvent);
50-
repository.CheckAndRaiseEventsIfCacheNewer(lastLocksChangedEvent);
45+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitStatus, lastRepositoryStatusChangedEvent);
46+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitLocks, lastLocksChangedEvent);
5147
}
5248

5349
private static void RepositoryOnStatusChanged(CacheUpdateEvent cacheUpdateEvent)

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/SettingsView.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,8 @@ private void DetachHandlers(IRepository repository)
158158

159159
private void ValidateCachedData(IRepository repository)
160160
{
161-
if (lastCurrentRemoteChangedEvent.cacheType == CacheType.None)
162-
lastCurrentRemoteChangedEvent = new CacheUpdateEvent(CacheType.RepositoryInfo, DateTimeOffset.MinValue);
163-
if (lastLocksChangedEvent.cacheType == CacheType.None)
164-
lastLocksChangedEvent = new CacheUpdateEvent(CacheType.GitLocks, DateTimeOffset.MinValue);
165-
166-
repository.CheckAndRaiseEventsIfCacheNewer(lastCurrentRemoteChangedEvent);
167-
repository.CheckAndRaiseEventsIfCacheNewer(lastLocksChangedEvent);
161+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.RepositoryInfo, lastCurrentRemoteChangedEvent);
162+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitLocks, lastLocksChangedEvent);
168163
}
169164

170165
private void MaybeUpdateData()

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,7 @@ public override void Update()
261261

262262
private void ValidateCachedData(IRepository repository)
263263
{
264-
if (lastCurrentBranchAndRemoteChangedEvent.cacheType == CacheType.None)
265-
lastCurrentBranchAndRemoteChangedEvent = new CacheUpdateEvent(CacheType.RepositoryInfo, DateTimeOffset.MinValue);
266-
repository.CheckAndRaiseEventsIfCacheNewer(lastCurrentBranchAndRemoteChangedEvent);
264+
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.RepositoryInfo, lastCurrentBranchAndRemoteChangedEvent);
267265
}
268266

269267
private void MaybeUpdateData()

0 commit comments

Comments
 (0)