curiosity-ai / h5

🚀 The next generation C# to JavaScript compiler

Home Page:https://h5.rocks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Task didn't work in h5

Ali1Jamal opened this issue · comments

Hello:
i'm got 4 errors when i'm run the code
first error is ' Task is not yet completed '
and the others errors is ' Task is already completed '

code here :

namespace System.Net.Http
{
    public class HttpClient
    {
        private static uint timeout = 100000;
        public static uint Timeout { get { return timeout; }
            set {
                if (value >= uint.MaxValue)
                {
                    timeout = uint.MaxValue ;
                }
                else
                {
                    timeout = value;
                }
            }
        }

        public static async Task<XMLHttpRequest> GetAsync(string requestUri)
        {
            var tcs = new TaskCompletionSource<XMLHttpRequest>();
       
             XMLHttpRequest xmlHttp = new XMLHttpRequest();
            xmlHttp.timeout = Timeout;
            xmlHttp.open("GET", requestUri);
     
            xmlHttp.onreadystatechange = async (e) =>
            {
                if(xmlHttp.status == 200)
                {
                    tcs.SetResult(xmlHttp);
                }
                else
                {
                    tcs.SetException(new Exception("Response status code does not indicate success: " + xmlHttp.statusText));
                }
            };
            xmlHttp.send();
            return await tcs.Task;

        }
    }
}

It does not make any sense in setter with type uint:

