polterguy / lizzie

A script language for .Net and the CLR
https://ainiro.io/
MIT License
190 stars 27 forks source link

Support Task-async/await #6

Closed hoangitk closed 5 years ago

hoangitk commented 6 years ago

I have try to bind a Task but lizzie does not support. Is there any way to deal with Task/async/wait?

Thank you so much with lizzie idea! 👍🤗

polterguy commented 6 years ago

Thank you for the nice words :)

Not sure about exactly what you're trying to achieve, but can't you create a wrapper method around your invocation to Lizzie, and then invoke this method as an async task ...?

If not, feel free to provide some example code that you'd expect to compile, and I (might) implement support for it. I've got a couple of other tasks with Lizzie I'll need to implement anyways, so I was thinking of having a Lizzie upgrade session soon anyways ...

hoangitk commented 5 years ago

Hi Mr. Thomas,

This is my sample code:

namespace lizzie.web_api.example.Controllers
{
    [Route("api/[controller]")]
    public class LizzieController : Controller
    {
        // ...
        #region [ -- Lizzie functions -- ]
        [Bind(Name = "call-other-api")]
        async Task<object> CallOtherApi(Binder<LizzieController> binder, Arguments arguments)
        {
            // httpclient call other web service in async
        }
        #endregion
    }
}

I look into source code of lizzie. And see it does not support yet. I wish lizzie will support Task in its core. I think about using a convention to determine call async Task like "~", don't know it is correct or not ^_^

var(@response, ~call-other-api())
if(get(response, 'success'), { 
     ~send-email(get(response, 'email'), ....) 
}, {
     notify(get(response, 'eventId'), 'fail')
})
namespace lizzie.web_api.example.Controllers
{
    [Route("api/[controller]")]
    public class LizzieController : Controller
    {
        [HttpPost]
        public async Task<string> Post()
        {
            try
            {
                using (var reader = new StreamReader(Request.Body))
                {
                    string code = await reader.ReadToEndAsync();
                    var lambdaAsync = LambdaCompiler.CompileAsync(this, code);
                    var result = await lambdaAsync();
                    return result?.ToString() ?? string.Empty;
                }
            }
            catch (Exception err)
            {
                Response.StatusCode = 500;
                return err.Message;
            }
        }

        #region [ -- Lizzie functions -- ]

        [Bind(Name = "call-other-api")]
        async Task<object> CallOtherApi(Binder<LizzieController> binder, Arguments arguments)
        {
            //
        }

        [Bind(Name = "send-email")]
        async Task<object> CallOtherApi(Binder<LizzieController> binder, Arguments arguments)
        {
            //
        }

        [Bind(Name = "notify")]
        object CallOtherApi(Binder<LizzieController> binder, Arguments arguments)
        {
             var eventId = arguments.Get<Guid>(0);
             var msg = arguments.Get<string>(1);
             //...
        }

        #endregion
    }
}
polterguy commented 5 years ago

Hmm, although I do see your need, and your example code with sending emails obviously is a great example of the necessity for such features - Couldn't this be done by invoking the async/task method from the bound method, instead of having to support it directly from Lizzie?

Sorry if this is not the answer you are seeking, or if my understanding is inadequate (I'm no expert in the async/await/Task parts of C#) - But I am very reluctant to adding new features to the core of Lizzie, since its main USP is simplicity - Unless there is no way to support (a real problem, such as yours), without adding features to it ...

One alternative could possible be to simply spawn a new thread inside of your Binded method?

Or ...

... alternatively, if we could do this through some sort of "plugin" architecture, allowing us to keep Lizzie dead simple, yet still be able to solve your problem ...?

Suggestions ...?

hoangitk commented 5 years ago

^_^ Yes, I can invoke Task/Thread for "fire-and-forgot" method. Just my curious and greedy 😅 to know how can do it more, haha 😁 .

Thank you again for lizzie! ✌ Right now, I have a simple way to write DSL for Validator, Workflow, Like-Excel Formula Evaluator, Automation script,... hihi.

polterguy commented 5 years ago

Hehe, thx.

Let me know if you've got more ideas/suggestions :)