Bug 1355696 - convert uses of 'defer' to 'new Promise' in client/sourceeditor; r?tromey draft
authorMatt R <matthieu.rigolot@gmail.com>
Sat, 20 May 2017 19:14:59 +0100
changeset 582050 a0174006f57fd4bc5d2d5b416871ba81c3b9cb51
parent 582001 5b74bbf20e803e299790d266fc6ebf5d53b7a1b7
child 629652 e6ee1a14ba49fa90a1503cd0a1e9452dcff5d521
push id59953
push userbmo:matthieu.rigolot@gmail.com
push dateSat, 20 May 2017 21:26:20 +0000
reviewerstromey
bugs1355696
milestone55.0a1
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) {