magiccodingman / Magic.IndexedDb

Use IndexedDb in Blazor like LINQ!
MIT License
28 stars 8 forks source link

avoid callback #2

Open yueyinqiu opened 1 year ago

yueyinqiu commented 1 year ago

Hello, I'm here again.

Actually I was trying to find out 'what will happen when the primary key get duplicate' but haven't caught any exceptions. So I dived into your code and fount that all the JSExceptions are caught there and the ActionCompleted event was raised. This confused me a lot since there seems to be no reason to do that, and both you and the users will find it inconvenient to deal with the event and the transactions.

Well, later when I found that all the js functions are returning nothing in direct but just using .then and .catch, I think I understand all of that. However, Blazor JavaScript interop could directly deal with promises:

// js part
window.func = function (doReject)
{
    return new Promise(
        (resolve, reject) => setTimeout(() =>
        {
            if (doReject)
                reject("Task Failed");
            else
                resolve("Task Completed Successfully");
        }, 1000));
}
        // c# part
        var result = await js.InvokeAsync<string>("func", false);
        logger.LogInformation(result);
        // Output: Task Completed Successfully

        try
        {
            await js.InvokeAsync<string>("func", true);
        }
        catch(JSException e)
        {
            logger.LogError(e.Message);
            // Output: Task Failed
        }

Meanwhile, an async js method might be a better choice:

window.delay = function (ms)
{
    return new Promise(resolve => setTimeout(resolve, ms));
}

window.func = async function (doReject)
{
    await delay(1000);
    if (doReject)
        throw "Task Failed";
    return "Task Completed Successfully";
}

And CancellationTokenSources are even able to be used to cancel the promises: Abort a long-running JavaScript function

So my suggestion is to avoid using callbacks but to use the above measures instead. Hope you won't hate me this time, since it will be a really great change to the whole project. Of course, it's just a suggestion and the choice is yours.

magiccodingman commented 1 year ago

@yueyinqiu This is totally fine! Let me try to implement something like this actually. I've been away from the project for a bit. Aka, sorry for the late response. But last time I was working on the code, I was doing something very similar. I was reworking the callback completely as well. So, we both were funnily on the same page! The current version of the callback was implemented by the project I branched from. There's reasons to have it the way he coded it. But since we're both on the same page, it means we both understand the inherit disadvantages of the current callback system.

As far as I know, the version the old code used with callback is good for extreme high flow scenarios. At least, that's what Chat GPT said haha. Because I still struggle to grasp the concept of the real advantages of why it was coded that way. But yes, I 100% agree.

My company finished a huge portion of the project recently which puts me on the path towards actually implementing the code I published here. But now that my daily life will be implementing this code, I'll be back full force to focus on it and updates like this will be definitely implemented :)

I'm going to leave this comment thread open as I work on this.