4
4
5
5
namespace AIStudio . Components ;
6
6
7
- public partial class DebouncedTextField : MudComponentBase
7
+ public partial class DebouncedTextField : MudComponentBase , IDisposable
8
8
{
9
9
[ Parameter ]
10
10
public string Label { get ; set ; } = string . Empty ;
@@ -51,6 +51,7 @@ public partial class DebouncedTextField : MudComponentBase
51
51
private readonly Timer debounceTimer = new ( ) ;
52
52
private string text = string . Empty ;
53
53
private string lastParameterText = string . Empty ;
54
+ private bool isInitialized ;
54
55
55
56
#region Overrides of ComponentBase
56
57
@@ -68,20 +69,30 @@ protected override async Task OnInitializedAsync()
68
69
this . InvokeAsync ( ( ) => this . WhenTextCanged ( this . text ) ) ;
69
70
} ;
70
71
72
+ this . isInitialized = true ;
71
73
await base . OnInitializedAsync ( ) ;
72
74
}
73
75
74
- protected override void OnParametersSet ( )
76
+ protected override async Task OnParametersSetAsync ( )
75
77
{
78
+ // Ensure the timer uses the latest debouncing interval:
79
+ if ( ! this . isInitialized )
80
+ return ;
81
+
82
+ if ( Math . Abs ( this . debounceTimer . Interval - this . DebounceTime . TotalMilliseconds ) > 1 )
83
+ this . debounceTimer . Interval = this . DebounceTime . TotalMilliseconds ;
84
+
76
85
// Only sync when the parent's parameter actually changed since the last change:
77
86
if ( this . Text != this . lastParameterText )
78
87
{
79
88
this . text = this . Text ;
80
89
this . lastParameterText = this . Text ;
81
-
82
- this . debounceTimer . Stop ( ) ;
83
- this . debounceTimer . Start ( ) ;
84
90
}
91
+
92
+ this . debounceTimer . Stop ( ) ;
93
+ this . debounceTimer . Start ( ) ;
94
+
95
+ await base . OnParametersSetAsync ( ) ;
85
96
}
86
97
87
98
#endregion
@@ -92,4 +103,21 @@ private void OnTextChanged(string value)
92
103
this . debounceTimer . Stop ( ) ;
93
104
this . debounceTimer . Start ( ) ;
94
105
}
106
+
107
+ #region IDisposable
108
+
109
+ public void Dispose ( )
110
+ {
111
+ try
112
+ {
113
+ this . debounceTimer . Stop ( ) ;
114
+ this . debounceTimer . Dispose ( ) ;
115
+ }
116
+ catch
117
+ {
118
+ // ignore
119
+ }
120
+ }
121
+
122
+ #endregion
95
123
}
0 commit comments