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

Commit 60312fd

Browse files
Migrating Caching of user data from GitClient to User
Misunderstood the initial requirements. I believed the User object was just for currying data. So I created GitUser to represent the user data returned from GitClient and then moved the caching functionality over to User.
1 parent 1de70d6 commit 60312fd

File tree

10 files changed

+146
-131
lines changed

10 files changed

+146
-131
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 47 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace GitHub.Unity
88
{
99
public interface IGitClient
1010
{
11-
event Action<CacheUpdateEvent> CurrentUserChanged;
12-
1311
Task<NPath> FindGitInstallation();
1412
ITask<ValidateGitInstallResult> ValidateGitInstall(NPath path);
1513

@@ -25,6 +23,8 @@ ITask<string> GetConfig(string key, GitConfigSource configSource,
2523
ITask<string> SetConfig(string key, string value, GitConfigSource configSource,
2624
IOutputProcessor<string> processor = null);
2725

26+
ITask<GitUser> GetConfigUserAndEmail();
27+
2828
ITask<List<GitLock>> ListLocks(bool local,
2929
BaseOutputListProcessor<GitLock> processor = null);
3030

@@ -84,11 +84,7 @@ ITask<string> Unlock(string file, bool force,
8484

8585
ITask<Version> LfsVersion(IOutputProcessor<Version> processor = null);
8686

87-
void SetConfigUserAndEmail(string username, string email);
88-
89-
void CheckUserChangedEvent(CacheUpdateEvent gitLogCacheUpdateEvent);
90-
91-
User CurrentUser { get; }
87+
ITask<GitUser> SetConfigUserAndEmail(string username, string email);
9288
}
9389

9490
class GitClient : IGitClient
@@ -100,51 +96,14 @@ class GitClient : IGitClient
10096
private readonly ITaskManager taskManager;
10197
private readonly CancellationToken cancellationToken;
10298

103-
public event Action<CacheUpdateEvent> CurrentUserChanged;
104-
private bool cacheInitialized;
105-
10699
public GitClient(IEnvironment environment, IProcessManager processManager, ITaskManager taskManager)
107100
{
108-
Logger.Trace("Constructed");
109-
110101
this.environment = environment;
111102
this.processManager = processManager;
112103
this.taskManager = taskManager;
113104
this.cancellationToken = taskManager.Token;
114105
}
115106

116-
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent)
117-
{
118-
var managedCache = environment.CacheContainer.GitUserCache;
119-
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
120-
121-
Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
122-
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
123-
124-
if (raiseEvent)
125-
{
126-
var dateTimeOffset = managedCache.LastUpdatedAt;
127-
var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() };
128-
HandleGitLogCacheUpdatedEvent(updateEvent);
129-
}
130-
}
131-
132-
public User CurrentUser
133-
{
134-
get
135-
{
136-
if (!cacheInitialized)
137-
{
138-
cacheInitialized = true;
139-
environment.CacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated;
140-
environment.CacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated;
141-
}
142-
143-
return environment.CacheContainer.GitUserCache.User;
144-
}
145-
private set { environment.CacheContainer.GitUserCache.User = value; }
146-
}
147-
148107
public async Task<NPath> FindGitInstallation()
149108
{
150109
if (!String.IsNullOrEmpty(environment.GitExecutablePath))
@@ -300,12 +259,35 @@ public ITask<string> SetConfig(string key, string value, GitConfigSource configS
300259
.Configure(processManager);
301260
}
302261

303-
public void SetConfigUserAndEmail(string username, string email)
262+
public ITask<GitUser> GetConfigUserAndEmail()
263+
{
264+
string username = null;
265+
string email = null;
266+
267+
return GetConfig(UserNameConfigKey, GitConfigSource.User)
268+
.Then((success, value) => {
269+
if (success)
270+
{
271+
username = value;
272+
}
273+
})
274+
.Then(GetConfig(UserEmailConfigKey, GitConfigSource.User)
275+
.Then((success, value) => {
276+
if (success)
277+
{
278+
email = value;
279+
}
280+
})).Then(success => {
281+
Logger.Trace("{0}:{1} {2}:{3}", UserNameConfigKey, username, UserEmailConfigKey, email);
282+
return new GitUser(username, email);
283+
});
284+
}
285+
286+
public ITask<GitUser> SetConfigUserAndEmail(string username, string email)
304287
{
305-
SetConfig(UserNameConfigKey, username, GitConfigSource.User)
288+
return SetConfig(UserNameConfigKey, username, GitConfigSource.User)
306289
.Then(SetConfig(UserEmailConfigKey, email, GitConfigSource.User))
307-
.Then(UpdateUserAndEmail)
308-
.Start();
290+
.Then(b => new GitUser(username, email));
309291
}
310292

311293
public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null)
@@ -480,54 +462,28 @@ public ITask<string> Unlock(string file, bool force,
480462
.Configure(processManager);
481463
}
482464

483-
private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset)
484-
{
485-
HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent
486-
{
487-
UpdatedTimeString = timeOffset.ToString()
488-
});
489-
}
465+
protected static ILogging Logger { get; } = Logging.GetLogger<GitClient>();
466+
}
490467

