Bug 1011480 - Remove CommonUtils.laterTickResolvingPromise() and replace it by Async.promiseYield() and Async.jankYielder(). r=eoger
MozReview-Commit-ID: 3YSGVKgqdUq
--- a/browser/components/translation/BingTranslator.jsm
+++ b/browser/components/translation/BingTranslator.jsm
@@ -4,17 +4,17 @@
"use strict";
this.EXPORTED_SYMBOLS = [ "BingTranslator" ];
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm");
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
-ChromeUtils.import("resource://services-common/utils.js");
+ChromeUtils.import("resource://services-common/async.js");
ChromeUtils.import("resource://gre/modules/Http.jsm");
// The maximum amount of net data allowed per request on Bing's API.
const MAX_REQUEST_DATA = 5000; // Documentation says 10000 but anywhere
// close to that is refused by the service.
// The maximum number of chunks allowed to be translated in a single
// request.
@@ -62,17 +62,17 @@ this.BingTranslator.prototype = {
// Let's split the document into various requests to be sent to
// Bing's Translation API.
for (let requestCount = 0; requestCount < MAX_REQUESTS; requestCount++) {
// Generating the text for each request can be expensive, so
// let's take the opportunity of the chunkification process to
// allow for the event loop to attend other pending events
// before we continue.
- await CommonUtils.laterTickResolvingPromise();
+ await Async.promiseYield();
// Determine the data for the next request.
let request = this._generateNextTranslationRequest(currentIndex);
// Create a real request to the server, and put it on the
// pending requests list.
let bingRequest = new BingRequest(request.data,
this.sourceLanguage,
--- a/browser/components/translation/TranslationDocument.jsm
+++ b/browser/components/translation/TranslationDocument.jsm
@@ -3,17 +3,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
this.EXPORTED_SYMBOLS = [ "TranslationDocument" ];
const TEXT_NODE = Ci.nsIDOMNode.TEXT_NODE;
-ChromeUtils.import("resource://services-common/utils.js");
+ChromeUtils.import("resource://services-common/async.js");
/**
* This class represents a document that is being translated,
* and it is responsible for parsing the document,
* generating the data structures translation (the list of
* translation items and roots), and managing the original
* and translated texts on the translation items.
*
@@ -203,24 +203,20 @@ this.TranslationDocument.prototype = {
* @param target A string that is either "translation"
* or "original".
*/
_swapDocumentContent(target) {
(async () => {
// Let the event loop breath on every 100 nodes
// that are replaced.
const YIELD_INTERVAL = 100;
- let count = YIELD_INTERVAL;
-
+ let maybeYield = Async.jankYielder(YIELD_INTERVAL);
for (let root of this.roots) {
root.swapText(target);
- if (count-- == 0) {
- count = YIELD_INTERVAL;
- await CommonUtils.laterTickResolvingPromise();
- }
+ await maybeYield();
}
})();
}
};
/**
* This class represents an item for translation. It's basically our
* wrapper class around a node returned by getTranslationNode, with
--- a/browser/components/translation/YandexTranslator.jsm
+++ b/browser/components/translation/YandexTranslator.jsm
@@ -4,17 +4,17 @@
"use strict";
this.EXPORTED_SYMBOLS = [ "YandexTranslator" ];
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm");
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
-ChromeUtils.import("resource://services-common/utils.js");
+ChromeUtils.import("resource://services-common/async.js");
ChromeUtils.import("resource://gre/modules/Http.jsm");
// The maximum amount of net data allowed per request on Bing's API.
const MAX_REQUEST_DATA = 5000; // Documentation says 10000 but anywhere
// close to that is refused by the service.
// The maximum number of chunks allowed to be translated in a single
// request.
@@ -80,17 +80,17 @@ this.YandexTranslator.prototype = {
// Let's split the document into various requests to be sent to
// Yandex's Translation API.
for (let requestCount = 0; requestCount < MAX_REQUESTS; requestCount++) {
// Generating the text for each request can be expensive, so
// let's take the opportunity of the chunkification process to
// allow for the event loop to attend other pending events
// before we continue.
- await CommonUtils.laterTickResolvingPromise();
+ await Async.promiseYield();
// Determine the data for the next request.
let request = this._generateNextTranslationRequest(currentIndex);
// Create a real request to the server, and put it on the
// pending requests list.
let yandexRequest = new YandexRequest(request.data,
this.sourceLanguage,
--- a/services/common/utils.js
+++ b/services/common/utils.js
@@ -129,29 +129,16 @@ this.CommonUtils = {
nextTick: function nextTick(callback, thisObj) {
if (thisObj) {
callback = callback.bind(thisObj);
}
Services.tm.dispatchToMainThread(callback);
},
/**
- * Return a promise resolving on some later tick.
- *
- * This a wrapper around Promise.resolve() that prevents stack
- * accumulation and prevents callers from accidentally relying on
- * same-tick promise resolution.
- */
- laterTickResolvingPromise(value) {
- return new Promise(resolve => {
- this.nextTick(() => resolve(value));
- });
- },
-
- /**
* Return a timer that is scheduled to call the callback after waiting the
* provided time or as soon as possible. The timer will be set as a property
* of the provided object with the given timer name.
*/
namedTimer: function namedTimer(callback, wait, thisObj, name) {
if (!thisObj || !name) {
throw new Error(
"You must provide both an object and a property name for the timer!");