Await for OnInitializedAsync completes
Hello, I've a scenario where my template receives a service via dependency injection this service in turn performs an async operation.
the idea is to call the async operation in the OnInitializedAsync method of the page/component/template which will populate the asynchronously received data that the page will finally display once ready
I've seen that BlazorTemplater correctly invokes the OnInitializedAsync method (as well as all the other Async lifecycle method) but unfortunately the "render" phase appears to occur immediately without waiting the async methods to complete; So my data are not contained in the produced html. Looks like there are no method to "RenderAsync" or am I wrong?
Do you have any ideas/plans on how to achieve this? Basically what I would like to have is a RenderAsync Method that returns only when all the "Async" lifecycle methods of the component have completed and therefore provides the "final" html populated with all the values obtained from these async services.
-Of course I could invoke the service externally, obtain the values and then pass as parameter to the page, but I would prefer the other way because that allows me to reuse the template as normal page in my application (providing me to preview it to the user for example, before transforming it in a HTML string that I then transform in a PDF or I can email: preview is a cool feature!)
Any idea?
I've a PR almost ready but I would like to discuss it with you a bit first, the solution was quite easy but it produces some code duplication, that could be avoided but it is required to choose which coding style to take here
Duplicate of #23 though I don't agree with the workaround presented there and would like a proper solution as well.
Sorry I've not noticed the #23 and the related #30 PR untill i've pushed my PR #33 . My fault, I'm in a big hurry in implementing a solution for my project. Anyway, looks like that The NextRender tasks made exactly for the purpose, so why to not use it? it is very easy to create a code path that awaits it and then grabs the final HTML once.
It doesn't looks to be a workaround but a proper use of already existing feature.
I understand you can easily miss my PR because the corresponding issue has already been closed. About the NextRender see the comment I made in #30 I haven't tested it but I imagine something like below wouldn't work:
protected override async Task OnInitializedAsync()
{
await Task.Delay(500);
await InvokeAsync(StateHasChanged);
await Task.Delay(500);
Text = "Async value";
await base.OnInitializedAsync();
}
That's correct, it doesn't work because it introduces one more "NextRender"
Thanks for the contribution! I've had the async issue and the previous PR #30 on my to-do list for a while. I'll review the contribution and if everything checks out, I'll update.
Hello! Yes i've seen PR #30 and I prefer that solution. My fix (this PR #33) is a rushed solution because I really need it for my project, but it doesn't cover all the cases. Looks like that #30 is more robust, maybe we can work on the code review of that one so to adapt the "style" to the rest of the project but from a conceptual point of view it looks better.
Hi all, Firstly thanks for this excellent library. Helped me out a lot! Do you know when the async functionality will be added please? Also have a service via DI that performs an async operation. When you do add will it also work for the non-generic version that accepts a Type? Kind regards - Nigel