@@ -89,22 +89,19 @@ private void InitializeWaiting()
8989 {
9090 if ( ! WaitTask . IsCompleted )
9191 {
92- var renderCountAtSubscribeTime = renderedFragment . RenderCount ;
93-
94- // Before subscribing to renderedFragment.OnAfterRender,
95- // we need to make sure that the desired state has not already been reached.
96- OnAfterRender ( this , EventArgs . Empty ) ;
97-
98- SubscribeToOnAfterRender ( ) ;
99-
100- // If the render count from before subscribing has changes
101- // till now, we need to do trigger another check, since
102- // the render may have happened asynchronously and before
103- // the subscription was set up.
104- if ( renderCountAtSubscribeTime < renderedFragment . RenderCount )
92+ // Subscribe inside the renderers synchronization context
93+ // to ensure no renders happens between the
94+ // initial OnAfterRender and subscribing.
95+ // This also ensures that checks performed during OnAfterRender,
96+ // which are usually not atomic, e.g. search the DOM tree,
97+ // can be performed without the DOM tree changing.
98+ renderedFragment . InvokeAsync ( ( ) =>
10599 {
100+ // Before subscribing to renderedFragment.OnAfterRender,
101+ // we need to make sure that the desired state has not already been reached.
106102 OnAfterRender ( this , EventArgs . Empty ) ;
107- }
103+ SubscribeToOnAfterRender ( ) ;
104+ } ) ;
108105 }
109106 }
110107
0 commit comments