Bug 1440320 - Removed unused methods from async-utils module. r=jryans draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 19 Feb 2018 02:29:12 -0800
changeset 760263 849b158882a77a32de1cfd55f9d22314e8d392a6
parent 760185 580d833df9c44acec686a9fb88b5f27e9d29f68d
child 760264 830088d336ab7c9aba4026b7ce600ddb555422ec
push id100597
push userbmo:poirot.alex@gmail.com
push dateTue, 27 Feb 2018 09:38:37 +0000
reviewersjryans
bugs1440320
milestone60.0a1
Bug 1440320 - Removed unused methods from async-utils module. r=jryans MozReview-Commit-ID: 3ixJhRWeRpd
devtools/shared/async-utils.js
devtools/shared/tests/unit/test_async-utils.js
devtools/shared/tests/unit/xpcshell.ini
--- a/devtools/shared/async-utils.js
+++ b/devtools/shared/async-utils.js
@@ -11,38 +11,16 @@
  * returned value. If it throws the promise rejects with the thrown error.
  *
  * See Task documentation at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm.
  */
 
 var {Task} = require("devtools/shared/task");
 
 /**
- * Create an async function that only executes once per instance of an object.
- * Once called on a given object, the same promise will be returned for any
- * future calls for that object.
- *
- * @param Function func
- *        The generator function that to wrap as an async function.
- * @return Function
- *         The async function.
- */
-exports.asyncOnce = function asyncOnce(func) {
-  const promises = new WeakMap();
-  return function (...args) {
-    let promise = promises.get(this);
-    if (!promise) {
-      promise = Task.spawn(func.apply(this, args));
-      promises.set(this, promise);
-    }
-    return promise;
-  };
-};
-
-/**
  * Adds an event listener to the given element, and then removes its event
  * listener once the event is called, returning the event object as a promise.
  * @param  nsIDOMElement element
  *         The DOM element to listen on
  * @param  String event
  *         The name of the event type to listen for
  * @param  Boolean useCapture
  *         Should we initiate the capture phase?
@@ -54,53 +32,8 @@ exports.listenOnce = function listenOnce
   return new Promise(function (resolve, reject) {
     let onEvent = function (ev) {
       element.removeEventListener(event, onEvent, useCapture);
       resolve(ev);
     };
     element.addEventListener(event, onEvent, useCapture);
   });
 };
-
-/**
- * Call a function that expects a callback as the last argument and returns a
- * promise for the result. This simplifies using callback APIs from tasks and
- * async functions.
- *
- * @param Any obj
- *        The |this| value to call the function on.
- * @param Function func
- *        The callback-expecting function to call.
- * @param Array args
- *        Additional arguments to pass to the method.
- * @return Promise
- *         The promise for the result. If the callback is called with only one
- *         argument, it is used as the resolution value. If there's multiple
- *         arguments, an array containing the arguments is the resolution value.
- *         If the method throws, the promise is rejected with the thrown value.
- */
-function promisify(obj, func, args) {
-  return new Promise(resolve => {
-    args.push((...results) => {
-      resolve(results.length > 1 ? results : results[0]);
-    });
-    func.apply(obj, args);
-  });
-}
-
-/**
- * Call a method that expects a callback as the last argument and returns a
- * promise for the result.
- *
- * @see promisify
- */
-exports.promiseInvoke = function promiseInvoke(obj, func, ...args) {
-  return promisify(obj, func, args);
-};
-
-/**
- * Call a function that expects a callback as the last argument.
- *
- * @see promisify
- */
-exports.promiseCall = function promiseCall(func, ...args) {
-  return promisify(undefined, func, args);
-};
deleted file mode 100644
--- a/devtools/shared/tests/unit/test_async-utils.js
+++ /dev/null
@@ -1,157 +0,0 @@
-/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
-/* Any copyright is dedicated to the Public Domain.
-   http://creativecommons.org/publicdomain/zero/1.0/ */
-
-"use strict";
-
-// Test async-utils.js
-
-const {Task} = require("devtools/shared/task");
-// |const| will not work because
-// it will make the Promise object immutable before assigning.
-// Using Object.defineProperty() instead.
-Object.defineProperty(this, "Promise", {
-  value: require("promise"),
-  writable: false, configurable: false
-});
-const {asyncOnce, promiseInvoke, promiseCall} = require("devtools/shared/async-utils");
-
-function run_test() {
-  do_test_pending();
-  Task.spawn(function* () {
-    yield test_async_args(asyncOnce);
-    yield test_async_return(asyncOnce);
-    yield test_async_throw(asyncOnce);
-
-    yield test_async_once();
-    yield test_async_invoke();
-    do_test_finished();
-  }).catch(error => {
-    do_throw(error);
-  });
-}
-
-// Test that arguments are correctly passed through to the async function.
-function test_async_args(async) {
-  let obj = {
-    method: async(function* (a, b) {
-      Assert.equal(this, obj);
-      Assert.equal(a, "foo");
-      Assert.equal(b, "bar");
-    })
-  };
-
-  return obj.method("foo", "bar");
-}
-
-// Test that the return value from the async function is resolution value of
-// the promise.
-function test_async_return(async) {
-  let obj = {
-    method: async(function* (a, b) {
-      return a + b;
-    })
-  };
-
-  return obj.method("foo", "bar").then(ret => {
-    Assert.equal(ret, "foobar");
-  });
-}
-
-// Test that the throwing from an async function rejects the promise.
-function test_async_throw(async) {
-  let obj = {
-    method: async(function* () {
-      throw new Error("boom");
-    })
-  };
-
-  return obj.method().catch(error => {
-    Assert.ok(error instanceof Error);
-    Assert.equal(error.message, "boom");
-  });
-}
-
-// Test that asyncOnce only runs the async function once per instance and
-// returns the same promise for that instance.
-function test_async_once() {
-  let counter = 0;
-
-  function Foo() {}
-  Foo.prototype = {
-    ran: false,
-    method: asyncOnce(function* () {
-      yield Promise.resolve();
-      if (this.ran) {
-        do_throw("asyncOnce function unexpectedly ran twice on the same object");
-      }
-      this.ran = true;
-      return counter++;
-    })
-  };
-
-  let foo1 = new Foo();
-  let foo2 = new Foo();
-  let p1 = foo1.method();
-  let p2 = foo2.method();
-
-  Assert.notEqual(p1, p2);
-
-  let p3 = foo1.method();
-  Assert.equal(p1, p3);
-  Assert.ok(!foo1.ran);
-
-  let p4 = foo2.method();
-  Assert.equal(p2, p4);
-  Assert.ok(!foo2.ran);
-
-  return p1.then(ret => {
-    Assert.ok(foo1.ran);
-    Assert.equal(ret, 0);
-    return p2;
-  }).then(ret => {
-    Assert.ok(foo2.ran);
-    Assert.equal(ret, 1);
-  });
-}
-
-// Test invoke and call.
-function test_async_invoke() {
-  return Task.spawn(function* () {
-    function func(a, b, expectedThis, callback) {
-      Assert.equal(a, "foo");
-      Assert.equal(b, "bar");
-      Assert.equal(this, expectedThis);
-      callback(a + b);
-    }
-
-    // Test call.
-    let callResult = yield promiseCall(func, "foo", "bar", undefined);
-    Assert.equal(callResult, "foobar");
-
-    // Test invoke.
-    let obj = { method: func };
-    let invokeResult = yield promiseInvoke(obj, obj.method, "foo", "bar", obj);
-    Assert.equal(invokeResult, "foobar");
-
-    // Test passing multiple values to the callback.
-    function multipleResults(callback) {
-      callback("foo", "bar");
-    }
-
-    let results = yield promiseCall(multipleResults);
-    Assert.equal(results.length, 2);
-    Assert.equal(results[0], "foo");
-    Assert.equal(results[1], "bar");
-
-    // Test throwing from the function.
-    function thrower() {
-      throw new Error("boom");
-    }
-
-    yield promiseCall(thrower).catch(error => {
-      Assert.ok(error instanceof Error);
-      Assert.equal(error.message, "boom");
-    });
-  });
-}
--- a/devtools/shared/tests/unit/xpcshell.ini
+++ b/devtools/shared/tests/unit/xpcshell.ini
@@ -24,17 +24,16 @@ run-if = nightly_build
 [test_flatten.js]
 [test_indentation.js]
 [test_independent_loaders.js]
 [test_invisible_loader.js]
 [test_isSet.js]
 [test_old_eventemitter_basic.js]
 [test_safeErrorString.js]
 [test_defineLazyPrototypeGetter.js]
-[test_async-utils.js]
 [test_console_filtering.js]
 [test_pluralForm-english.js]
 [test_pluralForm-makeGetter.js]
 [test_prettifyCSS.js]
 [test_require_lazy.js]
 [test_require_raw.js]
 [test_require.js]
 [test_sprintfjs.js]