Skip to content

Commit 3c5b069

Browse files
Run UserStoppedTypingBehavior Command on UIThread (#2591)
* run continuation on UIThread * use tokenSource.Token * refactor to use async/await * Don't throw the exception, and log it * Use `async Task`, remove unncessesary try/catch block --------- Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
1 parent e17d471 commit 3c5b069

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

src/CommunityToolkit.Maui/Behaviors/UserStoppedTypingBehavior.shared.cs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using System.Diagnostics;
23
using System.Windows.Input;
34

45
namespace CommunityToolkit.Maui.Behaviors;
@@ -97,13 +98,13 @@ public void Dispose()
9798
}
9899

99100
/// <inheritdoc/>
100-
protected override void OnViewPropertyChanged(InputView sender, PropertyChangedEventArgs e)
101+
protected override async void OnViewPropertyChanged(InputView sender, PropertyChangedEventArgs e)
101102
{
102103
base.OnViewPropertyChanged(sender, e);
103104

104105
if (e.PropertyName == InputView.TextProperty.PropertyName)
105106
{
106-
OnTextPropertyChanged();
107+
await OnTextPropertyChanged(sender, sender.Text);
107108
}
108109
}
109110

@@ -121,38 +122,38 @@ protected virtual void Dispose(bool disposing)
121122
}
122123
}
123124

124-
void OnTextPropertyChanged()
125+
async Task OnTextPropertyChanged(InputView view, string? text)
125126
{
126-
if (tokenSource != null)
127+
if (tokenSource is not null)
127128
{
128129
tokenSource.Cancel();
129130
tokenSource.Dispose();
130131
}
132+
131133
tokenSource = new CancellationTokenSource();
132134

133-
Task.Delay(StoppedTypingTimeThreshold, tokenSource.Token)
134-
.ContinueWith(task =>
135-
{
136-
if (task.IsFaulted && task.Exception != null)
137-
{
138-
throw task.Exception;
139-
}
140-
141-
if (task.Status == TaskStatus.Canceled ||
142-
View?.Text?.Length < MinimumLengthThreshold)
143-
{
144-
return;
145-
}
146-
147-
if (View != null && ShouldDismissKeyboardAutomatically)
148-
{
149-
Dispatcher.DispatchIfRequired(View.Unfocus);
150-
}
151-
152-
if (View != null && Command?.CanExecute(CommandParameter ?? View.Text) is true)
153-
{
154-
Command.Execute(CommandParameter ?? View.Text);
155-
}
156-
});
135+
if (text is null || text.Length < MinimumLengthThreshold)
136+
{
137+
return;
138+
}
139+
140+
var task = Task.Delay(StoppedTypingTimeThreshold, tokenSource.Token);
141+
await task.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing | ConfigureAwaitOptions.ContinueOnCapturedContext);
142+
143+
if (task.Status is TaskStatus.Canceled)
144+
{
145+
Trace.WriteLine($"{nameof(UserStoppedTypingBehavior)}.{nameof(OnTextPropertyChanged)} cancelled");
146+
return;
147+
}
148+
149+
if (ShouldDismissKeyboardAutomatically)
150+
{
151+
view.Unfocus();
152+
}
153+
154+
if (Command?.CanExecute(CommandParameter ?? text) is true)
155+
{
156+
Command.Execute(CommandParameter ?? text);
157+
}
157158
}
158-
}
159+
}

0 commit comments

Comments
 (0)