1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 22:38:00 +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

1
common/bootstrap.js vendored
View File

@@ -264,6 +264,7 @@ function init() {
bootstrap = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(), bootstrap = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(),
{ sandboxName: BOOTSTRAP, { sandboxName: BOOTSTRAP,
addonId: addon.id, addonId: addon.id,
wantGlobalProperties: ["TextDecoder", "TextEncoder"],
metadata: { addonID: addon.id } }); metadata: { addonID: addon.id } });
Services.scriptloader.loadSubScript(BOOTSTRAP, bootstrap); Services.scriptloader.loadSubScript(BOOTSTRAP, bootstrap);
} }

View File

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

View File

@@ -855,13 +855,15 @@ var CommandLine = Module("commandline", {
* @... {string} default - The initial value that will be returned * @... {string} default - The initial value that will be returned
* if the user presses <CR> straightaway. @default "" * if the user presses <CR> straightaway. @default ""
*/ */
input: promises.withCallbacks(function _input([callback, reject], prompt, extra={}, thing={}) { input: function _input(prompt, extra={}, thing={}) {
return new Promise((resolve, reject) => {
if (callable(extra)) if (callable(extra))
// Deprecated. // Deprecated.
[callback, extra] = [extra, thing]; [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) { readHeredoc: function readHeredoc(end) {
return util.waitFor(commandline.inputMultiline(end)); return util.waitFor(commandline.inputMultiline(end));
@@ -876,11 +878,12 @@ var CommandLine = Module("commandline", {
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
// FIXME: Buggy, especially when pasting. // FIXME: Buggy, especially when pasting.
inputMultiline: promises.withCallbacks(function inputMultiline([callback], end) { inputMultiline: function inputMultiline(end) {
return new Promise((resolve, reject) => {
let cmd = this.command; let cmd = this.command;
let self = { let self = {
end: "\n" + end + "\n", end: "\n" + end + "\n",
callback: callback callback: resolve
}; };
modes.push(modes.INPUT_MULTILINE, null, { modes.push(modes.INPUT_MULTILINE, null, {
@@ -902,7 +905,8 @@ var CommandLine = Module("commandline", {
this._autosizeMultilineInputWidget(); this._autosizeMultilineInputWidget();
this.timeout(function () { dactyl.focus(this.widgets.multilineInput); }, 10); this.timeout(function () { dactyl.focus(this.widgets.multilineInput); }, 10);
}), });
},
get commandMode() this.commandSession && isinstance(modes.main, modes.COMMAND_LINE), get commandMode() this.commandSession && isinstance(modes.main, modes.COMMAND_LINE),

View File

@@ -182,7 +182,6 @@ defineModule("base", {
"Cr", "Cr",
"Cs", "Cs",
"Cu", "Cu",
"DOMPromise",
"ErrorBase", "ErrorBase",
"Finished", "Finished",
"JSMLoader", "JSMLoader",
@@ -762,12 +761,6 @@ function memoize(obj, key, getter) {
} }
} }
let sandbox = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(),
{ wantGlobalProperties: ["TextDecoder", "TextEncoder"],
sandboxPrototype: this });
var { TextEncoder, TextDecoder, Promise: DOMPromise } = sandbox;
/** /**
* Updates an object with the properties of another object. Getters * Updates an object with the properties of another object. Getters
* and setters are copied as expected. Moreover, any function * and setters are copied as expected. Moreover, any function

View File

@@ -15,6 +15,9 @@ this["import"] = function import_(obj) {
return res; return res;
} }
if (typeof TextEncoder == "undefined")
Components.utils.importGlobalProperties(["TextEncoder", "TextDecoder"]);
// Deal with subScriptLoader prepending crap to loaded URLs // Deal with subScriptLoader prepending crap to loaded URLs
Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/Services.jsm");
function loadSubScript() Services.scriptloader.loadSubScript.apply(null, arguments); function loadSubScript() Services.scriptloader.loadSubScript.apply(null, arguments);

View File

@@ -86,7 +86,8 @@ var Buffer = Module("Buffer", {
* @param {string} pref The name of the preference to return. * @param {string} pref The name of the preference to return.
* @returns {Promise<*>} * @returns {Promise<*>}
*/ */
get: promises.withCallbacks(function get([resolve, reject], pref) { get: function get(pref) {
return new Promise((resolve, reject) => {
let val = services.contentPrefs.getCachedByDomainAndName( let val = services.contentPrefs.getCachedByDomainAndName(
self.uri.spec, pref, self.loadContext); self.uri.spec, pref, self.loadContext);
@@ -105,7 +106,8 @@ var Buffer = Module("Buffer", {
resolve(pref.value); resolve(pref.value);
}, },
handleError: reject }); handleError: reject });
}), });
},
/** /**
* Sets a content preference for the given buffer. * Sets a content preference for the given buffer.
@@ -113,26 +115,30 @@ var Buffer = Module("Buffer", {
* @param {string} pref The preference to set. * @param {string} pref The preference to set.
* @param {string} value The value to store. * @param {string} value The value to store.
*/ */
set: promises.withCallbacks(function set([resolve, reject], pref, value) { set: function set(pref, value) {
return new Promise((resolve, reject) => {
services.contentPrefs.set( services.contentPrefs.set(
self.uri.spec, pref, value, self.loadContext, self.uri.spec, pref, value, self.loadContext,
{ handleCompletion: () => {}, { handleCompletion: () => {},
handleResult: resolve, handleResult: resolve,
handleError: reject }); handleError: reject });
}), });
},
/** /**
* Clear a content preference for the given buffer. * Clear a content preference for the given buffer.
* *
* @param {string} pref The preference to clear. * @param {string} pref The preference to clear.
*/ */
clear: promises.withCallbacks(function clear([resolve, reject], pref) { clear: function clear(pref) {
return new Promise((resolve, reject) => {
services.contentPrefs.removeByDomainAndName( services.contentPrefs.removeByDomainAndName(
self.uri.spec, pref, self.loadContext, self.uri.spec, pref, self.loadContext,
{ handleCompletion: () => {}, { handleCompletion: () => {},
handleResult: resolve, handleResult: resolve,
handleError: reject }); handleError: reject });
}) });
}
}; };
}), }),

View File

@@ -140,7 +140,7 @@ var ConfigBase = Class("ConfigBase", {
"options", "options",
"overlay", "overlay",
"prefs", "prefs",
["promises", "Promise", "Task", "promises"], ["promises", "CancelablePromise", "Promise", "Task", "promises"],
"protocol", "protocol",
"sanitizer", "sanitizer",
"services", "services",

View File

@@ -95,7 +95,7 @@ var Modules = function Modules(window) {
if (normal) if (normal)
return create(proto); return create(proto);
sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules, let sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules,
sandboxName: name || ("Dactyl Sandbox " + ++_id), sandboxName: name || ("Dactyl Sandbox " + ++_id),
wantXrays: true }); wantXrays: true });

View File

@@ -5,7 +5,7 @@
"use strict"; "use strict";
defineModule("promises", { defineModule("promises", {
exports: ["Promise", "Task", "promises"], exports: ["CancelablePromise", "Promise", "Task", "promises"],
require: [] require: []
}); });
@@ -24,6 +24,26 @@ function withCallbacks(fn) {
} }
} }
function CancelablePromise(executor, oncancel) {
let deferred = Promise.defer();
let canceled = new Promise((accept, reject) => {
promises.oncancel(deferred, accept);
});
try {
executor(deferred.resolve, deferred.reject, canceled);
}
catch (e) {
deferred.reject(e);
}
return Object.freeze(Object.create(deferred.promise, {
cancel: {
value: thing => promises.cancel(deferred.promise, thing)
}
}));
}
var Promises = Module("Promises", { var Promises = Module("Promises", {
_cancel: new WeakMap, _cancel: new WeakMap,
@@ -38,7 +58,7 @@ var Promises = Module("Promises", {
let cleanup = this._cancel.get(promise); let cleanup = this._cancel.get(promise);
if (cleanup) { if (cleanup) {
cleanup[0](promise); cleanup[0](promise);
cleanup[1].reject(reason); cleanup[1](reason);
} }
this._cancel.delete(promise); this._cancel.delete(promise);
}, },
@@ -50,16 +70,18 @@ var Promises = Module("Promises", {
* @param {function} fn The cleanup function. * @param {function} fn The cleanup function.
*/ */
oncancel: function oncancel(deferred, fn) { oncancel: function oncancel(deferred, fn) {
this._cancel.set(deferred.promise, [fn, deferred]); this._cancel.set(deferred.promise, [fn, deferred.reject]);
}, },
/** /**
* Returns a promise which resolves after a brief delay. * Returns a promise which resolves after a brief delay.
*/ */
delay: withCallbacks(function delay([accept]) { delay: function delay([accept]) {
return new Promise(resolve => {
let { mainThread } = services.threading; let { mainThread } = services.threading;
mainThread.dispatch(accept, mainThread.DISPATCH_NORMAL); mainThread.dispatch(resolve, mainThread.DISPATCH_NORMAL);
}), });
},
/** /**
* Returns a promise which resolves after the given number of * Returns a promise which resolves after the given number of
@@ -67,9 +89,11 @@ var Promises = Module("Promises", {
* *
* @param {number} delay The number of milliseconds to wait. * @param {number} delay The number of milliseconds to wait.
*/ */
sleep: withCallbacks(function sleep([callback], delay) { sleep: function sleep(delay) {
this.timeout(callback, delay); return new Promise(resolve => {
}), this.timeout(resolve, delay);
});
},
/** /**
* Wraps the given function so that each call spawns a Task. * Wraps the given function so that each call spawns a Task.
@@ -94,7 +118,8 @@ var Promises = Module("Promises", {
* @param {number} pollInterval The poll interval, in milliseconds. * @param {number} pollInterval The poll interval, in milliseconds.
* @default 10 * @default 10
*/ */
waitFor: withCallbacks(function waitFor([accept, reject], test, timeout=null, pollInterval=10) { waitFor: function waitFor(test, timeout=null, pollInterval=10) {
return new Promise((resolve, reject) => {
let end = timeout && Date.now() + timeout, result; let end = timeout && Date.now() + timeout, result;
let timer = services.Timer( let timer = services.Timer(
@@ -108,11 +133,12 @@ var Promises = Module("Promises", {
} }
if (result) { if (result) {
timer.cancel(); timer.cancel();
accept(result); resolve(result);
} }
}, },
pollInterval, services.Timer.TYPE_REPEATING_SLACK); pollInterval, services.Timer.TYPE_REPEATING_SLACK);
}), });
},
/** /**
* Wraps the given function so that its first argument is an array * Wraps the given function so that its first argument is an array

View File

@@ -828,14 +828,16 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @param {string} url The URL to fetch. * @param {string} url The URL to fetch.
* @param {object} params Parameter object, as in #httpGet. * @param {object} params Parameter object, as in #httpGet.
*/ */
fetchUrl: promises.withCallbacks(function fetchUrl([accept, reject, deferred], url, params) { fetchUrl: function fetchUrl(url, params) {
return new CancelablePromise((accept, reject, canceled) => {
params = update({}, params); params = update({}, params);
params.onload = accept; params.onload = accept;
params.onerror = reject; params.onerror = reject;
let req = this.httpGet(url, params); let req = this.httpGet(url, params);
promises.oncancel(deferred, req.cancel); canceled.then(req.cancel);
}), });
},
/** /**
* The identity function. * The identity function.