Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await for OnInitializedAsync completes #32

Open
PhotoAtomic opened this issue Sep 21, 2022 · 8 comments
Open

Await for OnInitializedAsync completes #32

PhotoAtomic opened this issue Sep 21, 2022 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@PhotoAtomic
Copy link

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?

@PhotoAtomic
Copy link
Author

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

@Drake53
Copy link

Drake53 commented Sep 21, 2022

Duplicate of #23 though I don't agree with the workaround presented there and would like a proper solution as well.

@PhotoAtomic
Copy link
Author

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.

@Drake53
Copy link

Drake53 commented Sep 21, 2022

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();
    }

@PhotoAtomic
Copy link
Author

That's correct, it doesn't work because it introduces one more "NextRender"

@conficient
Copy link
Owner

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.

@conficient conficient self-assigned this Sep 26, 2022
@conficient conficient added the enhancement New feature or request label Sep 26, 2022
@PhotoAtomic
Copy link
Author

PhotoAtomic commented Sep 26, 2022

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.

@OCATCloudApps
Copy link

OCATCloudApps commented Jan 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

4 participants