Bug 1525395 - Part 1: Make Scratchpad.openFile return a promise. r=jimb

Tests can use the promise to avoid racing on the text being available and the
UI ready for interaction.

Differential Revision: https://phabricator.services.mozilla.com/D20758

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jason Orendorff 2019-03-06 01:06:31 +00:00
Родитель 5b46393d34
Коммит 05d6cd72a1
1 изменённых файлов: 42 добавлений и 27 удалений

Просмотреть файл

@ -1087,18 +1087,30 @@ var Scratchpad = {
/**
* Open a file to edit in the Scratchpad.
*
*
* @param integer aIndex
* Optional integer: clicked menuitem in the 'Open Recent'-menu.
* If omitted, prompt the user to pick a file to open.
*
* @return Promise
* A Promise that resolves to undefined when the file is opened. The
* promise is rejected instead if the user cancels the file picker
* dialog, or if the file can't be opened.
*/
openFile: function SP_openFile(aIndex) {
const promptCallback = aFile => {
this.promptSave((aCloseFile, aSaved, aStatus) => {
let shouldOpen = aCloseFile;
if (aSaved && !Components.isSuccessCode(aStatus)) {
shouldOpen = false;
}
openFile(aIndex) {
return new Promise((resolve, reject) => {
const promptCallback = aFile => {
this.promptSave((aCloseFile, aSaved, aStatus) => {
let shouldOpen = aCloseFile;
if (aSaved && !Components.isSuccessCode(aStatus)) {
shouldOpen = false;
}
if (!shouldOpen) {
reject(new Error("cancelled"));
return;
}
if (shouldOpen) {
let file;
if (aFile) {
file = aFile;
@ -1118,29 +1130,32 @@ var Scratchpad = {
null);
this.clearFiles(aIndex, 1);
reject(new Error("file-no-longer-exists"));
return;
}
this.importFromFile(file, false);
}
});
};
this.importFromFile(file, false).then(_ => resolve(), reject);
});
};
if (aIndex > -1) {
promptCallback();
} else {
const fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(window, this.strings.GetStringFromName("openFile.title"),
Ci.nsIFilePicker.modeOpen);
fp.defaultString = "";
fp.appendFilter("JavaScript Files", "*.js; *.jsm; *.json");
fp.appendFilter("All Files", "*.*");
fp.open(aResult => {
if (aResult != Ci.nsIFilePicker.returnCancel) {
promptCallback(fp.file);
}
});
}
if (aIndex > -1) {
promptCallback();
} else {
const fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(window, this.strings.GetStringFromName("openFile.title"),
Ci.nsIFilePicker.modeOpen);
fp.defaultString = "";
fp.appendFilter("JavaScript Files", "*.js; *.jsm; *.json");
fp.appendFilter("All Files", "*.*");
fp.open(aResult => {
if (aResult == Ci.nsIFilePicker.returnCancel) {
reject(new Error("cancelled"));
} else {
promptCallback(fp.file);
}
});
}
});
},
/**