if (value >= uint.MaxValue)
                {
                    timeout = uint.MaxValue ;

Why is it async?

.onreadystatechange = async (e) =>

It is C# code and it is public Action OnReadyStateChange property.

All your code looks at least strange.

@hardhub
1- I made it uint to force the user to enter a positive number .... I add the if because there might be a bug or something in the future that could cause the problem like number bigger than uint max value (i can remove it if you like ).
2-I didn't focus on anything just focus on that bug in task even if I remove the async, the problem will still exist.

uint is not CLS-compliant, a check itself does not make any sense (if internal represnation of uint can be greater than uint it is problem of uint and not property of HttpClient), that API is not standard - HttpClient in .Net has Timeout instance (TimeSpan) property.

But as I said whole code looks strange. It does not look like correct async programming. Though I am not involved a lot into your code.

But if I need to return Task<XMLHttpRequest> then I can simply can do the following:

XMLHttpRequest request = new XMLHttpRequest();

return Task.FromResult(request);

or await Task.FromResult(...) if it is async method...

So, it is only my opinion, but H5 should not be a playing place or place for school labs.
To be better than original Bridge.NET implementation it needs robust professional code.

my guess from using XMLHttpRequest is that xmlHttp.onreadystatechange is called multiple times, and as you're only checking the status code, it's probably being called twice and the status code 200 was already set.

For reference, this is how we used internally on our code - this sample isn't complete but gives you an idea of the behaviour:

            var tcs = new TaskCompletionSource<T>();

            xmlHttp.onreadystatechange = e =>
            {
                if (HasCancellationBeenRequested())
                {
                    // 2020-03-03 DWR: This onreadystatechange will be made if the request was aborted but with a weird combination of readyState 4 and status 0 and I don't know for sure if that behaviour is documented and
                    // so, instead, we'll check whether we had a cancellation token that was cancelled because that should be the only way that a request abort could occur and then 
                    tcs.TrySetCanceled();
                    tcs = null;
                    return;
                }

                if (xmlHttp.readyState == 0)
                {
                    tcs.TrySetCanceled();
                    tcs = null;
                    return;
                }

                if (xmlHttp.readyState == 4 /*AjaxReadyState.Done*/)
                {
                    if (xmlHttp.status == 200 || xmlHttp.status == 201 || xmlHttp.status == 304)
                    {
                        tcs.TrySetResult(responseRetriever(xmlHttp));
                        tcs = null;
                    }
                    else if (xmlHttp.status == 400) // Bad request
                    {
                        tcs.TrySetException(new BadRequestException(TryToReadResponseText(xmlHttp), _url + (_parameter ?? "")));
                        tcs = null;
                    }
                    else if (xmlHttp.status == 401) // Unauthorized
                    {
                        tcs.TrySetException(new UnauthorizedAccessException(TryToReadResponseText(xmlHttp)));
                        tcs = null;
                    }
                    else if (xmlHttp.status == 403) // Forbidden
                    {
                            tcs.TrySetException(new ForbiddenAccessException(TryToReadResponseText(xmlHttp)));
                            tcs = null;
                    }
                    else if (xmlHttp.status == 404) // Not found
                    {
                        tcs.TrySetException(new NotFoundException(TryToReadResponseText(xmlHttp)));
                        tcs = null;
                    }
                    else if (xmlHttp.status == 409) // Conflict
                    {
                        tcs.TrySetException(new ConflictException(TryToReadResponseText(xmlHttp)));
                        tcs = null;
                    }
                    else if (xmlHttp.status == 429) // Too many requests
                    {
                        tcs.TrySetException(new TooManyRequestsReceivedException());
                        tcs = null;
                    }
                    else if (xmlHttp.status == 503 || xmlHttp.status == 0) // Unavailable
                    {
                        tcs.TrySetException(new ServerNotAvailableException());
                        tcs = null;
                    }
                    else
                    {
                        tcs.TrySetException(new Exception(TryToReadResponseText(xmlHttp)));
                        tcs = null;
                    }
                }
            };

@hardhub as i tell you before i'm I didn't focus on my code very well because the task didn't work so i change the code a lot like more than 5 times to make it work but it doesn't , this implementation It's not official yet and it's just a experimental for my self ...just from the first half an hour, problems began to appear......And you expect me to write clean code because I spent the whole day looking for a solution to the problem ....
It is better for you not to focus on the unimportant aspects, it is better to focus on the main problem (Task).. You should not mock of the other person In directly or indirectly, and to judge a person just because of a simple mistake and start with empty talk because you do not know what is in his day and the problems in his life. So You should learn to say good or keep quiet.

@theolivenbaum ok thanks i well try

@hardhub as i tell you before i'm I didn't focus on my code very well because the task didn't work so i change the code a lot like more than 5 times to make it work but it doesn't , this implementation It's not official yet and it's just a experimental for my self ...just from the first half an hour, problems began to appear......And you expect me to write clean code because I spent the whole day looking for a solution to the problem .... It is better for you not to focus on the unimportant aspects, it is better to focus on the main problem (Task).. You should not mock of the other person In directly or indirectly, and to judge a person just because of a simple mistake and start with empty talk because you do not know what is in his day and the problems in his life. So You should learn to say good or keep quiet.

No, I should not. When I expect the problems I must to say about my doubts (you can agree or not - it is another question).

Even more you should not tell me what I should do. As well as you are free to do what you want (and of course it does not mean that I will automatically agree with the result).

@hardhub as i tell you before i'm I didn't focus on my code very well because the task didn't work so i change the code a lot like more than 5 times to make it work but it doesn't , this implementation It's not official yet and it's just a experimental for my self ...just from the first half an hour, problems began to appear......And you expect me to write clean code because I spent the whole day looking for a solution to the problem .... It is better for you not to focus on the unimportant aspects, it is better to focus on the main problem (Task).. You should not mock of the other person In directly or indirectly, and to judge a person just because of a simple mistake and start with empty talk because you do not know what is in his day and the problems in his life. So You should learn to say good or keep quiet.

No, I should not. When I expect the problems I must to say about my doubts (you can agree or not - it is another question).

Even more you should not tell me what I should do. As well as you are free to do what you want (and of course it does not mean that I will automatically agree with the result).

i tell you i didn't focues very well and told you that I will delete it if you like because I am not interested in it and it came as a result of many edits in the code because the problem .... I appreciate that you are trying to help, but you kept insisting on the side issue and start making empty talking, for example,h5 it was not a place to play and not a place for school labs ... etc. did you think I'm playing here to say that?? When you cross the border of freedom of other person then I have the right to remind you of your limits and when you should and shouldn't ....Now I've had enough of this conversation

I have probably repeat what I mentioned... I did not pass any limits. I just said that your attempt to write bridge version of HttpClient looks as some playing with code. So yes like a lab... And it is what I personally do not like to see in repo. In the past Bridge was a victim of tons of bugs. So I mentioned only that nothing else. If anybody else try to introduce some code which in theory breaks cross-compilation my reaction will be the same (I have tons of #if BRIDGE directives in code and of course I do not like that).
So never mind, nothing personal...

I have probably repeat what I mentioned... I did not pass any limits. I just said that your attempt to write bridge version of HttpClient looks as some playing with code. So yes like a lab... And it is what I personally do not like to see in repo. In the past Bridge was a victim of tons of bugs. So I mentioned only that nothing else. If anybody else try to introduce some code which in theory breaks cross-compilation my reaction will be the same (I have tons of #if BRIDGE directives in code and of course I do not like that). So never mind, nothing personal...

ok i will repeat my talk .....i told you before i well implement httpClint ...but this code just trying to write task ...nothing public or real implement...just few lines to test (i mean this code is for test Task if task work than i will do the standard in corert like timespan ....etc ). you can be helpful like theolivenbaum he know what i want and solve the problem ....i dont like to complete this message ....so i will leave this conversation don't reply ok👍