ExpressionEvaluator
ExpressionEvaluator copied to clipboard
[suggestion] Implement async / await
~~Currently this is not included in todo / roadmap document here~~ - https://github.com/codingseb/ExpressionEvaluator/wiki/ExpressionEvaluator-Todo-List
Implementing async / await (with priority on await) would greatly increase flexibility of EE. C# is becoming more and more asynchronous with each new version and by using async patterns we can free current thread while external work is being processed (querying a database, creating/saving a stream, waiting for an external library to do some work...). This is critical for web applications where threadpool is very limited and we need to free our threads as often as possible.
This suggestion hence proposes that async / await keywords would be recognized when parsing scripts.
Backend:
public class Main {
public staic void Main() {
ExpressionEvaluator eval = new();
eval .StaticTypesForExtensionsMethods.Add(typeof(MyClassExt));
eval.Variables = new Dictionary<string, object> {
{ "SomeAsyncMethod", new Func<int>(async () => await someAwaitableJobReturningInt())} }
}
eval.ScriptEvaluate(script);
}
}
Script:
myResult = await SomeAsyncMethod();
Note that we would also need to support this on extension methods.
For the sake of supporting various syntaxes keywords should be remappable.
myResult = waitfor SomeAsyncMethod(); /* waitfor = await */
Yes it would be great. I need some reflection on how to do it. I keep it open as a todo 😃
Core of the problem here is that we need to "await" EventHandler which we can't do as it returns void. Something like DefferedEvents would be needed here - https://pedrolamas.github.io/DeferredEvents/
what do you think?
+1 to this feature