Fix debounce timer setup and synchronization logic in DebouncedTextField component

This commit is contained in:
Thorsten Sommer 2025-08-15 20:58:43 +02:00
parent 29694e7edb
commit 0fa2e91afc
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -51,6 +51,7 @@ public partial class DebouncedTextField : MudComponentBase, IDisposable
private readonly Timer debounceTimer = new(); private readonly Timer debounceTimer = new();
private string text = string.Empty; private string text = string.Empty;
private string lastParameterText = string.Empty; private string lastParameterText = string.Empty;
private bool isInitialized;
#region Overrides of ComponentBase #region Overrides of ComponentBase
@ -68,20 +69,28 @@ public partial class DebouncedTextField : MudComponentBase, IDisposable
this.InvokeAsync(() => this.WhenTextCanged(this.text)); this.InvokeAsync(() => this.WhenTextCanged(this.text));
}; };
this.isInitialized = true;
await base.OnInitializedAsync(); await base.OnInitializedAsync();
} }
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
// Ensure the timer uses the latest debouncing interval:
if (!this.isInitialized)
return;
if(Math.Abs(this.debounceTimer.Interval - this.DebounceTime.TotalMilliseconds) > 1)
this.debounceTimer.Interval = this.DebounceTime.TotalMilliseconds;
// Only sync when the parent's parameter actually changed since the last change: // Only sync when the parent's parameter actually changed since the last change:
if (this.Text != this.lastParameterText) if (this.Text != this.lastParameterText)
{ {
this.text = this.Text; this.text = this.Text;
this.lastParameterText = this.Text; this.lastParameterText = this.Text;
}
this.debounceTimer.Stop(); this.debounceTimer.Stop();
this.debounceTimer.Start(); this.debounceTimer.Start();
}
await base.OnParametersSetAsync(); await base.OnParametersSetAsync();
} }