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

Make legacy fileOpen API accept configurable cleanup and rejection #45

Merged

Conversation

jmrog
Copy link
Contributor

@jmrog jmrog commented May 3, 2021

Here is a rough idea/proposal for handling the later discussion in #41.

@google-cla
Copy link

google-cla bot commented May 3, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@jmrog jmrog force-pushed the configurable-legacy-cleanup-and-reject branch from d29362b to e1b18e9 Compare May 3, 2021 17:49
@google-cla
Copy link

google-cla bot commented May 3, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@jmrog
Copy link
Contributor Author

jmrog commented May 4, 2021

@googlebot I signed it!

@tomayac
Copy link
Member

tomayac commented May 4, 2021

Thanks for opening this PR. I have to admit I'm not a fan of the way this complicates the API. Let me think a bit more if there is a different way.

I had more something in mind like the following:

import { fileOpen, supported } from 'browser-fs-access';

// … before
let file;
if (supported) {
  try {
    file = await fileOpen();
  } catch (err) {
    console.error(err);
  }
} else {
  file = await fileOpen();
}
// … after

I admit this looks not very elegant, but it makes clear that there is no magic. What do you think of this?

@jmrog
Copy link
Contributor Author

jmrog commented May 4, 2021

Thanks for opening this PR. I have to admit I'm not a fan of the way this complicates the API. Let me think a bit more if there is a different way.

Yeah, it does complicate the API a bit. I'm inclined to think it doesn't complicate things too much, though, especially given the payoff: the only change surfaced to the library user is that you can, if you wish, provide an optional callback that can be used to set up the conditions under which a cancel/rejection is triggered in the legacy API. The callback just sets the rejectionHandler and then returns a method that will be called on "cleanup" (i.e., when success occurs, or when the rejection condition is hit). This is a bit analogous to React's useEffect hook or React Router's history.listen API, so there's something like "prior art" for it that people may be familiar with. The payoff is that you can, if you wish, completely configure how rejection is both triggered and handled for the legacy API; fewer need-to-fix bug reports on browser-fs-access and more control for the developer (but it's opt-in; you don't need to deal with this part of the API at all).

I should've included the example of how it would work here, rather than just in the issue comments for #41, so I'm dropping the example here just for reference (note that I've removed the comments this time, in case that made it look more complicated than it is):

const file = await fileOpen({
  setupLegacyCleanupAndRejection: (rejectionHandler) => {
    const timeoutId = setTimeout(rejectionHandler, 10000);
    return (reject) => {
      clearTimeout(timeoutId);
      if (reject) {
        reject('My error message here.');
      }
    };
  },
});

If you don't want to use the option to configure this stuff, you can still of course just do:

const file = await fileOpen();

I had more something in mind like the following:

import { fileOpen, supported } from 'browser-fs-access';

// … before
let file;
if (supported) {
  try {
    file = await fileOpen();
  } catch (err) {
    console.error(err);
  }
} else {
  file = await fileOpen();
}
// … after

I admit this looks not very elegant, but it makes clear that there is no magic. What do you think of this?

I don't think I understand this part. Wasn't the goal to make rejection behavior configurable, so that the user could control how/when rejection occurs (that's what I took from the last line of this comment)? The example above doesn't seem to do that anywhere, but I could be missing it. Is the example supposed to show that the library itself would handle rejection if and only if supported is true, but would otherwise not handle the rejection and instead leave the handling up to the developer? That wouldn't be enough for us (i.e., not enough to fix #41 for us), because the library would still do the rejecting immediately in Chrome over HTTP (at least with the current implementation), even if it were to then be left up to us how to handle the rejection. We need some control over when a "rejection" is triggered in the first place (or else we need the library just not to reject at all -- which is why we've temporarily reverted back to v0.13.1 of the library). EDIT: Ah, now that I look more closely, I'm wondering if perhaps the example is showing that the library will just not reject in the case where supported is false?

Copy link
Member

@tomayac tomayac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, approval

Thank you! You have convinced me. Let's give it a try…

@tomayac tomayac merged commit 8997ed1 into GoogleChromeLabs:main May 5, 2021
@jmrog jmrog deleted the configurable-legacy-cleanup-and-reject branch May 6, 2021 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants