Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.

Commit c507fb9

Browse files
committed
Improved async portion of the loads
1 parent 0b327fe commit c507fb9

11 files changed

Lines changed: 59 additions & 69 deletions

File tree

CodeHub.Core/ViewModels/ChangesetViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public void Init(NavObject navObject)
8181
ShowRepository = navObject.ShowRepository;
8282
}
8383

84-
protected override Task Load(bool forceDataRefresh)
84+
protected override Task Load(bool forceCacheInvalidation)
8585
{
86-
var t1 = Task.Run(() => this.RequestModel(_application.Client.Users[User].Repositories[Repository].Commits[Node].Get(), forceDataRefresh, response => Changeset = response.Data));
87-
FireAndForgetTask.Start(() => Comments.SimpleCollectionLoad(_application.Client.Users[User].Repositories[Repository].Commits[Node].Comments.GetAll(), forceDataRefresh));
86+
var t1 = this.RequestModel(_application.Client.Users[User].Repositories[Repository].Commits[Node].Get(), forceCacheInvalidation, response => Changeset = response.Data);
87+
Comments.SimpleCollectionLoad(_application.Client.Users[User].Repositories[Repository].Commits[Node].Comments.GetAll(), forceCacheInvalidation).FireAndForget();
8888
return t1;
8989
}
9090

CodeHub.Core/ViewModels/Gists/GistViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ public async Task ForkGist()
112112
ShowViewModel<GistViewModel>(new GistViewModel.NavObject { Id = forkedGist.Id });
113113
}
114114

115-
protected override Task Load(bool forceDataRefresh)
115+
protected override Task Load(bool forceCacheInvalidation)
116116
{
117-
var t1 = Task.Run(() => this.RequestModel(this.GetApplication().Client.Gists[Id].Get(), forceDataRefresh, response => Gist = response.Data));
118-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.Gists[Id].IsGistStarred(), forceDataRefresh, response => IsStarred = response.Data));
119-
FireAndForgetTask.Start(() => Comments.SimpleCollectionLoad(this.GetApplication().Client.Gists[Id].GetComments(), forceDataRefresh));
117+
var t1 = this.RequestModel(this.GetApplication().Client.Gists[Id].Get(), forceCacheInvalidation, response => Gist = response.Data);
118+
this.RequestModel(this.GetApplication().Client.Gists[Id].IsGistStarred(), forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget();
119+
Comments.SimpleCollectionLoad(this.GetApplication().Client.Gists[Id].GetComments(), forceCacheInvalidation).FireAndForget();
120120
return t1;
121121
}
122122

CodeHub.Core/ViewModels/Issues/IssueViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ public ICommand GoToWeb
8383

8484
protected override Task Load(bool forceCacheInvalidation)
8585
{
86-
var t1 = Task.Run(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[Repository].Issues[Id].Get(), forceCacheInvalidation, response => Issue = response.Data));
86+
var t1 = this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[Repository].Issues[Id].Get(), forceCacheInvalidation, response => Issue = response.Data);
8787

88-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[Repository].Issues[Id].GetComments(), forceCacheInvalidation, response => {
88+
this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[Repository].Issues[Id].GetComments(), forceCacheInvalidation, response => {
8989
Comments.Items.Reset(response.Data);
9090
this.CreateMore(response, m => Comments.MoreItems = m, Comments.Items.AddRange);
91-
}));
91+
}).FireAndForget();
9292

9393
return t1;
9494
}

CodeHub.Core/ViewModels/NotificationsViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ public NotificationsViewModel()
114114

115115
protected override Task Load(bool forceCacheInvalidation)
116116
{
117-
return Task.Run(() => this.RequestModel(this.GetApplication().Client.Notifications.GetAll(all: Notifications.Filter.All, participating: Notifications.Filter.Participating), forceCacheInvalidation, response => {
117+
return this.RequestModel(this.GetApplication().Client.Notifications.GetAll(all: Notifications.Filter.All, participating: Notifications.Filter.Participating), forceCacheInvalidation, response => {
118118
Notifications.Items.Reset(response.Data);
119119
UpdateAccountNotificationsCount();
120-
}));
120+
});
121121
}
122122

123123
private async void Read(NotificationModel model)

CodeHub.Core/ViewModels/Organizations/OrganizationViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public ICommand GoToRepositoriesCommand
6565
get { return new MvxCommand(() => ShowViewModel<OrganizationRepositoriesViewModel>(new OrganizationRepositoriesViewModel.NavObject { Name = Name })); }
6666
}
6767

68-
protected override Task Load(bool forceDataRefresh)
68+
protected override Task Load(bool forceCacheInvalidation)
6969
{
70-
return Task.Run(() => this.RequestModel(this.GetApplication().Client.Organizations[Name].Get(), forceDataRefresh, response => Organization = response.Data));
70+
return this.RequestModel(this.GetApplication().Client.Organizations[Name].Get(), forceCacheInvalidation, response => Organization = response.Data);
7171
}
7272

7373
public class NavObject

