1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 14:57:59 +01:00

Decruftify crufty old gunk, promisify stuff.

This commit is contained in:
Kris Maglione
2015-03-02 19:01:19 -08:00
parent 7b2f821e04
commit 29e614ab74
2 changed files with 78 additions and 98 deletions

View File

@@ -373,7 +373,7 @@ var Editor = Module("editor", XPCOM(Ci.nsIEditActionListener, ModuleBase), {
dactyl.assert(args.length >= 1, _("option.notSet", "editor")); dactyl.assert(args.length >= 1, _("option.notSet", "editor"));
io.run(args.shift(), args, blocking); return io.run(args.shift(), args, blocking);
}, },
// TODO: clean up with 2 functions for textboxes and currentEditor? // TODO: clean up with 2 functions for textboxes and currentEditor?

View File

@@ -807,8 +807,7 @@ var Buffer = Module("Buffer", {
/** /**
* Saves the contents of a URI to disk. * Saves the contents of a URI to disk.
* *
* @param {nsIURI} uri The URI to save * @returns {Promise}
* @param {nsIFile} file The file into which to write the result.
*/ */
saveURI: function saveURI(params) { saveURI: function saveURI(params) {
if (params instanceof Ci.nsIURI) if (params instanceof Ci.nsIURI)
@@ -816,42 +815,34 @@ var Buffer = Module("Buffer", {
params = { uri: arguments[0], file: arguments[1], params = { uri: arguments[0], file: arguments[1],
callback: arguments[2], self: arguments[3] }; callback: arguments[2], self: arguments[3] };
var persist = services.Persist(); let promise = new CancelablePromise(promises.task(function* (resolve, reject, canceled) {
persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE if (params.isPrivate === undefined) {
| persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
let window = this.topWindow;
let privacy = sanitizer.getContext(params.context || this.win); let privacy = sanitizer.getContext(params.context || this.win);
let file = File(params.file); params.isPrivate = privacy.usePrivateBrowsing;
if (!file.exists()) }
file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
let downloadListener = new window.DownloadListener(window, let download = Downloads.createDownload({
services.Transfer(params.uri, file.URI, "", null, null, null, source: {
persist, privacy && privacy.usePrivateBrowsing)); url: params.uri.spec,
isPrivate: params.isPrivate,
var { callback, self } = params; referrer: params.referrer,
if (callback) },
persist.progressListener = update(Object.create(downloadListener), { target: params.file,
onStateChange: util.wrapCallback(function onStateChange(progress, request, flags, status) {
if (callback && (flags & Ci.nsIWebProgressListener.STATE_STOP) && status == 0)
util.trapErrors(callback, self, params.uri, file.file,
progress, request, flags, status);
return onStateChange.superapply(this, arguments);
})
}); });
else canceled.then(download.cancel);
persist.progressListener = downloadListener;
if (persist.saveURI.length <= 7) let list = yield Downloads.getList(Downloads.ALL);
persist.saveURI(params.uri, null, null, null, null, list.add(download);
file.file, privacy);
else return download.start();
// Let's add an extra null to the middle of the arguments }));
// list, because why not.
persist.saveURI(params.uri, null, null, null, null, null, // Deprecated.
file.file, privacy); if (params.callback)
promise.then(() => {
params.callback.call(params.self, params.uri, params.file);
});
return promise;
}, },
/** /**
@@ -1235,25 +1226,22 @@ var Buffer = Module("Buffer", {
* immediately. * immediately.
* *
* @param {Document} doc The document to view. * @param {Document} doc The document to view.
* @param {function|object} callback If a function, the callback to be * @returns {Promise}
* called with two arguments: the nsIFile of the file, and temp, a
* boolean which is true if the file is temporary. Otherwise, an object
* with line and column properties used to determine where to open the
* source.
* @optional
*/ */
viewSourceExternally: Class("viewSourceExternally", viewSourceExternally: function viewSourceExternally(doc, loc) {
XPCOM([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]), { return this.fetchSourceExternally(doc).then(([file, isTemp]) => {
init: function init(doc, callback) {
this.callback = callable(callback) ? callback :
function (file, temp) {
let { editor } = overlay.activeModules; let { editor } = overlay.activeModules;
editor.editFileExternally(update({ file: file.path }, callback || {}), editor.editFileExternally(update({ file: file.path }, loc))
function () { temp && file.remove(false); }); .then(() => {
return true; if (isTemp)
}; file.remove(false);
});
});
},
fetchSourceExternally: function fetchSourceExternally(doc) {
return new Promise((resolve, reject) => {
if (isString(doc)) { if (isString(doc)) {
var privacyContext = null; var privacyContext = null;
var uri = util.newURI(doc); var uri = util.newURI(doc);
@@ -1270,42 +1258,29 @@ var Buffer = Module("Buffer", {
} }
catch (e) {} catch (e) {}
if (!isString(doc)) if (!isString(doc)) {
return io.withTempFiles(function (temp) { let file = io.createTempFile();
let encoder = services.HtmlEncoder(); let encoder = services.HtmlEncoder();
encoder.init(doc, "text/unicode", encoder.OutputRaw|encoder.OutputPreformatted); encoder.init(doc, "text/unicode", encoder.OutputRaw|encoder.OutputPreformatted);
temp.write(encoder.encodeToString(), ">");
return this.callback(temp, true);
}, this, true, ext);
OS.File.writeAtomic(file.path, encoder.encodeToString())
.then(() => { resolve([file, true]) })
.catch(reject);
}
else {
let file = util.getFile(uri); let file = util.getFile(uri);
if (file) if (file)
this.callback(file, false); resolve([file, false]);
else { else {
this.file = io.createTempFile(); let file = io.createTempFile();
var persist = services.Persist(); Downloads.fetch(uri, file.file).then(() => {
persist.persistFlags = persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES; resolve([file, true]);
persist.progressListener = this; }).catch(reject);
persist.saveURI(uri, null, null, null, null, this.file,
privacyContext);
} }
return null; }
});
}, },
onStateChange: function onStateChange(progress, request, flags, status) {
if ((flags & this.STATE_STOP) && status == 0) {
try {
var ok = this.callback(this.file, true);
}
finally {
if (ok !== true)
this.file.remove(false);
}
}
return 0;
}
}),
/** /**
* Increases the zoom level of the current buffer. * Increases the zoom level of the current buffer.
* *
@@ -1956,9 +1931,12 @@ var Buffer = Module("Buffer", {
let command = commandline.command; let command = commandline.command;
if (filename) { if (filename) {
if (filename[0] == "!") if (filename[0] == "!")
return buffer.viewSourceExternally(buffer.focusedFrame.document, return buffer.fetchSourceExternally(buffer.focusedFrame.document)
function (file) { .then(([file, isTemp]) => {
let output = io.system(filename.substr(1), file); let output = io.system(filename.substr(1), file);
if (isTemp)
file.remove(false);
commandline.command = command; commandline.command = command;
commandline.commandOutput(["span", { highlight: "CmdOutput" }, output]); commandline.commandOutput(["span", { highlight: "CmdOutput" }, output]);
}); });
@@ -1968,14 +1946,16 @@ var Buffer = Module("Buffer", {
dactyl.assert(args.bang || file.exists() && file.isWritable(), dactyl.assert(args.bang || file.exists() && file.isWritable(),
_("io.notWriteable", JSON.stringify(file.path))); _("io.notWriteable", JSON.stringify(file.path)));
return buffer.viewSourceExternally(buffer.focusedFrame.document, return buffer.fetchSourceExternally(buffer.focusedFrame.document)
function (tmpFile) { .then(([tmpFile, isTemp]) => {
try { try {
file.write(tmpFile, ">>"); file.write(tmpFile, ">>");
} }
catch (e) { catch (e) {
dactyl.echoerr(_("io.notWriteable", JSON.stringify(file.path))); dactyl.echoerr(_("io.notWriteable", JSON.stringify(file.path)));
} }
if (isTemp)
tmpFile.remove();
}); });
} }