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

Commit e0deaf9

Browse files
committed
Remove NewInlineCommentThreadViewModel.Finished
Instead listen to `LinesChanged` to decide when to update the thread.
1 parent 18cd657 commit e0deaf9

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

src/GitHub.InlineReviews/ViewModels/InlineCommentPeekViewModel.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reactive;
45
using System.Reactive.Linq;
@@ -13,6 +14,7 @@
1314
using GitHub.Services;
1415
using Microsoft.VisualStudio.Language.Intellisense;
1516
using Microsoft.VisualStudio.Text;
17+
using NLog;
1618
using ReactiveUI;
1719

1820
namespace GitHub.InlineReviews.ViewModels
@@ -22,6 +24,7 @@ namespace GitHub.InlineReviews.ViewModels
2224
/// </summary>
2325
public sealed class InlineCommentPeekViewModel : ReactiveObject, IDisposable
2426
{
27+
static readonly Logger log = LogManager.GetCurrentClassLogger();
2528
readonly IApiClientFactory apiClientFactory;
2629
readonly IInlineCommentPeekService peekService;
2730
readonly IPeekSession peekSession;
@@ -129,7 +132,25 @@ public async Task Initialize()
129132
.Subscribe(x => SessionChanged(x).Forget());
130133
}
131134

132-
fileSubscription = file.WhenAnyValue(x => x.InlineCommentThreads).Subscribe(_ => UpdateThread().Forget());
135+
fileSubscription?.Dispose();
136+
fileSubscription = file.LinesChanged.Subscribe(LinesChanged);
137+
}
138+
139+
async void LinesChanged(IReadOnlyList<int> lines)
140+
{
141+
try
142+
{
143+
var lineNumber = peekService.GetLineNumber(peekSession, triggerPoint).Item1;
144+
145+
if (lines.Contains(lineNumber))
146+
{
147+
await UpdateThread();
148+
}
149+
}
150+
catch (Exception e)
151+
{
152+
log.Error("Error updating InlineCommentViewModel", e);
153+
}
133154
}
134155

135156
async Task UpdateThread()
@@ -156,7 +177,6 @@ async Task UpdateThread()
156177
else
157178
{
158179
var newThread = new NewInlineCommentThreadViewModel(session, file, lineNumber, leftBuffer);
159-
threadSubscription = newThread.Finished.Subscribe(_ => UpdateThread().Forget());
160180
Thread = newThread;
161181
}
162182

src/GitHub.InlineReviews/ViewModels/NewInlineCommentThreadViewModel.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ public NewInlineCommentThreadViewModel(
7676
/// </summary>
7777
public IPullRequestSession Session { get; }
7878

79-
/// <summary>
80-
/// Gets an observable that is fired with a single value when a comment is sucessfully
81-
/// posted and therefore this is no loner a new comment thread.
82-
/// </summary>
83-
public IObservable<Unit> Finished => finished;
84-
8579
/// <summary>
8680
/// Gets a value indicating whether the user must commit and push their changes before
8781
/// leaving a comment on the requested line.

test/GitHub.InlineReviews.UnitTests/ViewModels/InlineCommentPeekViewModelTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Linq;
55
using System.Reactive.Linq;
6+
using System.Reactive.Subjects;
67
using System.Threading.Tasks;
78
using GitHub.Api;
89
using GitHub.Factories;
@@ -98,6 +99,7 @@ public async Task SwitchesFromNewThreadToExistingThreadWhenCommentPosted()
9899
peekSession.TextView.TextBuffer);
99100
var newThread = CreateThread(8, "New Comment");
100101
file.InlineCommentThreads.Returns(new[] { newThread });
102+
RaiseLinesChanged(file, 8);
101103
});
102104

103105
await target.Thread.Comments[0].CommitEdit.ExecuteAsyncTask(null);
@@ -211,7 +213,7 @@ void AddCommentToExistingThread(IPullRequestSessionFile file)
211213
var newComments = thread.Comments.Concat(new[] { newComment }).ToList();
212214
thread.Comments.Returns(newComments);
213215
file.InlineCommentThreads.Returns(newThreads);
214-
RaisePropertyChanged(file, nameof(file.InlineCommentThreads));
216+
RaiseLinesChanged(file, thread.LineNumber);
215217
}
216218

217219
IApiClientFactory CreateApiClientFactory()
@@ -289,6 +291,7 @@ IPullRequestSessionManager CreateSessionManager(string commitSha = "COMMIT")
289291
file.CommitSha.Returns(commitSha);
290292
file.Diff.Returns(new[] { diff });
291293
file.InlineCommentThreads.Returns(new[] { thread });
294+
file.LinesChanged.Returns(new Subject<IReadOnlyList<int>>());
292295

293296
var session = Substitute.For<IPullRequestSession>();
294297
session.LocalRepository.CloneUrl.Returns(new UriString("https://foo.bar"));
@@ -301,6 +304,12 @@ IPullRequestSessionManager CreateSessionManager(string commitSha = "COMMIT")
301304
return result;
302305
}
303306

307+
void RaiseLinesChanged(IPullRequestSessionFile file, params int[] lineNumbers)
308+
{
309+
var subject = (Subject<IReadOnlyList<int>>)file.LinesChanged;
310+
subject.OnNext(lineNumbers);
311+
}
312+
304313
void RaisePropertyChanged<T>(T o, string propertyName)
305314
where T : INotifyPropertyChanged
306315
{

test/GitHub.InlineReviews.UnitTests/ViewModels/NewInlineCommentThreadViewModelTests.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,6 @@ public void AddsCommentToCorrectDeletedLine()
119119
7);
120120
}
121121

122-
[Fact]
123-
public void SignalsFinishedWhenCommentPosted()
124-
{
125-
var session = CreateSession();
126-
var file = CreateFile();
127-
var target = new NewInlineCommentThreadViewModel(session, file, 10, false);
128-
var signalled = false;
129-
130-
target.Finished.Subscribe(_ => signalled = true);
131-
Assert.False(signalled);
132-
133-
target.Comments[0].Body = "New Comment";
134-
target.Comments[0].CommitEdit.Execute(null);
135-
136-
Assert.True(signalled);
137-
}
138-
139122
IApiClient CreateApiClient()
140123
{
141124
var result = Substitute.For<IApiClient>();

0 commit comments

Comments
 (0)