SleepWellPupper
SleepWellPupper
CC#
Created by SleepWellPupper on 8/17/2024 in #help
UI behavior specification language/tool
Context: Angular frontend, Cypress tests I'm currently implementing a UI behavior specification tool. The idea is to be able to generate guided tutorial tours, automatically build a document (pdf) detailing the spec (customer requirement) and generate cypress tests that verify the UI was implemented as per the spec, from the spec. The spec itself is a declarative TS object definition based off of which I then generate ShepherdJs guided tours as well as cypress tests. Is there tools or languages out there that aim for a similar thing? I'd like to research some alternative implementations to see how others solve this problem. I realize it's a rather specific problem but hey, can't hurt to ask eh? :)
4 replies
CC#
Created by SleepWellPupper on 7/26/2024 in #help
✅ Periodically (asynchronously) yielding in an otherwise synchronous method
I have a long-running CPU-bound algorithm that is being executed in a Blazor WASM application. I would like to cooperatively allow the UI to update / do event loop stuff etc. Would you think this approach would work:
/*
we have garanteed async yields via the flag, so we should return the
task instead of synchronously waiting and thus force consumers to also
await. The intended consumer is of course calling our method from a single
threaded context like in Blazor WASM, allowing for periodic UI updates and
event loops to execute. Because we assume a single threaded context, even
calling ConfigureAwait(false) would not help, as even the thread pool only has
the single thread available.
see also: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
*/
public static Task MyAlgoAsync() => MyAlgoCore(periodicallyYield: true);
/*
we can guarantee the task returned by MyAlgoCore to have already completed
because we disable all yields via the flag
*/
public static void MyAlgo() => MyAlgoCore(periodicallyYield: false).Wait();
private static async Task MyAlgoCore(bool periodicallyYield)
{
//our algorithm takes a large number of steps
for(var i = 0; i < 1000000; i++)
{
//we can put Task.Yield inbetween those steps
if(periodicallyYield)
{
await Task.Yield();
}
//do some stuff
}
}
/*
we have garanteed async yields via the flag, so we should return the
task instead of synchronously waiting and thus force consumers to also
await. The intended consumer is of course calling our method from a single
threaded context like in Blazor WASM, allowing for periodic UI updates and
event loops to execute. Because we assume a single threaded context, even
calling ConfigureAwait(false) would not help, as even the thread pool only has
the single thread available.
see also: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
*/
public static Task MyAlgoAsync() => MyAlgoCore(periodicallyYield: true);
/*
we can guarantee the task returned by MyAlgoCore to have already completed
because we disable all yields via the flag
*/
public static void MyAlgo() => MyAlgoCore(periodicallyYield: false).Wait();
private static async Task MyAlgoCore(bool periodicallyYield)
{
//our algorithm takes a large number of steps
for(var i = 0; i < 1000000; i++)
{
//we can put Task.Yield inbetween those steps
if(periodicallyYield)
{
await Task.Yield();
}
//do some stuff
}
}
Essentially we hide the await Task.Yield() path behind a flag, guarantueeing synchronous execution when desired, and enabling periodic yielding of the thread if not. CCing @BenMcLean since we got to the idea together.
42 replies
CC#
Created by SleepWellPupper on 7/20/2024 in #help
✅ What is C# Code Lowering?
Is there a cut and dry definition of what code lowering ("yoinking") encompasses? e.g.: - are top level statements "lowered" into a class/function? - are records "lowered" into class definitions? - are properties "lowered" into methods? Generally asking: What is code lowering in C#, where can I observe it or its effects in my code, and where can I read more about it? Thanks in advance!
10 replies