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

Commit

Permalink
Merge pull request #2626 from asturur/promisification
Browse files Browse the repository at this point in the history
Update to promise api for some methods in the electron API
  • Loading branch information
smashwilson authored Feb 14, 2021
2 parents 8c07644 + 44d5d86 commit b4896c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
14 changes: 6 additions & 8 deletions lib/views/directory-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ export default class DirectorySelect extends React.Component {
);
}

chooseDirectory = () => new Promise(resolve => {
this.props.showOpenDialog(this.props.currentWindow, {
chooseDirectory = async () => {
const {filePaths} = await this.props.showOpenDialog(this.props.currentWindow, {
defaultPath: this.props.buffer.getText(),
properties: ['openDirectory', 'createDirectory', 'promptToCreate'],
}, filePaths => {
if (filePaths !== undefined) {
this.props.buffer.setText(filePaths[0]);
}
resolve();
});
});
if (filePaths.length) {
this.props.buffer.setText(filePaths[0]);
}
}
}
38 changes: 20 additions & 18 deletions lib/views/git-timings-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,17 @@ class WaterfallWidget extends React.Component {
this.setState(s => ({collapsed: !s.collapsed}));
}

handleExportClick(e) {
async handleExportClick(e) {
e.preventDefault();
const json = JSON.stringify(this.props.markers.map(m => m.serialize()), null, ' ');
const buffer = new TextBuffer({text: json});
dialog.showSaveDialog({
const {filePath} = await dialog.showSaveDialog({
defaultPath: 'git-timings.json',
}, filename => {
if (!filename) { return; }
buffer.saveAs(filename);
});
if (!filePath) {
return;
}
buffer.saveAs(filePath);
}
}

Expand Down Expand Up @@ -419,22 +420,23 @@ export default class GitTimingsView extends React.Component {
);
}

handleImportClick(e) {
async handleImportClick(e) {
e.preventDefault();
dialog.showOpenDialog({
const {filePaths} = await dialog.showOpenDialog({
properties: ['openFile'],
}, async filenames => {
if (!filenames) { return; }
const filename = filenames[0];
try {
const contents = await fs.readFile(filename, {encoding: 'utf8'});
const data = JSON.parse(contents);
const restoredMarkers = data.map(item => Marker.deserialize(item));
GitTimingsView.restoreGroup(restoredMarkers);
} catch (_err) {
atom.notifications.addError(`Could not import timings from ${filename}`);
}
});
if (!filePaths.length) {
return;
}
const filename = filePaths[0];
try {
const contents = await fs.readFile(filename, {encoding: 'utf8'});
const data = JSON.parse(contents);
const restoredMarkers = data.map(item => Marker.deserialize(item));
GitTimingsView.restoreGroup(restoredMarkers);
} catch (_err) {
atom.notifications.addError(`Could not import timings from ${filename}`);
}
}

serialize() {
Expand Down
6 changes: 3 additions & 3 deletions test/views/directory-select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('DirectorySelect', function() {
<DirectorySelect
currentWindow={atomEnv.getCurrentWindow()}
buffer={buffer}
showOpenDialog={() => {}}
showOpenDialog={() => Promise.resolve()}
tabGroup={new TabGroup()}
{...override}
/>
Expand All @@ -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']}));
const buffer = new TextBuffer({text: '/original'});

const wrapper = shallow(buildApp({showOpenDialog, buffer}));
Expand All @@ -58,7 +58,7 @@ describe('DirectorySelect', function() {
});

it('leaves the destination path buffer unmodified on cancel', async function() {
const showOpenDialog = sinon.stub().callsArgWith(2, undefined);
const showOpenDialog = sinon.stub().returns(Promise.resolve({filePaths: []}));
const buffer = new TextBuffer({text: '/original'});

const wrapper = shallow(buildApp({showOpenDialog, buffer}));
Expand Down
2 changes: 1 addition & 1 deletion test/worker-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('WorkerManager', function() {
});
`;

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

workerManager.destroy(true);
workerManager = new WorkerManager();
Expand Down

0 comments on commit b4896c9

Please sign in to comment.