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

Add tasks to defaultobjectconverter #1787

Merged

Conversation

tom-b-iodigital
Copy link
Contributor

With this PR it's possible to call object methods that return tasks.

For example:

var engine = new Engine();
engine.SetValue("log", new Action<object>(Console.WriteLine))
    .SetValue("getTest", new Func<TestClass>(() => new()))
    .SetValue("__result", 0);

engine.Execute("""

(async () => {
    __result = "Before call";
    let testClass = getTest();

    __result = await testClass.GetDelayedStringAsync();
})();

""");

Console.WriteLine(engine.GetValue("__result"));


public class TestClass
{
    public async Task<string> GetDelayedStringAsync()
    {
        await Task.Delay(5000).ConfigureAwait(false);

        return "This is a delayed string";
    }
}

Before PR:

image

After PR:

image

@lahma
Copy link
Collaborator

lahma commented Feb 21, 2024

Thanks for the PR, we also need tests to cover this new functionality - it's easy to break things later if behavior hasn't been tested.

@tom-b-iodigital
Copy link
Contributor Author

I'll add some test cases

@tom-b-iodigital
Copy link
Contributor Author

tom-b-iodigital commented Feb 21, 2024

test cases added, let me know if you need anything else

Copy link
Collaborator

@lahma lahma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for your contribution!

@lahma lahma merged commit 3fbbff7 into sebastienros:main Feb 21, 2024
3 checks passed
@tom-b-iodigital tom-b-iodigital deleted the feature/convert-tasks-correctly branch February 21, 2024 18:22
@uleelx
Copy link

uleelx commented Mar 2, 2024

This feature breaks the code before. In version 3.0.0, we can get the value synchronously: var result = sometask.result, and it seems much faster than Promise.then in v3.0.1 @tom-b-iodigital @lahma

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

Successfully merging this pull request may close these issues.

3 participants