flutter_web_preloader icon indicating copy to clipboard operation
flutter_web_preloader copied to clipboard

refactor: use fetch

Open lishaduck opened this issue 1 year ago • 2 comments

Description

This template uses both the fetch API and XMLHTTPRequest. I believe fetch is faster and it's certainly less verbose (which is faster to load, at least). I think it's worth doing mostly just for consistency though, as there's no reason to use XMLHTTPRequest + Promise.resolve if you only target environments supporting fetch.

Requirements

??

  • [ ] All CI/CD checks are passing.
  • [ ] There is no drop in the test coverage percentage.

Additional Context

I replaced it with this code, haven't extensively tested it though:

async function load(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(
        `Failed to load: ${response.status} ${response.statusText}`,
      );
    }
    return await response.text();
  } catch (error) {
    throw new Error("Network error");
  }
}

I also tweaked the loadPromises definition, but it doesn't really make a difference:

const loadPromises = urls.map(async (url) => {
  await load(url);
  await reportProgress();
});

I can PR, I just wanted to check if this was a change y'all agreed with, or if there was a reason for doing it this way in the first place.

lishaduck avatar Sep 20 '24 14:09 lishaduck

Hi @lishaduck 👋🏻, thanks for opening an issue!

I find your suggestion reasonable, altough I haven't looked much into both implementations to see if there are any discrepancies between them I yet don't know of, but consistency seems like a solid arguement.

The original author was @erickzanardo , I'll ping him to see if he would like to drop his two cents on this change.

Since the change is straightforward, I'll say to feel free in opening a Pull Request, if later on there are any objections (I currently doubt) we can work with it or close it.

alestiago avatar Sep 21 '24 07:09 alestiago

I feel this seems reaseonable!

erickzanardo avatar Sep 21 '24 11:09 erickzanardo