Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a65a587

Browse files
committed
Add support for refreshing an existing collection
To support refreshing data from the server without having to clear the whole collection and redo inserting and sorting and filtering all over again, we can just pass an existing collection to `ModelService`, so it will set up the new query and update the existing collection.
1 parent e726e6a commit a65a587

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/GitHub.App/Services/ModelService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ public IObservable<AccountCacheItem> GetUserFromCache()
135135
return Observable.Defer(() => hostCache.GetObject<AccountCacheItem>("user"));
136136
}
137137

138-
public ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryModel repo)
138+
public ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryModel repo,
139+
[AllowNull]ITrackingCollection<IPullRequestModel> collection = null)
139140
{
140141
// Since the api to list pull requests returns all the data for each pr, cache each pr in its own entry
141142
// and also cache an index that contains all the keys for each pr. This way we can fetch prs in bulk
@@ -146,7 +147,8 @@ public ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryM
146147
var keyobs = GetUserFromCache()
147148
.Select(user => string.Format(CultureInfo.InvariantCulture, "{0}|{1}|pr", user.Login, repo.Name));
148149

149-
var col = new TrackingCollection<IPullRequestModel>();
150+
if (collection == null)
151+
collection = new TrackingCollection<IPullRequestModel>();
150152

151153
var source = Observable.Defer(() => keyobs
152154
.SelectMany(key =>
@@ -156,7 +158,7 @@ public ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryM
156158
item =>
157159
{
158160
// this could blow up due to the collection being disposed somewhere else
159-
try { col.RemoveItem(Create(item)); }
161+
try { collection.RemoveItem(Create(item)); }
160162
catch (ObjectDisposedException) { }
161163
},
162164
TimeSpan.FromMinutes(5),
@@ -165,8 +167,8 @@ public ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryM
165167
.Select(Create)
166168
);
167169

168-
col.Listen(source);
169-
return col;
170+
collection.Listen(source);
171+
return collection;
170172
}
171173

172174
public IObservable<Unit> InvalidateAll()

src/GitHub.Exports.Reactive/Services/IModelService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public interface IModelService : IDisposable
1919
IObservable<IReadOnlyList<IRepositoryModel>> GetRepositories();
2020
IObservable<IReadOnlyList<LicenseItem>> GetLicenses();
2121
IObservable<IReadOnlyList<GitIgnoreItem>> GetGitIgnoreTemplates();
22-
ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryModel repo);
22+
ITrackingCollection<IPullRequestModel> GetPullRequests(ISimpleRepositoryModel repo,
23+
ITrackingCollection<IPullRequestModel> collection = null);
2324
IObservable<Unit> InvalidateAll();
2425
}
2526
}

0 commit comments

Comments
 (0)