1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 06:07:59 +01:00

Factor out unnecessary and potentially problematic use of threadYield in editFieldExternally.

This commit is contained in:
Kris Maglione
2010-12-22 04:33:32 -05:00
parent 35a0917084
commit 9a1d78ee56
3 changed files with 71 additions and 61 deletions

View File

@@ -252,7 +252,7 @@ const Editor = Module("editor", {
}, },
// TODO: clean up with 2 functions for textboxes and currentEditor? // TODO: clean up with 2 functions for textboxes and currentEditor?
editFieldExternally: function (forceEditing) { editFieldExternally: function editFieldExternally(forceEditing) {
if (!options["editor"]) if (!options["editor"])
return; return;
@@ -280,10 +280,40 @@ const Editor = Module("editor", {
dactyl.assert(editor); dactyl.assert(editor);
text = Array.map(editor.rootElement.childNodes, function (e) util.domToString(e, true)).join(""); text = Array.map(editor.rootElement.childNodes, function (e) util.domToString(e, true)).join("");
} }
let oldBg, tmpBg; let oldBg, tmpBg;
function cleanup(error) {
if (timer)
timer.cancel();
if (error) {
dactyl.reportError(error, true);
tmpBg = "red";
}
else
dactyl.trapErrors(update, null, true);
if (tmpfile && tmpfile.exists())
tmpfile.remove(false);
if (textBox)
textBox.removeAttribute("readonly");
// blink the textbox after returning
if (textBox) {
let colors = [tmpBg, oldBg, tmpBg, oldBg];
(function next() {
textBox.style.backgroundColor = colors.shift();
if (colors.length > 0)
util.timeout(next, 100);
})();
}
}
try { try {
let res = io.withTempFiles(function (tmpfile) { var tmpfile = io.createTempFile();
if (!tmpfile)
throw Error("Couldn't create temporary file");
if (textBox) { if (textBox) {
textBox.setAttribute("readonly", "true"); textBox.setAttribute("readonly", "true");
oldBg = textBox.style.backgroundColor; oldBg = textBox.style.backgroundColor;
@@ -311,41 +341,11 @@ const Editor = Module("editor", {
} }
} }
let timer = services.Timer(update, 100, services.Timer.TYPE_REPEATING_SLACK); var timer = services.Timer(update, 100, services.Timer.TYPE_REPEATING_SLACK);
this.editFileExternally({ file: tmpfile.path, line: line, column: column }, cleanup);
try {
this.editFileExternally({ file: tmpfile.path, line: line, column: column }, true);
}
finally {
timer.cancel();
}
update(true);
}, this);
if (res == false)
throw Error("Couldn't create temporary file");
} }
catch (e) { catch (e) {
// Errors are unlikely, and our error messages won't cleanup(e);
// likely be any more helpful than that given in the
// exception.
dactyl.reportError(e, true);
tmpBg = "red";
}
finally {
if (textBox)
textBox.removeAttribute("readonly");
}
// blink the textbox after returning
if (textBox) {
let colors = [tmpBg, oldBg, tmpBg, oldBg];
(function next() {
textBox.style.backgroundColor = colors.shift();
if (colors.length > 0)
util.timeout(next, 100);
})();
} }
}, },

View File

@@ -200,9 +200,9 @@ const IO = Module("io", {
*/ */
createTempFile: function () { createTempFile: function () {
let file = services.directory.get("TmpD", Ci.nsIFile); let file = services.directory.get("TmpD", Ci.nsIFile);
file.append(config.tempFile); file.append(config.tempFile);
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, octal(600)); file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, octal(600));
Cc["@mozilla.org/uriloader/external-helper-app-service;1"] Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
.getService(Ci.nsPIExternalAppLauncher).deleteTemporaryFileOnExit(file); .getService(Ci.nsPIExternalAppLauncher).deleteTemporaryFileOnExit(file);
@@ -279,7 +279,7 @@ lookup:
function () { function () {
if (!process.isRunning) { if (!process.isRunning) {
timer.cancel(); timer.cancel();
callback(); dactyl.trapErrors(blocking);
} }
}, },
100, services.Timer.TYPE_REPEATING_SLACK); 100, services.Timer.TYPE_REPEATING_SLACK);

View File

@@ -1230,6 +1230,16 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
while (flush === true && mainThread.hasPendingEvents()); while (flush === true && mainThread.hasPendingEvents());
}, },
yieldable: function yieldable(func)
function magic() {
let gen = func.apply(this, arguments);
(function next() {
try {
util.timeout(next, gen.next());
} catch(e if e instanceof StopIteration) {};
})();
},
/** /**
* Traps errors in the called function, possibly reporting them. * Traps errors in the called function, possibly reporting them.
* *