w3c / FileAPI

File API
https://w3c.github.io/FileAPI/
Other
104 stars 44 forks source link

Support using AbortController with FileAPI #159

Open benjamingr opened 3 years ago

benjamingr commented 3 years ago

Hey,

It would be useful to use the web platform standard cancellation method to abort readers. Other web platform code (like fetch) supports cancellation through signals.

Code before:

async function readFile(file, { signal }) {
  var reader = new FileReader();
  const handler = e => reader.abort();
  signal.addEventListener('abort', handler, { once: true });
  reader.addEventListener('load', () => signal.removeEventListener('abort', handler));
  reader.addEventListener('error', () => signal.removeEventListener('abort', handler));
  const p = new Promise((resolve, reject) => {
    reader.onload = (e) => resolve(e.target.result);
    reader.onerror = (e) => reject(reader.error);
  });
  reader.readAsText(file);
  return await p;
}

Possible suggested change, assuming signal support:

async function readFile(file, { signal }) {
  var reader = new FileReader({ signal });
  const p = new Promise((resolve, reject) => {
    reader.onload = (e) => resolve(e.target.result);
    reader.onerror = (e) => reject(reader.error);
  });
  reader.readAsText(file);
  return await p;
}
domenic commented 3 years ago

Not speaking for the editors here, but for my part, I consider FileReader deprecated and replaced by the .stream(), .text(), and .arrayBuffer() methods.

benjamingr commented 3 years ago

@domenic Ah thanks, so in that case the ask should be to support signals in .text and .arrayBuffer?

Is it currently possible to abort an ongoing .text()?

Since it returns a promise and does not take a signal parameter I don't see/know how to do that.

domenic commented 3 years ago

Yeah, that would make sense to me. Indeed they cannot be aborted currently; you'd need to use .stream() + TextDecoderStream.

annevk commented 3 years ago

It would be a bit unfortunate to lose the symmetry with Request/Response, but maybe that's okay. I guess these are already different in that you can invoke them multiple times.

domenic commented 3 years ago

I mean, we could also add it to Request / Response's methods. Aborting in that case would probably throw away the entire body.

mkruisselbrink commented 3 years ago

Adding an AbortController to .text() and .arrayBuffer() seems reasonable to me. Agreed that I'd consider FileReader deprecated/legacy, especially now all major browsers support the new methods.

I agree that adding it to Request/Response (i.e. Body) as well would be ideal, but I don't think it would be too bad to only have it here either. It would still be true that you can do anything with these methods on Blob that you can do on other Bodys. And the reverse isn't true already.