Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Update to promise api for some methods in the electron API #2626

Merged
merged 6 commits into from
Feb 14, 2021

Conversation

asturur
Copy link
Contributor

@asturur asturur commented Feb 13, 2021

Please be sure to read the contributor's guide to the GitHub package before submitting any pull requests.

Requirements

  • Filling out the template is required. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.

Description of the Change

This PR aims at using the promise version of

  • showSaveDialog,
  • showOpenDialog,
  • executeJavascript

electron API

In order to ease the upgrade path to newer versions of electron where the callback is no more supported.

Screenshot or Gif

Applicable Issues

@codecov
Copy link

codecov bot commented Feb 13, 2021

Codecov Report

Merging #2626 (44d5d86) into master (8c07644) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2626      +/-   ##
==========================================
- Coverage   93.46%   93.46%   -0.01%     
==========================================
  Files         237      237              
  Lines       13215    13213       -2     
  Branches     1900     1900              
==========================================
- Hits        12352    12349       -3     
- Misses        863      864       +1     
Impacted Files Coverage Δ
lib/views/directory-select.js 100.00% <100.00%> (ø)
lib/atom/gutter.js 90.47% <0.00%> (-2.39%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c07644...44d5d86. Read the comment docs.

Copy link
Contributor

@smashwilson smashwilson left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this! I'll be happy to merge if we can resolve:

  • Using async/await instead of .then (which is just a style/consistency thing), and
  • Confirming that this will still work against Atom 1.55, or finding a way to bridge the Electron API gap, since the return value of showOpenDialog has changed.

this.props.showOpenDialog(this.props.currentWindow, {
defaultPath: this.props.buffer.getText(),
properties: ['openDirectory', 'createDirectory', 'promptToCreate'],
}, filePaths => {
if (filePaths !== undefined) {
}).then(({filePaths}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd prefer to use the async/await style to call the Promise-based APIs here for consistency, if you don't mind 🙇🏻 .

Also, it looks like the return value of showOpenDialog has changed, too; it now returns an Object with certain properties:

https://github.com/electron/electron/blob/v9.4.3/docs/api/dialog.md#dialogshowopendialogbrowserwindow-options

So I think this call would be something like:

chooseDirectory = async () => {
  const {canceled, filePaths} = await this.props.showOpenDialog(this.props.currentWindow, {
      defaultPath: this.props.buffer.getText(),
      properties: ['openDirectory', 'createDirectory', 'promptToCreate'],
  };
  if (!canceled && filePaths.length > 0) {
    this.props.buffer.setText(filePaths[0]);
  }
}

But, we have to make sure that the different return value is compatible with the call signature on the Electron versions back to Atom 1.55 (the current beta), too, so we don't break our ability to backport 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Hah! I totally missed at first read that you'd already added the {filePaths} destructuring in your .then callback argument.

@@ -47,7 +47,7 @@ describe('DirectorySelect', function() {

describe('clicking the directory button', function() {
it('populates the destination path buffer on accept', async function() {
const showOpenDialog = sinon.stub().callsArgWith(2, ['/some/directory/path']);
const showOpenDialog = sinon.stub().returns(Promise.resolve({filePaths: ['/some/directory/path']}));
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Perfect.

@@ -167,7 +167,7 @@ describe('WorkerManager', function() {
});
`;

await new Promise(resolve => browserWindow.webContents.executeJavaScript(script, resolve));
await browserWindow.webContents.executeJavaScript(script);
Copy link
Contributor

Choose a reason for hiding this comment

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

🙇🏻

@asturur
Copy link
Contributor Author

asturur commented Feb 14, 2021

i ll get to requested changes soon

@asturur
Copy link
Contributor Author

asturur commented Feb 14, 2021

@smashwilson if atom 1.55 is based on electron 6 those api change will work.
The callback was removed from documentation on electron 6 and still supported, but this promise API has been introduced in 6.0

@smashwilson
Copy link
Contributor

Looks like Atom 1.55 uses Electron 6.1.12:

https://github.com/atom/atom/blob/24274440ec3e9c296a20daa3ad72aa57c074024a/package.json#L15

And here are the showOpenDialog docs for that version:

https://github.com/electron/electron/blob/v6.1.12/docs/api/dialog.md#dialogshowopendialogbrowserwindow-options

So you're correct, looks like we're good 👍🏻

if (filePaths.length) {
this.props.buffer.setText(filePaths[0]);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Perfect!

buffer.saveAs(filename);
}).then(({filePath}) => {
if (!filePath) { return; }
buffer.saveAs(filePath);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use async/await here too?

}, async filenames => {
if (!filenames) { return; }
const filename = filenames[0];
}).then(async ({filePaths}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

And here ☝🏻

Sorry, should have clarified that I meant at all three callsites on the first review 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh ok no issue, i can convert them, i m still here.

Copy link
Contributor

@smashwilson smashwilson left a comment

Choose a reason for hiding this comment

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

Excellent, thank you. Will merge when builds are green 🍏

@smashwilson smashwilson merged commit b4896c9 into atom:master Feb 14, 2021
@asturur
Copy link
Contributor Author

asturur commented Feb 14, 2021

Is it possible to get a new version published? what is your procedure around that?

@smashwilson
Copy link
Contributor

Ah, to make it easier to move forward the Electron upgrade work? Sure, I can do that:

atom-github ➤ apm publish patch
Preparing and tagging a new version ✓
Pushing v0.36.8 tag ✓
Publishing github@v0.36.8 ✓

@asturur
Copy link
Contributor Author

asturur commented Feb 16, 2021

thanks @smashwilson appreciated

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

Successfully merging this pull request may close these issues.

None yet

2 participants