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

CancellationToken in templates #2

Closed
Tyrrrz opened this issue Nov 3, 2022 · 3 comments
Closed

CancellationToken in templates #2

Tyrrrz opened this issue Nov 3, 2022 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@Tyrrrz
Copy link

Tyrrrz commented Nov 3, 2022

Hi!

It would be nice to have some sort of CancellationToken inside a template, that you can pass by calling Render() or RenderAsync().

The motivation is such that a template may invoke async methods that support cancellation, so it could be nice to propagate a token during rendering.

In MiniRazor, I just exposed a CancellationToken property:

https://github.com/Tyrrrz/MiniRazor/blob/c83bf8b9d4ed1d07dc4a377b30b308f6cecc44e2/MiniRazor.Runtime/TemplateBase.cs#L27-L28

Which is set when calling RenderAsync():

https://github.com/Tyrrrz/MiniRazor/blob/c83bf8b9d4ed1d07dc4a377b30b308f6cecc44e2/MiniRazor.CodeGen/TemplateClassGenerator.cs#L95-L103

@ltrzesniewski ltrzesniewski added the enhancement New feature or request label Nov 3, 2022
@ltrzesniewski
Copy link
Owner

ltrzesniewski commented Nov 3, 2022

Hi!

That's a good idea. Adding a CancellationToken property and a RenderAsync overload/default argument makes sense. I suppose I should also add this to Render for parity's sake.

In the meantime, you can work around the lack of this feature by adding the property to the template, or by creating an intermediate base class. It's a bit cumbersome though.

@functions
{
	public CancellationToken CancellationToken { get; init; }
}

@Tyrrrz
Copy link
Author

Tyrrrz commented Nov 3, 2022

That's a good workaround, thanks!

@ltrzesniewski ltrzesniewski added this to the v0.3.0 milestone Nov 5, 2022
@ltrzesniewski
Copy link
Owner

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

No branches or pull requests

2 participants