tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
927 stars 259 forks source link

[upload] Downloading doesn't report the correct progress #1266

Closed pxdl closed 5 months ago

pxdl commented 5 months ago

When trying to download zip files using the @tauri-apps/plugin-upload plugin, the progress callback isn't reporting the correct length of the chunks being downloaded.

Here's an example (using console.log) of the total chunks reported not matching the entire size of the file:

Saving
Downloaded 1371 of 820420 bytes
Downloaded 2742 of 820420 bytes
Downloaded 4113 of 820420 bytes
Downloaded 5484 of 820420 bytes
Downloaded 6855 of 820420 bytes
Asset downloaded

Here's an example of the total chunks reported properly matching the actual size of the file:

Saving
Downloaded 1371 of 5210 bytes
Downloaded 2742 of 5210 bytes
Downloaded 4113 of 5210 bytes
Downloaded 5210 of 5210 bytes
Asset downloaded

Some bigger files also work and other don't, so I don't think it has to do with the size of the file.

Here's my JavaScript code calling the @tauri-apps/plugin-upload plugin:

let receivedLength = 0;

// Download asset
await download(assetUrl, downloadPathTest, ({ progress, total }) => {
  receivedLength += progress;
  console.log(`Downloaded ${receivedLength} of ${total} bytes`);
});

Is there some kind of compression going on during the download? How to report the actual progress?

Legend-Master commented 5 months ago

This will probably get fixed in #1237, we need to update tauri api version to apply the fix made in https://github.com/tauri-apps/tauri/pull/9463

FabianLars commented 5 months ago

New packages were released, can you make sure to update all tauri related packages (both rust and js) and then try again?

pxdl commented 5 months ago

After updating all packages to the latest version, this bug seems to have been fixed, thank you!

pxdl commented 5 months ago

After some testing, I noticed that the plugin worked fine at first, even when downloading large files. But now it doesn't seem to be working properly, it seems to choke when sending updates to the channel and only properly updates when the download finishes, especially with large files. After this happens, lots of errors pop up on the console after reloading the page, which can be further detailed in the Network tab:

image

These errors keep popping up until I restart the program completely, reloading the page isn't enough.

This is how I'm updating the download progress state (I'm passing down the handleDownloadProgress function to where I call the plugin):

function reducer(state, action) {
  switch (action.type) {
    case 'reset':
      return { total: 0, downloaded: 0 };
    case 'downloaded_chunk':
      return {
        downloaded:
          action.total !== state.total
            ? action.chunk
            : state.downloaded + action.chunk,
        total: action.total,
      };
    default:
      throw Error('Unknown action.');
  }
}

function Updater() {
const [downloadState, dispatchDownloadState] = useReducer(reducer, {
    total: 0,
    downloaded: 0,
  });

const handleDownloadProgress = ({
    progress,
    total,
  }: {
    progress: number;
    total: number;
  }) => {
    dispatchDownloadState({ type: 'downloaded_chunk', chunk: progress, total });
  };

  return (
    <Text>{`${downloadState.downloaded} bytes / ${downloadState.total} bytes`}</Text>
  );
}

Here's a small gif showing what happens. This was working fine a while ago, updating in real time: msedgewebview2_CKELyv4L2l

FabianLars commented 5 months ago

Fairly sure this is not the plugin's fault but an issue with the Channel api. I could swear i talked about this somewhere before but i can't find the issue or discord discussion 🤔

pxdl commented 5 months ago

It seems related to this issue on the tauri repository: https://github.com/tauri-apps/tauri/issues/8177

Given that the issue doesn't seem to be related to the plugin, but to the channel API, I'm closing it again.

@FabianLars I noticed that you implemented some sort of workaround on https://github.com/tauri-apps/plugins-workspace/commit/dd8bc4ab399582e876f84116333b3e29bf3ceee6, are there any plans to implement event emission throttling on the main branch?

FabianLars commented 5 months ago

I'm not sure if it's really 8177. This emit issue makes the whole app crash while we're just talking about lost events. Considering that we're talking about v1 vs v2 it may be the same issue just without the crash due to refactoring, not sure.

As long as we're in beta (hoping that we figure out the issue) i won't add the throttle. Once we're running out of time and don't have a fix, i'll add it though.