CodeHub.Core/ViewModels/PullRequests/PullRequestViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ protected override Task Load(bool forceDataRefresh)
7474
var pullRequest = this.GetApplication().Client.Users[User].Repositories[Repo].PullRequests[PullRequestId].Get();
7575
var commentsRequest = this.GetApplication().Client.Users[User].Repositories[Repo].Issues[PullRequestId].GetComments();
7676

77-
var t1 = Task.Run(() => this.RequestModel(pullRequest, forceDataRefresh, response => PullRequest = response.Data));
77+
var t1 = this.RequestModel(pullRequest, forceDataRefresh, response => PullRequest = response.Data);
7878

79-
FireAndForgetTask.Start(() => this.RequestModel(commentsRequest, forceDataRefresh, response => {
79+
this.RequestModel(commentsRequest, forceDataRefresh, response => {
80+
this.CreateMore(response, m => Comments.MoreItems = m, Comments.Items.AddRange);
8081
Comments.Items.Reset(response.Data);
81-
this.CreateMore(response, m => Comments.MoreItems = m, d => Comments.Items.AddRange(d));
82-
}));
82+
}).FireAndForget();
8383

8484
return t1;
8585
}
@@ -97,7 +97,7 @@ public async Task Merge()
9797
throw new Exception(response.Data.Message);
9898

9999
var pullRequest = this.GetApplication().Client.Users[User].Repositories[Repo].PullRequests[PullRequestId].Get();
100-
await Task.Run(() => this.RequestModel(pullRequest, true, r => PullRequest = r.Data));
100+
await this.RequestModel(pullRequest, true, r => PullRequest = r.Data);
101101
}
102102

103103
public ICommand MergeCommand

CodeHub.Core/ViewModels/Repositories/RepositoryViewModel.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,19 @@ private void PinRepository()
171171

172172
protected override Task Load(bool forceCacheInvalidation)
173173
{
174-
var t1 = Task.Run(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].Get(), forceCacheInvalidation, response => Repository = response.Data));
174+
var t1 = this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].Get(), forceCacheInvalidation, response => Repository = response.Data);
175175

176-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].GetReadme(),
177-
forceCacheInvalidation, response => Readme = response.Data));
176+
this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].GetReadme(),
177+
forceCacheInvalidation, response => Readme = response.Data).FireAndForget();
178178

179-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].GetBranches(),
180-
forceCacheInvalidation, response => Branches = response.Data));
179+
this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].GetBranches(),
180+
forceCacheInvalidation, response => Branches = response.Data).FireAndForget();
181181

182-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].IsWatching(),
183-
forceCacheInvalidation, response => IsWatched = response.Data));
182+
this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].IsWatching(),
183+
forceCacheInvalidation, response => IsWatched = response.Data).FireAndForget();
184184

185-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].IsStarred(),
186-
forceCacheInvalidation, response => IsStarred = response.Data));
185+
this.RequestModel(this.GetApplication().Client.Users[Username].Repositories[RepositoryName].IsStarred(),
186+
forceCacheInvalidation, response => IsStarred = response.Data).FireAndForget();
187187

188188
return t1;
189189
}

CodeHub.Core/ViewModels/Source/BranchesAndTagsViewModel.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ protected override Task Load(bool forceCacheInvalidation)
7575
if (SelectedFilter == 0)
7676
{
7777
var request = this.GetApplication().Client.Users[Username].Repositories[Repository].GetBranches();
78-
return Task.Run(() => this.RequestModel(request, forceCacheInvalidation, response =>
78+
return this.RequestModel(request, forceCacheInvalidation, response =>
7979
{
80-
Items.Items.Reset(response.Data.Where(x => x != null).Select(x => new ViewObject { Name = x.Name, Object = x }));
8180
this.CreateMore(response, m => Items.MoreItems = m, d => Items.Items.AddRange(d.Where(x => x != null).Select(x => new ViewObject { Name = x.Name, Object = x })));
82-
}));
81+
Items.Items.Reset(response.Data.Where(x => x != null).Select(x => new ViewObject { Name = x.Name, Object = x }));
82+
});
8383
}
8484
else
8585
{
8686
var request = this.GetApplication().Client.Users[Username].Repositories[Repository].GetTags();
87-
return Task.Run(() => this.RequestModel(request, forceCacheInvalidation, response => {
88-
Items.Items.Reset(response.Data.Where(x => x != null).Select(x => new ViewObject { Name = x.Name, Object = x }));
87+
return this.RequestModel(request, forceCacheInvalidation, response =>
88+
{
8989
this.CreateMore(response, m => Items.MoreItems = m, d => Items.Items.AddRange(d.Where(x => x != null).Select(x => new ViewObject { Name = x.Name, Object = x })));
90-
}));
90+
Items.Items.Reset(response.Data.Where(x => x != null).Select(x => new ViewObject { Name = x.Name, Object = x }));
91+
});
9192
}
9293
}
9394

CodeHub.Core/ViewModels/User/ProfileViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public void Init(NavObject navObject)
102102

