toolkit/devtools/async-utils.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 /**
michael@0 8 * Helpers for async functions. Async functions are generator functions that are
michael@0 9 * run by Tasks. An async function returns a Promise for the resolution of the
michael@0 10 * function. When the function returns, the promise is resolved with the
michael@0 11 * returned value. If it throws the promise rejects with the thrown error.
michael@0 12 *
michael@0 13 * See Task documentation at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm.
michael@0 14 */
michael@0 15
michael@0 16 let {Cu} = require("chrome");
michael@0 17 let {Task} = require("resource://gre/modules/Task.jsm");
michael@0 18 let {Promise} = require("resource://gre/modules/Promise.jsm");
michael@0 19
michael@0 20 /**
michael@0 21 * Create an async function from a generator function.
michael@0 22 *
michael@0 23 * @param Function func
michael@0 24 * The generator function that to wrap as an async function.
michael@0 25 * @return Function
michael@0 26 * The async function.
michael@0 27 */
michael@0 28 exports.async = function async(func) {
michael@0 29 return function(...args) {
michael@0 30 return Task.spawn(func.apply(this, args));
michael@0 31 };
michael@0 32 };
michael@0 33
michael@0 34 /**
michael@0 35 * Create an async function that only executes once per instance of an object.
michael@0 36 * Once called on a given object, the same promise will be returned for any
michael@0 37 * future calls for that object.
michael@0 38 *
michael@0 39 * @param Function func
michael@0 40 * The generator function that to wrap as an async function.
michael@0 41 * @return Function
michael@0 42 * The async function.
michael@0 43 */
michael@0 44 exports.asyncOnce = function asyncOnce(func) {
michael@0 45 const promises = new WeakMap();
michael@0 46 return function(...args) {
michael@0 47 let promise = promises.get(this);
michael@0 48 if (!promise) {
michael@0 49 promise = Task.spawn(func.apply(this, args));
michael@0 50 promises.set(this, promise);
michael@0 51 }
michael@0 52 return promise;
michael@0 53 };
michael@0 54 };
michael@0 55
michael@0 56
michael@0 57 /**
michael@0 58 * Call a function that expects a callback as the last argument and returns a
michael@0 59 * promise for the result. This simplifies using callback APIs from tasks and
michael@0 60 * async functions.
michael@0 61 *
michael@0 62 * @param Any obj
michael@0 63 * The |this| value to call the function on.
michael@0 64 * @param Function func
michael@0 65 * The callback-expecting function to call.
michael@0 66 * @param Array args
michael@0 67 * Additional arguments to pass to the method.
michael@0 68 * @return Promise
michael@0 69 * The promise for the result. If the callback is called with only one
michael@0 70 * argument, it is used as the resolution value. If there's multiple
michael@0 71 * arguments, an array containing the arguments is the resolution value.
michael@0 72 * If the method throws, the promise is rejected with the thrown value.
michael@0 73 */
michael@0 74 function promisify(obj, func, args) {
michael@0 75 return new Promise(resolve => {
michael@0 76 args.push((...results) => {
michael@0 77 resolve(results.length > 1 ? results : results[0]);
michael@0 78 });
michael@0 79 func.apply(obj, args);
michael@0 80 });
michael@0 81 }
michael@0 82
michael@0 83 /**
michael@0 84 * Call a method that expects a callback as the last argument and returns a
michael@0 85 * promise for the result.
michael@0 86 *
michael@0 87 * @see promisify
michael@0 88 */
michael@0 89 exports.promiseInvoke = function promiseInvoke(obj, func, ...args) {
michael@0 90 return promisify(obj, func, args);
michael@0 91 };
michael@0 92
michael@0 93 /**
michael@0 94 * Call a function that expects a callback as the last argument.
michael@0 95 *
michael@0 96 * @see promisify
michael@0 97 */
michael@0 98 exports.promiseCall = function promiseCall(func, ...args) {
michael@0 99 return promisify(undefined, func, args);
michael@0 100 };

mercurial