491-
private void GitUserCacheOnCacheInvalidated()
492-
{
493-
Logger.Trace("GitUserCache Invalidated");
494-
UpdateUserAndEmail();
495-
}
496-
497-
private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
498-
{
499-
Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString);
500-
CurrentUserChanged?.Invoke(cacheUpdateEvent);
501-
}
468+
public struct GitUser
469+
{
470+
public static GitUser Default = new GitUser();
502471

503-
private void UpdateUserAndEmail()
504-
{
505-
Logger.Trace("UpdateUserAndEmail");
472+
public string name;
473+
public string email;
506474

507-
string username = null;
508-
string email = null;
475+
public string Name { get { return name; } }
476+
public string Email { get { return email; } }
509477

510-
GetConfig(UserNameConfigKey, GitConfigSource.User)
511-
.Then((success, value) => {
512-
if (success)
513-
{
514-
username = value;
515-
}
516-
})
517-
.Then(GetConfig(UserEmailConfigKey, GitConfigSource.User)
518-
.Then((success, value) => {
519-
if (success)
520-
{
521-
email = value;
522-
}
523-
})).ThenInUI(success => {
524-
CurrentUser = new User {
525-
Name = username,
526-
Email = email
527-
};
528-
}).Start();
478+
public GitUser(string name, string email)
479+
{
480+
this.name = name;
481+
this.email = email;
529482
}
530483

531-
protected static ILogging Logger { get; } = Logging.GetLogger<GitClient>();
484+
public override string ToString()
485+
{
486+
return $"Name:\"{Name}\" Email:\"{Email}\"";
487+
}
532488
}
533489
}

src/GitHub.Api/Git/Repository.cs

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,15 +668,43 @@ public bool IsGitHub
668668

669669
public interface IUser
670670
{
671-
string Name { get; set; }
672-
string Email { get; set; }
671+
string Name { get; }
672+
string Email { get; }
673+
event Action<CacheUpdateEvent> UserChanged;
674+
void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent);
673675
}
674676

