Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cancel a compression in progress [feature request] #101

Closed
felipeaf opened this issue Apr 23, 2021 · 4 comments
Closed

cancel a compression in progress [feature request] #101

felipeaf opened this issue Apr 23, 2021 · 4 comments

Comments

@felipeaf
Copy link

Some compressions (very large resolutions and bytes in size) can take a while and be CPU expensive. It should be a way to cancel a compression in progress in WebWorker mode (example of use cases: in SPA, when user remove file from form or change file again).

@SharpDevSa
Copy link

It'd be great if you expose the webworker.terminate () method only for the web workers for now.

@handbremse
Copy link

Something like a cancelToken that contains a Promise, which can be resolved/rejected by the user.

@Donaldcwl
Copy link
Owner

In the coming releases, we will use AbortController for canceling mechanism:
https://developer.mozilla.org/en-US/docs/Web/API/AbortController

reference: https://axios-http.com/docs/cancellation

@Donaldcwl
Copy link
Owner

This feature is implemented in v2.0.0. For example:

function handleImageUpload(event) {

  var imageFile = event.target.files[0];

  var controller = new AbortController();

  var options = {
    // other options here
    signal: controller.signal,
  }
  imageCompression(imageFile, options)
    .then(function (compressedFile) {
      return uploadToServer(compressedFile); // write your own logic
    })
    .catch(function (error) {
      console.log(error.message); // output: I just want to stop
    });
  
  // simulate abort the compression after 1.5 seconds
  setTimeout(function () {
    controller.abort(new Error('I just want to stop'));
  }, 1500);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants