1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-01-09 07:44:14 +01:00

Promises cleanup.

This commit is contained in:
Kris Maglione
2015-03-02 18:12:57 -08:00
parent c84c657d27
commit 7b2f821e04
10 changed files with 160 additions and 126 deletions

View File

@@ -1,6 +1,6 @@
// Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
// Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
// Copyright (c) 2008-2014 Kris Maglione <maglione.k@gmail.com>
// Copyright (c) 2008-2015 Kris Maglione <maglione.k@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
@@ -302,23 +302,22 @@ var Bookmarks = Module("bookmarks", {
* @returns {Promise<Array>}
*/
makeSuggestions: function makeSuggestions(url, parser) {
let deferred = Promise.defer();
return new CancelablePromise((resolve, reject, canceled) => {
let req = util.fetchUrl(url);
req.then(function process(req) {
let results = [];
try {
results = parser(req);
}
catch (e) {
reject(e);
return;
}
resolve(results);
});
let req = util.fetchUrl(url);
req.then(function process(req) {
let results = [];
try {
results = parser(req);
}
catch (e) {
deferred.reject(e);
return;
}
deferred.resolve(results);
canceled.then(req.cancel);
});
promises.oncancel(deferred, reason => promises.cancel(req, reason));
return deferred.promise;
},
suggestionProviders: {},

View File

@@ -855,13 +855,15 @@ var CommandLine = Module("commandline", {
* @... {string} default - The initial value that will be returned
* if the user presses <CR> straightaway. @default ""
*/
input: promises.withCallbacks(function _input([callback, reject], prompt, extra={}, thing={}) {
if (callable(extra))
// Deprecated.
[callback, extra] = [extra, thing];
input: function _input(prompt, extra={}, thing={}) {
return new Promise((resolve, reject) => {
if (callable(extra))
// Deprecated.
[resolve, extra] = [extra, thing];
CommandPromptMode(prompt, update({ onSubmit: callback, onCancel: reject }, extra)).open();
}),
CommandPromptMode(prompt, update({ onSubmit: resolve, onCancel: reject }, extra)).open();
});
},
readHeredoc: function readHeredoc(end) {
return util.waitFor(commandline.inputMultiline(end));
@@ -876,33 +878,35 @@ var CommandLine = Module("commandline", {
* @returns {Promise<string>}
*/
// FIXME: Buggy, especially when pasting.
inputMultiline: promises.withCallbacks(function inputMultiline([callback], end) {
let cmd = this.command;
let self = {
end: "\n" + end + "\n",
callback: callback
};
inputMultiline: function inputMultiline(end) {
return new Promise((resolve, reject) => {
let cmd = this.command;
let self = {
end: "\n" + end + "\n",
callback: resolve
};
modes.push(modes.INPUT_MULTILINE, null, {
holdFocus: true,
leave: function leave() {
if (!self.done)
self.callback(null);
},
mappingSelf: self
modes.push(modes.INPUT_MULTILINE, null, {
holdFocus: true,
leave: function leave() {
if (!self.done)
self.callback(null);
},
mappingSelf: self
});
if (cmd != false)
this._echoLine(cmd, this.HL_NORMAL);
// save the arguments, they are needed in the event handler onKeyPress
this.multilineInputVisible = true;
this.widgets.multilineInput.value = "";
this._autosizeMultilineInputWidget();
this.timeout(function () { dactyl.focus(this.widgets.multilineInput); }, 10);
});
if (cmd != false)
this._echoLine(cmd, this.HL_NORMAL);
// save the arguments, they are needed in the event handler onKeyPress
this.multilineInputVisible = true;
this.widgets.multilineInput.value = "";
this._autosizeMultilineInputWidget();
this.timeout(function () { dactyl.focus(this.widgets.multilineInput); }, 10);
}),
},
get commandMode() this.commandSession && isinstance(modes.main, modes.COMMAND_LINE),