Bug 1355696 - convert uses of 'defer' to 'new Promise' in client/sourceeditor; r=tromey
authorMatt R <matthieu.rigolot@gmail.com>
Sat, 20 May 2017 19:14:59 +0100
changeset 360088 2fde169875b3358a319695cb3007d452b9d5f920
parent 360087 0aebf5a631ce11bee2e9c923638f6de4395b42cb
child 360089 1bf641716cae6e15961405bbda213319ebf69875
push id43224
push userttromey@mozilla.com
push dateTue, 23 May 2017 11:37:52 +0000
treeherderautoland@2fde169875b3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstromey
bugs1355696
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1355696 - convert uses of 'defer' to 'new Promise' in client/sourceeditor; r=tromey MozReview-Commit-ID: 5b1ObMvNA2e
devtools/client/sourceeditor/debugger.js
devtools/client/sourceeditor/editor.js
devtools/client/sourceeditor/test/browser_editor_autocomplete_js.js
devtools/client/sourceeditor/test/head.js
--- a/devtools/client/sourceeditor/debugger.js
+++ b/devtools/client/sourceeditor/debugger.js
@@ -1,16 +1,15 @@
 /* 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";
 
 const DevToolsUtils = require("devtools/shared/DevToolsUtils");
-const promise = require("promise");
 const dbginfo = new WeakMap();
 
 // These functions implement search within the debugger. Since
 // search in the debugger is different from other components,
 // we can't use search.js CodeMirror addon. This is a slightly
 // modified version of that addon. Depends on searchcursor.js.
 
 function SearchState() {
@@ -125,57 +124,57 @@ function hasBreakpoint(ctx, line) {
 /**
  * Adds a visual breakpoint for a specified line. Third
  * parameter 'cond' can hold any object.
  *
  * After adding a breakpoint, this function makes Editor to
  * emit a breakpointAdded event.
  */
 function addBreakpoint(ctx, line, cond) {
-  function _addBreakpoint() {
-    let { ed, cm } = ctx;
-    let meta = dbginfo.get(ed);
-    let info = cm.lineInfo(line);
-
-    // The line does not exist in the editor. This is harmless, the
-    // architecture calling this assumes the editor will handle this
-    // gracefully, and make sure breakpoints exist when they need to.
-    if (!info) {
-      return;
-    }
-
-    ed.addLineClass(line, "breakpoint");
-    meta.breakpoints[line] = { condition: cond };
-
-    // TODO(jwl): why is `info` null when breaking on page reload?
-    info.handle.on("delete", function onDelete() {
-      info.handle.off("delete", onDelete);
-      meta.breakpoints[info.line] = null;
-    });
-
-    if (cond) {
-      setBreakpointCondition(ctx, line);
-    }
-    ed.emit("breakpointAdded", line);
-    deferred.resolve();
-  }
-
   if (hasBreakpoint(ctx, line)) {
     return null;
   }
 
-  let deferred = promise.defer();
-  // If lineInfo() returns null, wait a tick to give the editor a chance to
-  // initialize properly.
-  if (ctx.cm.lineInfo(line) === null) {
-    DevToolsUtils.executeSoon(() => _addBreakpoint());
-  } else {
-    _addBreakpoint();
-  }
-  return deferred.promise;
+  return new Promise(resolve => {
+    function _addBreakpoint() {
+      let { ed, cm } = ctx;
+      let meta = dbginfo.get(ed);
+      let info = cm.lineInfo(line);
+
+      // The line does not exist in the editor. This is harmless, the
+      // architecture calling this assumes the editor will handle this
+      // gracefully, and make sure breakpoints exist when they need to.
+      if (!info) {
+        return;
+      }
+
+      ed.addLineClass(line, "breakpoint");
+      meta.breakpoints[line] = { condition: cond };
+
+      // TODO(jwl): why is `info` null when breaking on page reload?
+      info.handle.on("delete", function onDelete() {
+        info.handle.off("delete", onDelete);
+        meta.breakpoints[info.line] = null;
+      });
+
+      if (cond) {
+        setBreakpointCondition(ctx, line);
+      }
+      ed.emit("breakpointAdded", line);
+      resolve();
+    }
+
+    // If lineInfo() returns null, wait a tick to give the editor a chance to
+    // initialize properly.
+    if (ctx.cm.lineInfo(line) === null) {
+      DevToolsUtils.executeSoon(() => _addBreakpoint());
+    } else {
+      _addBreakpoint();
+    }
+  });
 }
 
 /**
  * Helps reset the debugger's breakpoint state
  * - removes the breakpoints in the editor
  * - cleares the debugger's breakpoint state
  *
  * Note, does not *actually* remove a source's breakpoints.
--- a/devtools/client/sourceeditor/editor.js
+++ b/devtools/client/sourceeditor/editor.js
@@ -25,17 +25,16 @@ const VALID_KEYMAPS = new Set(["emacs", 
 const MAX_VERTICAL_OFFSET = 3;
 
 // Match @Scratchpad/N:LINE[:COLUMN] or (LINE[:COLUMN]) anywhere at an end of
 // line in text selection.
 const RE_SCRATCHPAD_ERROR = /(?:@Scratchpad\/\d+:|\()(\d+):?(\d+)?(?:\)|\n)/;
 const RE_JUMP_TO_LINE = /^(\d+):?(\d+)?/;
 
 const Services = require("Services");
-const promise = require("promise");
 const events = require("devtools/shared/event-emitter");
 const { PrefObserver } = require("devtools/client/shared/prefs");
 const { getClientCssProperties } = require("devtools/shared/fronts/css-properties");
 const KeyShortcuts = require("devtools/client/shared/key-shortcuts");
 
 const {LocalizationHelper} = require("devtools/shared/l10n");
 const L10N = new LocalizationHelper("devtools/client/locales/sourceeditor.properties");
 
@@ -252,55 +251,55 @@ Editor.prototype = {
    * Appends the current Editor instance to the element specified by
    * 'el'. You can also provide your won iframe to host the editor as
    * an optional second parameter. This method actually creates and
    * loads CodeMirror and all its dependencies.
    *
    * This method is asynchronous and returns a promise.
    */
   appendTo: function (el, env) {
-    let def = promise.defer();
-    let cm = editors.get(this);
+    return new Promise(resolve => {
+      let cm = editors.get(this);
 
-    if (!env) {
-      env = el.ownerDocument.createElementNS(el.namespaceURI, "iframe");
+      if (!env) {
+        env = el.ownerDocument.createElementNS(el.namespaceURI, "iframe");
 
-      if (el.namespaceURI === XUL_NS) {
-        env.flex = 1;
+        if (el.namespaceURI === XUL_NS) {
+          env.flex = 1;
+        }
       }
-    }
 
-    if (cm) {
-      throw new Error("You can append an editor only once.");
-    }
-
-    let onLoad = () => {
-      let win = env.contentWindow.wrappedJSObject;
-
-      if (!this.config.themeSwitching) {
-        win.document.documentElement.setAttribute("force-theme", "light");
+      if (cm) {
+        throw new Error("You can append an editor only once.");
       }
 
-      Services.scriptloader.loadSubScript(
-        "chrome://devtools/content/shared/theme-switching.js",
-        win, "utf8"
-      );
-      this.container = env;
-      this._setup(win.document.body, el.ownerDocument);
-      env.removeEventListener("load", onLoad, true);
+      let onLoad = () => {
+        let win = env.contentWindow.wrappedJSObject;
+
+        if (!this.config.themeSwitching) {
+          win.document.documentElement.setAttribute("force-theme", "light");
+        }
 
-      def.resolve();
-    };
+        Services.scriptloader.loadSubScript(
+          "chrome://devtools/content/shared/theme-switching.js",
+          win, "utf8"
+        );
+        this.container = env;
+        this._setup(win.document.body, el.ownerDocument);
+        env.removeEventListener("load", onLoad, true);
 
-    env.addEventListener("load", onLoad, true);
-    env.setAttribute("src", CM_IFRAME);
-    el.appendChild(env);
+        resolve();
+      };
 
-    this.once("destroy", () => el.removeChild(env));
-    return def.promise;
+      env.addEventListener("load", onLoad, true);
+      env.setAttribute("src", CM_IFRAME);
+      el.appendChild(env);
+
+      this.once("destroy", () => el.removeChild(env));
+    });
   },
 
   appendToLocalElement: function (el) {
     this._setup(el);
   },
 
   /**
    * Do the actual appending and configuring of the CodeMirror instance. This is
--- a/devtools/client/sourceeditor/test/browser_editor_autocomplete_js.js
+++ b/devtools/client/sourceeditor/test/browser_editor_autocomplete_js.js
@@ -24,22 +24,20 @@ function testJS(ed, win) {
 
   ok(ed.getOption("autocomplete"), "Autocompletion is set");
   ok(win.tern, "Tern is defined on the window");
 
   ed.focus();
   ed.setText("document.");
   ed.setCursor({line: 0, ch: 9});
 
-  let waitForSuggestion = promise.defer();
+  return new Promise(resolve => {
+    ed.on("before-suggest", () => {
+      info("before-suggest has been triggered");
+      EventUtils.synthesizeKey("VK_ESCAPE", { }, win);
+      resolve();
+    });
 
-  ed.on("before-suggest", () => {
-    info("before-suggest has been triggered");
-    EventUtils.synthesizeKey("VK_ESCAPE", { }, win);
-    waitForSuggestion.resolve();
+    let autocompleteKey =
+      Editor.keyFor("autocompletion", { noaccel: true }).toUpperCase();
+    EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
   });
-
-  let autocompleteKey =
-    Editor.keyFor("autocompletion", { noaccel: true }).toUpperCase();
-  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);
-
-  return waitForSuggestion.promise;
 }
--- a/devtools/client/sourceeditor/test/head.js
+++ b/devtools/client/sourceeditor/test/head.js
@@ -23,56 +23,55 @@ SimpleTest.registerCleanupFunction(() =>
 
 function promiseWaitForFocus() {
   return new Promise(resolve =>
     waitForFocus(resolve));
 }
 
 function setup(cb, additionalOpts = {}) {
   cb = cb || function () {};
-  let def = promise.defer();
-  const opt = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
-  const url = "data:application/vnd.mozilla.xul+xml;charset=UTF-8," +
-    "<?xml version='1.0'?>" +
-    "<?xml-stylesheet href='chrome://global/skin/global.css'?>" +
-    "<window xmlns='http://www.mozilla.org/keymaster/gatekeeper" +
-    "/there.is.only.xul' title='Editor' width='600' height='500'>" +
-    "<box flex='1'/></window>";
+  return new Promise(resolve => {
+    const opt = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
+    const url = "data:application/vnd.mozilla.xul+xml;charset=UTF-8," +
+      "<?xml version='1.0'?>" +
+      "<?xml-stylesheet href='chrome://global/skin/global.css'?>" +
+      "<window xmlns='http://www.mozilla.org/keymaster/gatekeeper" +
+      "/there.is.only.xul' title='Editor' width='600' height='500'>" +
+      "<box flex='1'/></window>";
 
-  let win = Services.ww.openWindow(null, url, "_blank", opt, null);
-  let opts = {
-    value: "Hello.",
-    lineNumbers: true,
-    foldGutter: true,
-    gutters: ["CodeMirror-linenumbers", "breakpoints", "CodeMirror-foldgutter"],
-    cssProperties: getClientCssProperties()
-  };
+    let win = Services.ww.openWindow(null, url, "_blank", opt, null);
+    let opts = {
+      value: "Hello.",
+      lineNumbers: true,
+      foldGutter: true,
+      gutters: ["CodeMirror-linenumbers", "breakpoints", "CodeMirror-foldgutter"],
+      cssProperties: getClientCssProperties()
+    };
 
-  for (let o in additionalOpts) {
-    opts[o] = additionalOpts[o];
-  }
+    for (let o in additionalOpts) {
+      opts[o] = additionalOpts[o];
+    }
 
-  win.addEventListener("load", function () {
-    waitForFocus(function () {
-      let box = win.document.querySelector("box");
-      let editor = new Editor(opts);
+    win.addEventListener("load", function () {
+      waitForFocus(function () {
+        let box = win.document.querySelector("box");
+        let editor = new Editor(opts);
 
-      editor.appendTo(box)
-        .then(() => {
-          def.resolve({
-            ed: editor,
-            win: win,
-            edWin: editor.container.contentWindow.wrappedJSObject
-          });
-          cb(editor, win);
-        }, err => ok(false, err.message));
-    }, win);
-  }, {once: true});
-
-  return def.promise;
+        editor.appendTo(box)
+          .then(() => {
+            resolve({
+              ed: editor,
+              win: win,
+              edWin: editor.container.contentWindow.wrappedJSObject
+            });
+            cb(editor, win);
+          }, err => ok(false, err.message));
+      }, win);
+    }, {once: true});
+  });
 }
 
 function ch(exp, act, label) {
   is(exp.line, act.line, label + " (line)");
   is(exp.ch, act.ch, label + " (ch)");
 }
 
 function teardown(ed, win) {