Don't schedule StateHasChanged if async lifecycle events complete synchronously, fixes #760

This commit is contained in:
Suchiman 2018-05-05 15:20:34 +02:00 committed by Steve Sanderson
parent 043556d111
commit 2192e00da5
1 changed files with 12 additions and 4 deletions

View File

@ -144,7 +144,7 @@ namespace Microsoft.AspNetCore.Blazor.Components
_renderHandle = renderHandle; _renderHandle = renderHandle;
} }
/// <summary> /// <summary>
/// Method invoked to apply initial or updated parameters to the component. /// Method invoked to apply initial or updated parameters to the component.
/// </summary> /// </summary>
@ -160,11 +160,19 @@ namespace Microsoft.AspNetCore.Blazor.Components
// If you override OnInitAsync and return a nonnull task, then by default // If you override OnInitAsync and return a nonnull task, then by default
// we automatically re-render once that task completes. // we automatically re-render once that task completes.
OnInitAsync()?.ContinueWith(ContinueAfterLifecycleTask); var initTask = OnInitAsync();
if (initTask != null && initTask.Status != TaskStatus.RanToCompletion)
{
initTask.ContinueWith(ContinueAfterLifecycleTask);
}
} }
OnParametersSet(); OnParametersSet();
OnParametersSetAsync()?.ContinueWith(ContinueAfterLifecycleTask); var parametersTask = OnParametersSetAsync();
if (parametersTask != null && parametersTask.Status != TaskStatus.RanToCompletion)
{
parametersTask.ContinueWith(ContinueAfterLifecycleTask);
}
StateHasChanged(); StateHasChanged();
} }
@ -200,7 +208,7 @@ namespace Microsoft.AspNetCore.Blazor.Components
// This just saves the developer the trouble of putting "StateHasChanged();" // This just saves the developer the trouble of putting "StateHasChanged();"
// at the end of every event callback. // at the end of every event callback.
StateHasChanged(); StateHasChanged();
if (task.Status == TaskStatus.RanToCompletion) if (task.Status == TaskStatus.RanToCompletion)
{ {
return; return;