Skip to content

Commit 17b9b32

Browse files
committed
reafactor out _suggestionRequestedCancellationSource
1 parent 1ba3228 commit 17b9b32

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/RichSuggestBox/RichSuggestBox.Helpers.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,5 @@ private static void ForEachLinkInDocument(ITextDocument document, Action<ITextRa
104104
nextIndex = range.GetIndex(TextRangeUnit.Link);
105105
}
106106
}
107-
108-
private static void CancelIfNotDisposed(CancellationTokenSource cancellationSource)
109-
{
110-
if (cancellationSource == null || cancellationSource.IsCancellationRequested)
111-
{
112-
return;
113-
}
114-
115-
try
116-
{
117-
cancellationSource.Cancel();
118-
}
119-
catch (ObjectDisposedException)
120-
{
121-
// pass
122-
}
123-
}
124107
}
125108
}

Microsoft.Toolkit.Uwp.UI.Controls.Input/RichSuggestBox/RichSuggestBox.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public partial class RichSuggestBox : ItemsControl
5555
private bool _popupOpenDown;
5656
private bool _textCompositionActive;
5757
private RichSuggestQuery _currentQuery;
58-
private CancellationTokenSource _suggestionRequestedCancellationSource;
5958

6059
/// <summary>
6160
/// Initializes a new instance of the <see cref="RichSuggestBox"/> class.
@@ -534,17 +533,25 @@ private async Task RequestSuggestionsAsync(ITextRange range = null)
534533
return;
535534
}
536535

537-
CancelIfNotDisposed(this._suggestionRequestedCancellationSource);
538-
this._suggestionRequestedCancellationSource = null;
536+
var previousTokenSource = currentQuery?.CancellationTokenSource;
537+
if (!(previousTokenSource?.IsCancellationRequested ?? true))
538+
{
539+
previousTokenSource.Cancel();
540+
}
539541

540542
if (queryFound)
541543
{
542-
_currentQuery = new RichSuggestQuery { Prefix = prefix, QueryText = query, Range = range };
544+
using var tokenSource = new CancellationTokenSource();
545+
_currentQuery = new RichSuggestQuery
546+
{
547+
Prefix = prefix,
548+
QueryText = query,
549+
Range = range,
550+
CancellationTokenSource = tokenSource
551+
};
543552

544553
if (SuggestionsRequested != null)
545554
{
546-
using var tokenSource = new CancellationTokenSource();
547-
_suggestionRequestedCancellationSource = tokenSource;
548555
var cancellationToken = tokenSource.Token;
549556
var eventArgs = new SuggestionsRequestedEventArgs { QueryText = query, Prefix = prefix };
550557
try
@@ -562,6 +569,8 @@ private async Task RequestSuggestionsAsync(ITextRange range = null)
562569
ShowSuggestionsPopup(_suggestionsList?.Items?.Count > 0);
563570
}
564571
}
572+
573+
tokenSource.Cancel();
565574
}
566575
else
567576
{

Microsoft.Toolkit.Uwp.UI.Controls.Input/RichSuggestBox/RichSuggestQuery.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Threading;
56
using Windows.UI.Text;
67

78
namespace Microsoft.Toolkit.Uwp.UI.Controls
@@ -16,5 +17,7 @@ internal class RichSuggestQuery
1617
public string QueryText { get; set; }
1718

1819
public ITextRange Range { get; set; }
20+
21+
public CancellationTokenSource CancellationTokenSource { get; set; }
1922
}
2023
}

0 commit comments

Comments
 (0)