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

Commit b4896c9

Browse files
authored
Merge pull request #2626 from asturur/promisification
Update to promise api for some methods in the electron API
2 parents 8c07644 + 44d5d86 commit b4896c9

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

lib/views/directory-select.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ export default class DirectorySelect extends React.Component {
4545
);
4646
}
4747

48-
chooseDirectory = () => new Promise(resolve => {
49-
this.props.showOpenDialog(this.props.currentWindow, {
48+
chooseDirectory = async () => {
49+
const {filePaths} = await this.props.showOpenDialog(this.props.currentWindow, {
5050
defaultPath: this.props.buffer.getText(),
5151
properties: ['openDirectory', 'createDirectory', 'promptToCreate'],
52-
}, filePaths => {
53-
if (filePaths !== undefined) {
54-
this.props.buffer.setText(filePaths[0]);
55-
}
56-
resolve();
5752
});
58-
});
53+
if (filePaths.length) {
54+
this.props.buffer.setText(filePaths[0]);
55+
}
56+
}
5957
}

lib/views/git-timings-view.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,17 @@ class WaterfallWidget extends React.Component {
322322
this.setState(s => ({collapsed: !s.collapsed}));
323323
}
324324

325-
handleExportClick(e) {
325+
async handleExportClick(e) {
326326
e.preventDefault();
327327
const json = JSON.stringify(this.props.markers.map(m => m.serialize()), null, ' ');
328328
const buffer = new TextBuffer({text: json});
329-
dialog.showSaveDialog({
329+
const {filePath} = await dialog.showSaveDialog({
330330
defaultPath: 'git-timings.json',
331-
}, filename => {
332-
if (!filename) { return; }
333-
buffer.saveAs(filename);
334331
});
332+
if (!filePath) {
333+
return;
334+
}
335+
buffer.saveAs(filePath);
335336
}
336337
}
337338

@@ -419,22 +420,23 @@ export default class GitTimingsView extends React.Component {
419420
);
420421
}
421422

422-
handleImportClick(e) {
423+
async handleImportClick(e) {
423424
e.preventDefault();
424-
dialog.showOpenDialog({
425+
const {filePaths} = await dialog.showOpenDialog({
425426
properties: ['openFile'],
426-
}, async filenames => {
427-
if (!filenames) { return; }
428-
const filename = filenames[0];
429-
try {
430-
const contents = await fs.readFile(filename, {encoding: 'utf8'});
431-
const data = JSON.parse(contents);
432-
const restoredMarkers = data.map(item => Marker.deserialize(item));
433-
GitTimingsView.restoreGroup(restoredMarkers);
434-
} catch (_err) {
435-
atom.notifications.addError(`Could not import timings from ${filename}`);
436-
}
437427
});
428+
if (!filePaths.length) {
429+
return;
430+
}
431+
const filename = filePaths[0];
432+
try {
433+
const contents = await fs.readFile(filename, {encoding: 'utf8'});
434+
const data = JSON.parse(contents);
435+
const restoredMarkers = data.map(item => Marker.deserialize(item));
436+
GitTimingsView.restoreGroup(restoredMarkers);
437+
} catch (_err) {
438+
atom.notifications.addError(`Could not import timings from ${filename}`);
439+
}
438440
}
439441

440442
serialize() {

test/views/directory-select.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('DirectorySelect', function() {
2424
<DirectorySelect
2525
currentWindow={atomEnv.getCurrentWindow()}
2626
buffer={buffer}
27-
showOpenDialog={() => {}}
27+
showOpenDialog={() => Promise.resolve()}
2828
tabGroup={new TabGroup()}
2929
{...override}
3030
/>
@@ -47,7 +47,7 @@ describe('DirectorySelect', function() {
4747

4848
describe('clicking the directory button', function() {
4949
it('populates the destination path buffer on accept', async function() {
50-
const showOpenDialog = sinon.stub().callsArgWith(2, ['/some/directory/path']);
50+
const showOpenDialog = sinon.stub().returns(Promise.resolve({filePaths: ['/some/directory/path']}));
5151
const buffer = new TextBuffer({text: '/original'});
5252

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

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

6464
const wrapper = shallow(buildApp({showOpenDialog, buffer}));

test/worker-manager.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ describe('WorkerManager', function() {
167167
});
168168
`;
169169

170-
await new Promise(resolve => browserWindow.webContents.executeJavaScript(script, resolve));
170+
await browserWindow.webContents.executeJavaScript(script);
171171

172172
workerManager.destroy(true);
173173
workerManager = new WorkerManager();

0 commit comments

Comments
 (0)