Bug 1265885 - Add devtools module to replace promise.defer. r=jryans
authorJason Laster <jlaster@mozilla.com>
Wed, 25 May 2016 12:40:00 -0400
changeset 299045 1a8b7c916dafe6c7c86759da2fa41c8040734224
parent 299044 0dfa11a25c898eb9d7d9d7220cc3323e8211d1aa
child 299046 4f75d0cc90d12974b5b2b487bd63f17626f9e20d
push id19392
push userryanvm@gmail.com
push dateThu, 26 May 2016 03:48:22 +0000
treeherderfx-team@25566a14a7ac [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1265885
milestone49.0a1
Bug 1265885 - Add devtools module to replace promise.defer. r=jryans MozReview-Commit-ID: 1Paf8cfkh5Q
.eslintignore
devtools/shared/moz.build
devtools/shared/promise_defer.js
devtools/shared/tests/unit/test_promise_defer.js
devtools/shared/tests/unit/xpcshell.ini
--- a/.eslintignore
+++ b/.eslintignore
@@ -106,16 +106,17 @@ devtools/client/webconsole/**
 !devtools/client/webconsole/panel.js
 !devtools/client/webconsole/jsterm.js
 devtools/client/webide/**
 devtools/server/**
 !devtools/server/actors/webbrowser.js
 !devtools/server/actors/styles.js
 devtools/shared/*.js
 !devtools/shared/css-lexer.js
+!devtools/shared/promise_defer.js
 !devtools/shared/task.js
 devtools/shared/*.jsm
 devtools/shared/apps/**
 devtools/shared/client/**
 devtools/shared/discovery/**
 devtools/shared/gcli/**
 !devtools/shared/gcli/templater.js
 devtools/shared/heapsnapshot/**
--- a/devtools/shared/moz.build
+++ b/devtools/shared/moz.build
@@ -47,13 +47,14 @@ DevToolsModules(
     'deprecated-sync-thenables.js',
     'DevToolsUtils.js',
     'event-emitter.js',
     'event-parsers.js',
     'indentation.js',
     'Loader.jsm',
     'Parser.jsm',
     'path.js',
+    'promise_defer.js',
     'protocol.js',
     'system.js',
     'task.js',
     'ThreadSafeDevToolsUtils.js',
 )
new file mode 100644
--- /dev/null
+++ b/devtools/shared/promise_defer.js
@@ -0,0 +1,22 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/**
+ * Returns a deferred object, with a resolve and reject property.
+ * https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred
+ */
+module.exports = function defer() {
+  let resolve, reject;
+  let promise = new Promise(function () {
+    resolve = arguments[0];
+    reject = arguments[1];
+  });
+  return {
+    resolve: resolve,
+    reject: reject,
+    promise: promise
+  };
+};
new file mode 100644
--- /dev/null
+++ b/devtools/shared/tests/unit/test_promise_defer.js
@@ -0,0 +1,35 @@
+/* -*- 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";
+
+var Cu = Components.utils;
+var {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
+const defer = require("devtools/shared/promise_defer");
+
+function testResolve() {
+  const deferred = defer();
+  deferred.resolve("success");
+  return deferred.promise;
+}
+
+function testReject() {
+  const deferred = defer();
+  deferred.reject("error");
+  return deferred.promise;
+}
+
+add_task(function* () {
+  const success = yield testResolve();
+  equal(success, "success");
+
+  let error;
+  try {
+    yield testReject();
+  } catch (e) {
+    error = e;
+  }
+
+  equal(error, "error");
+});
--- a/devtools/shared/tests/unit/xpcshell.ini
+++ b/devtools/shared/tests/unit/xpcshell.ini
@@ -21,9 +21,10 @@ support-files =
 [test_safeErrorString.js]
 [test_defineLazyPrototypeGetter.js]
 [test_async-utils.js]
 [test_console_filtering.js]
 [test_prettifyCSS.js]
 [test_require_lazy.js]
 [test_require.js]
 [test_stack.js]
+[test_promise_defer.js]
 [test_executeSoon.js]