675677
[Serializable]
676678
public class User : IUser
677679
{
678-
public string name;
679-
public string email;
680+
private ICacheContainer cacheContainer;
681+
private IGitClient gitClient;
682+
683+
public event Action<CacheUpdateEvent> UserChanged;
684+
685+
public User(ICacheContainer cacheContainer)
686+
{
687+
this.cacheContainer = cacheContainer;
688+
689+
cacheContainer.GitUserCache.CacheInvalidated += GitUserCacheOnCacheInvalidated;
690+
cacheContainer.GitUserCache.CacheUpdated += GitUserCacheOnCacheUpdated;
691+
}
692+
693+
public void CheckUserChangedEvent(CacheUpdateEvent cacheUpdateEvent)
694+
{
695+
var managedCache = cacheContainer.GitUserCache;
696+
var raiseEvent = managedCache.ShouldRaiseCacheEvent(cacheUpdateEvent);
697+
698+
Logger.Trace("Check GitUserCache CacheUpdateEvent Current:{0} Check:{1} Result:{2}", managedCache.LastUpdatedAt,
699+
cacheUpdateEvent.UpdatedTimeString ?? "[NULL]", raiseEvent);
700+
701+
if (raiseEvent)
702+
{
703+
var dateTimeOffset = managedCache.LastUpdatedAt;
704+
var updateEvent = new CacheUpdateEvent { UpdatedTimeString = dateTimeOffset.ToString() };
705+
HandleGitLogCacheUpdatedEvent(updateEvent);
706+
}
707+
}
680708

681709
public override string ToString()
682710
{
@@ -685,15 +713,54 @@ public override string ToString()
685713

686714
public string Name
687715
{
688-
get { return name; }
689-
set { name = value; }
716+
get { return cacheContainer.GitUserCache.User.Name; }
717+
private set { cacheContainer.GitUserCache.User.Name = value; }
690718
}
691719

692720
public string Email
693721
{
694-
get { return email; }
695-
set { email = value; }
722+
get { return cacheContainer.GitUserCache.User.Email; }
723+
private set { cacheContainer.GitUserCache.User.Email = value; }
696724
}
725+
726+
private void GitUserCacheOnCacheUpdated(DateTimeOffset timeOffset)
727+
{
728+
HandleGitLogCacheUpdatedEvent(new CacheUpdateEvent
729+
{
730+
UpdatedTimeString = timeOffset.ToString()
731+
});
732+
}
733+
734+
private void GitUserCacheOnCacheInvalidated()
735+
{
736+
Logger.Trace("GitUserCache Invalidated");
737+
UpdateUserAndEmail();
738+
}
739+
740+
private void HandleGitLogCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
741+
{
742+
Logger.Trace("GitUserCache Updated {0}", cacheUpdateEvent.UpdatedTimeString);
743+
UserChanged?.Invoke(cacheUpdateEvent);
744+
}
745+
746+
private void UpdateUserAndEmail()
747+
{
748+
Logger.Trace("UpdateUserAndEmail");
749+
750+
string username = null;
751+
string email = null;
752+
753+
gitClient.GetConfigUserAndEmail()
754+
.Then((success, value) => {
755+
if (success)
756+
{
757+
username = value.Name;
758+
email = value.Email;
759+
}
760+
}).Start();
761+
}
762+
763+
protected static ILogging Logger { get; } = Logging.GetLogger<Repository>();
697764
}
698765

699766
[Serializable]

src/GitHub.Api/Platform/DefaultEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void Initialize(string unityVersion, NPath extensionInstallPath, NPath un
4848
UnityAssetsPath = assetsPath;
4949
UnityProjectPath = assetsPath.Parent;
5050
UnityVersion = unityVersion;
51+
User = new User(CacheContainer);
5152
}
5253

5354
public void InitializeRepository(NPath expectedRepositoryPath = null)
@@ -134,6 +135,7 @@ public NPath GitExecutablePath
134135
public NPath RepositoryPath { get; private set; }
135136
public ICacheContainer CacheContainer { get; private set; }
136137
public IRepository Repository { get; set; }
138+
public IUser User { get; set; }
137139

138140
public bool IsWindows { get { return OnWindows; } }
139141
public bool IsLinux { get { return OnLinux; } }

src/GitHub.Api/Platform/IEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface IEnvironment
2727
NPath SystemCachePath { get; set; }
2828
NPath LogPath { get; }
2929
IFileSystem FileSystem { get; set; }
30+
IUser User { get; set; }
3031
IRepository Repository { get; set; }
3132
string ExecutableExtension { get; }
3233
ICacheContainer CacheContainer { get; }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace GitHub.Unity
77
abstract class BaseWindow : EditorWindow, IView
88
{
99
[NonSerialized] private bool initialized = false;
10+
[NonSerialized] private IUser cachedUser;
1011
[NonSerialized] private IRepository cachedRepository;
1112
[NonSerialized] private bool initializeWasCalled;
1213
[NonSerialized] private bool inLayout;
@@ -21,6 +22,7 @@ public void InitializeWindow(IApplicationManager applicationManager, bool requir
2122
initialized = true;
2223
initializeWasCalled = true;
2324
Manager = applicationManager;
25+
cachedUser = Environment.User;
2426
cachedRepository = Environment.Repository;
2527
Initialize(applicationManager);
2628
if (requiresRedraw)
@@ -103,6 +105,8 @@ public virtual void OnSelectionChange()
103105
public abstract bool IsBusy { get; }
104106
public IRepository Repository { get { return inLayout ? cachedRepository : Environment.Repository; } }
105107
public bool HasRepository { get { return Repository != null; } }
108+
public IUser User { get { return cachedUser; } }
109+
public bool HasUser { get { return User != null; } }
106110

107111
protected ITaskManager TaskManager { get { return Manager.TaskManager; } }
108112
protected IGitClient GitClient { get { return Manager.GitClient; } }

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface IView
1414
void Finish(bool result);
1515
IRepository Repository { get; }
1616
bool HasRepository { get; }
17+
IUser User { get; }
18+
bool HasUser { get; }
1719
IApplicationManager Manager { get; }
1820
bool IsBusy { get; }
1921
}

0 commit comments

Comments
 (0)