103103
protected override Task Load(bool forceCacheInvalidation)
104104
{
105-
FireAndForgetTask.Start(() => this.RequestModel(this.GetApplication().Client.AuthenticatedUser.IsFollowing(Username), forceCacheInvalidation, x => IsFollowing = x.Data));
106-
return Task.Run(() => this.RequestModel(this.GetApplication().Client.Users[Username].Get(), forceCacheInvalidation, response => User = response.Data));
105+
this.RequestModel(this.GetApplication().Client.AuthenticatedUser.IsFollowing(Username), forceCacheInvalidation, x => IsFollowing = x.Data).FireAndForget();
106+
return this.RequestModel(this.GetApplication().Client.Users[Username].Get(), forceCacheInvalidation, response => User = response.Data);
107107
}
108108

109109
public class NavObject

CodeHub.Core/ViewModels/ViewModelExtensions.cs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,64 @@ namespace CodeHub.Core.ViewModels
1111
{
1212
public static class ViewModelExtensions
1313
{
14-
public static void RequestModel<TRequest>(this MvxViewModel viewModel, GitHubRequest<TRequest> request, bool forceDataRefresh, Action<GitHubResponse<TRequest>> update) where TRequest : new()
14+
public static async Task RequestModel<TRequest>(this MvxViewModel viewModel, GitHubRequest<TRequest> request, bool forceDataRefresh, Action<GitHubResponse<TRequest>> update) where TRequest : new()
1515
{
16-
var stopWatch = new System.Diagnostics.Stopwatch();
17-
stopWatch.Start();
18-
1916
if (forceDataRefresh)
2017
{
2118
request.CheckIfModified = false;
2219
request.RequestFromCache = false;
2320
}
2421

25-
var response = Mvx.Resolve<IApplicationService>().Client.Execute(request);
26-
stopWatch.Stop();
27-
28-
Console.WriteLine("Request executed in: " + stopWatch.ElapsedMilliseconds + "ms");
29-
30-
stopWatch.Reset();
31-
stopWatch.Start();
22+
var response = await Mvx.Resolve<IApplicationService>().Client.ExecuteAsync(request);
3223
update(response);
33-
stopWatch.Stop();
34-
35-
Console.WriteLine("View updated in: " + stopWatch.ElapsedMilliseconds + "ms");
36-
3724

3825
if (response.WasCached)
3926
{
40-
Task.Run(() => {
27+
Task.Run(async () => {
4128
try
4229
{
43-
request.RequestFromCache = false;
44-
update(Mvx.Resolve<IApplicationService>().Client.Execute(request));
30+
request.RequestFromCache = false;
31+
var r = await Mvx.Resolve<IApplicationService>().Client.ExecuteAsync(request);
32+
update(r);
4533
}
4634
catch (NotModifiedException)
4735
{
4836
Console.WriteLine("Not modified: " + request.Url);
4937
}
50-
catch (Exception)
38+
catch (Exception)
5139
{
5240
Console.WriteLine("SHIT! " + request.Url);
5341
}
5442
});
5543
}
5644
}
5745

58-
public static void CreateMore<T>(this MvxViewModel viewModel,
59-
GitHubResponse<T> response,
60-
Action<Action> assignMore,
61-
Action<T> newDataAction) where T : new()
46+
public static void CreateMore<T>(this MvxViewModel viewModel, GitHubResponse<T> response,
47+
Action<Task> assignMore, Action<T> newDataAction) where T : new()
6248
{
6349
if (response.More == null)
6450
{
6551
assignMore(null);
6652
return;
6753
}
6854

69-
assignMore(() => {
70-
response.More.UseCache = false;
71-
var moreResponse = Mvx.Resolve<IApplicationService>().Client.Execute(response.More);
72-
viewModel.CreateMore(moreResponse, assignMore, newDataAction);
73-
newDataAction(moreResponse.Data);
74-
});
55+
var task = new Task(async () =>
56+
{
57+
response.More.UseCache = false;
58+
var moreResponse = await Mvx.Resolve<IApplicationService>().Client.ExecuteAsync(response.More);
59+
viewModel.CreateMore(moreResponse, assignMore, newDataAction);
60+
newDataAction(moreResponse.Data);
61+
});
62+
63+
assignMore(task);
7564
}
7665

7766
public static Task SimpleCollectionLoad<T>(this CollectionViewModel<T> viewModel, GitHubRequest<List<T>> request, bool forceDataRefresh) where T : new()
7867
{
79-
return Task.Run(() => viewModel.RequestModel(request, forceDataRefresh, response => {
80-
viewModel.CreateMore(response, m => viewModel.MoreItems = m, d => viewModel.Items.AddRange(d));
68+
return viewModel.RequestModel(request, forceDataRefresh, response => {
69+
viewModel.CreateMore(response, m => viewModel.MoreItems = m, viewModel.Items.AddRange);
8170
viewModel.Items.Reset(response.Data);
82-
}));
71+
});
8372
}
8473
}
8574
}

0 commit comments

Comments
 (0)