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

Commit 75d1d0b

Browse files
committed
Merge pull request #199 from github/shana/uicontroller-cleanup
Make UIController connection handling safer
2 parents f93886d + 24448c3 commit 75d1d0b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/GitHub.App/Controllers/UIController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,15 @@ public void Start([AllowNull] IConnection connection)
256256
else
257257
{
258258
connectionManager
259-
.IsLoggedIn(hosts)
260-
.Do(loggedin =>
259+
.GetLoggedInConnections(hosts)
260+
.FirstOrDefaultAsync()
261+
.Select(c =>
261262
{
263+
bool loggedin = c != null;
262264
if (currentFlow != UIControllerFlow.Authentication)
263265
{
264266
if (loggedin) // register the first available connection so the viewmodel can use it
265-
uiProvider.AddService(connectionManager.Connections.First(c => hosts.LookupHost(c.HostAddress).IsLoggedIn));
267+
uiProvider.AddService(c);
266268
else
267269
{
268270
// a connection will be added to the list when auth is done, register it so the next
@@ -284,6 +286,8 @@ public void Start([AllowNull] IConnection connection)
284286
.PermitIf(Trigger.Clone, UIViewType.Login, () => !loggedin)
285287
.PermitIf(Trigger.Publish, UIViewType.Publish, () => loggedin)
286288
.PermitIf(Trigger.Publish, UIViewType.Login, () => !loggedin);
289+
290+
return loggedin;
287291
})
288292
.ObserveOn(RxApp.MainThreadScheduler)
289293
.Subscribe(_ => { }, () =>

src/GitHub.Exports.Reactive/Extensions/ConnectionManagerExtensions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@ public static IObservable<bool> IsLoggedIn(this IConnectionManager cm, IReposito
1212
{
1313
return cm.Connections.ToObservable()
1414
.SelectMany(c => c.Login())
15-
.Select(c => hosts.LookupHost(c.HostAddress))
16-
.Any(h => h.IsLoggedIn);
15+
.Any(c => hosts.LookupHost(c.HostAddress).IsLoggedIn);
1716
}
1817

1918
public static IObservable<bool> IsLoggedIn(this IConnectionManager cm, IRepositoryHosts hosts, HostAddress address)
2019
{
2120
return cm.Connections.ToObservable()
2221
.Where(c => c.HostAddress.Equals(address))
2322
.SelectMany(c => c.Login())
24-
.Select(c => hosts.LookupHost(c.HostAddress))
25-
.Any(h => h.IsLoggedIn);
23+
.Any(c => hosts.LookupHost(c.HostAddress).IsLoggedIn);
24+
}
25+
26+
public static IObservable<IConnection> GetLoggedInConnections(this IConnectionManager cm, IRepositoryHosts hosts)
27+
{
28+
return cm.Connections.ToObservable()
29+
.SelectMany(c => c.Login())
30+
.Where(c => hosts.LookupHost(c.HostAddress).IsLoggedIn);
2631
}
2732
}
2833
}

0 commit comments

Comments
 (0)