@@ -353,6 +353,53 @@ public async Task PersistStateAsync_InvokesAllCallbacksEvenIfACallbackIsRemovedA
353353 Assert . Equal ( 4 , executionSequence . Count ) ;
354354 }
355355
356+ [ Fact ]
357+ public async Task PersistStateAsync_InvokesAllCallbacksWhenFirstCallbackUnregistersItself ( )
358+ {
359+ // Arrange
360+ var state = new Dictionary < string , byte [ ] > ( ) ;
361+ var store = new TestStore ( state ) ;
362+ var persistenceManager = new ComponentStatePersistenceManager (
363+ NullLogger < ComponentStatePersistenceManager > . Instance ,
364+ CreateServiceProvider ( ) ) ;
365+ var renderer = new TestRenderer ( ) ;
366+
367+ var executionSequence = new List < int > ( ) ;
368+
369+ // Register the first callback that will unregister itself - this is the key test case
370+ // where the first callback removes itself from the collection during iteration
371+ PersistingComponentStateSubscription firstSubscription = default ;
372+ firstSubscription = persistenceManager . State . RegisterOnPersisting ( ( ) =>
373+ {
374+ executionSequence . Add ( 1 ) ;
375+ firstSubscription . Dispose ( ) ; // Remove itself from the collection
376+ return Task . CompletedTask ;
377+ } , new TestRenderMode ( ) ) ;
378+
379+ // Register additional callbacks to ensure they still get executed
380+ persistenceManager . State . RegisterOnPersisting ( ( ) =>
381+ {
382+ executionSequence . Add ( 2 ) ;
383+ return Task . CompletedTask ;
384+ } , new TestRenderMode ( ) ) ;
385+
386+ persistenceManager . State . RegisterOnPersisting ( ( ) =>
387+ {
388+ executionSequence . Add ( 3 ) ;
389+ return Task . CompletedTask ;
390+ } , new TestRenderMode ( ) ) ;
391+
392+ // Act
393+ await persistenceManager . PersistStateAsync ( store , renderer ) ;
394+
395+ // Assert
396+ // All callbacks should be executed even though the first one removed itself
397+ Assert . Contains ( 1 , executionSequence ) ;
398+ Assert . Contains ( 2 , executionSequence ) ;
399+ Assert . Contains ( 3 , executionSequence ) ;
400+ Assert . Equal ( 3 , executionSequence . Count ) ;
401+ }
402+
356403 [ Fact ]
357404 public async Task RestoreStateAsync_ValidatesOnlySupportUpdatesWhenRestoreContextValueUpdate ( )
358405 {
0 commit comments