File tree 2 files changed +27
-2
lines changed
src/libraries/Microsoft.Extensions.Primitives 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -98,9 +98,12 @@ private void RegisterChangeTokenCallback(IChangeToken? token)
98
98
{
99
99
return ;
100
100
}
101
-
102
101
IDisposable registraton = token . RegisterChangeCallback ( s => ( ( ChangeTokenRegistration < TState > ? ) s ) ! . OnChangeTokenFired ( ) , this ) ;
103
-
102
+ if ( token . HasChanged && token . ActiveChangeCallbacks )
103
+ {
104
+ registraton ? . Dispose ( ) ;
105
+ return ;
106
+ }
104
107
SetDisposable ( registraton ) ;
105
108
}
106
109
Original file line number Diff line number Diff line change @@ -179,6 +179,28 @@ public void DisposingChangeTokenRegistrationDoesNotRaiseConsumerCallbackStateOve
179
179
Assert . Equal ( 5 , count ) ;
180
180
}
181
181
182
+ [ Fact ]
183
+ public void DisposingChangeTokenRegistrationDoesNotRaiseConsumerIfTokenProviderReturnsCancelledToken ( )
184
+ {
185
+ var provider = new ResettableChangeTokenProvider ( ) ;
186
+ Func < Func < IChangeToken > > changeTokenProviderFactory = ( ) =>
187
+ {
188
+ int n = 0 ;
189
+ return ( ) =>
190
+ {
191
+ var token = provider . GetChangeToken ( ) ;
192
+ if ( n ++ is 0 ) provider . Changed ( ) ;
193
+ return token ;
194
+ } ;
195
+ } ;
196
+ int count = 0 ;
197
+ var reg = ChangeToken . OnChange ( changeTokenProviderFactory ( ) , ( ) => count ++ ) ;
198
+ reg . Dispose ( ) ;
199
+ provider . Changed ( ) ;
200
+
201
+ Assert . Equal ( 1 , count ) ;
202
+ }
203
+
182
204
[ Fact ]
183
205
public void DisposingChangeTokenRegistrationDuringCallbackWorks ( )
184
206
{
You can’t perform that action at this time.
0 commit comments