From 95a994a1f856dbf01992bffc472e19fd926d8e91 Mon Sep 17 00:00:00 2001 From: Wliu <50Wliu@users.noreply.github.com> Date: Mon, 20 Nov 2017 19:30:15 +0100 Subject: [PATCH] Update Pane to use async showSaveDialog --- src/pane.js | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/pane.js b/src/pane.js index 0305b39dd..6fd515427 100644 --- a/src/pane.js +++ b/src/pane.js @@ -908,7 +908,7 @@ class Pane { // after the item is successfully saved, or with the error if it failed. // The return value will be that of `nextAction` or `undefined` if it was not // provided - saveItemAs (item, nextAction) { + async saveItemAs (item, nextAction) { if (!item) return if (typeof item.saveAs !== 'function') return @@ -919,22 +919,34 @@ class Pane { const itemPath = item.getPath() if (itemPath && !saveOptions.defaultPath) saveOptions.defaultPath = itemPath - const newItemPath = this.applicationDelegate.showSaveDialog(saveOptions) - if (newItemPath) { - return promisify(() => item.saveAs(newItemPath)) - .then(() => { - if (nextAction) nextAction() - }) - .catch(error => { - if (nextAction) { - nextAction(error) - } else { - this.handleSaveError(error, item) - } - }) - } else if (nextAction) { - return nextAction(new SaveCancelledError('Save Cancelled')) - } + let resolveSaveDialogPromise = null + const saveDialogPromise = new Promise(resolve => { resolveSaveDialogPromise = resolve }) + this.applicationDelegate.showSaveDialog(saveOptions, newItemPath => { + if (newItemPath) { + promisify(() => item.saveAs(newItemPath)) + .then(() => { + if (nextAction) { + resolveSaveDialogPromise(nextAction()) + } else { + resolveSaveDialogPromise() + } + }) + .catch(error => { + if (nextAction) { + resolveSaveDialogPromise(nextAction(error)) + } else { + this.handleSaveError(error, item) + resolveSaveDialogPromise() + } + }) + } else if (nextAction) { + resolveSaveDialogPromise(nextAction(new SaveCancelledError('Save Cancelled'))) + } else { + resolveSaveDialogPromise() + } + }) + + return await saveDialogPromise } // Public: Save all items.