Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1279)

Unified Diff: Source/devtools/front_end/sdk/CSSParser.js

Issue 685203003: DevTools: Get rid of synchronous XHRs in the frontend code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Address comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/devtools/front_end/sdk/CSSParser.js
diff --git a/Source/devtools/front_end/sdk/CSSParser.js b/Source/devtools/front_end/sdk/CSSParser.js
index 305dd957f71bb54bb843abaf7539aaf733a1c1b4..2d923361225ad84c1c80d067a55524f57a15d823 100644
--- a/Source/devtools/front_end/sdk/CSSParser.js
+++ b/Source/devtools/front_end/sdk/CSSParser.js
@@ -10,36 +10,61 @@
*/
WebInspector.CSSParser = function()
{
- this._worker = Runtime.startWorker("script_formatter_worker");
- this._worker.onmessage = this._onRuleChunk.bind(this);
this._rules = [];
-}
-
-WebInspector.CSSParser.Events = {
- RulesParsed: "RulesParsed"
+ this._pendingWorkerPromise = WorkerRuntime.startWorker("script_formatter_worker").then(this._init.bind(this));
}
WebInspector.CSSParser.prototype = {
/**
+ * @param {!Worker} worker
+ */
+ _init: function(worker)
+ {
+ this._worker = worker;
+ if (this._disposed) {
+ this.dispose();
+ return;
+ }
+ this._worker.onmessage = this._onRuleChunk.bind(this);
+ return this._worker;
+ },
+
+ /**
+ * @param {!Promise.<string>} contentPromise
+ */
+ _innerParseContentPromise: function(contentPromise)
+ {
+ return Promise.all([this._pendingWorkerPromise, contentPromise]).then(process.bind(this));
+
+ /**
+ * @param {!Array.<?>} results
+ * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
+ * @this {WebInspector.CSSParser}
+ */
+ function process(results)
+ {
+ return this._innerParse(/** @type {string} */ (results[1]));
+ }
+ },
+
+ /**
* @param {!WebInspector.CSSStyleSheetHeader} styleSheetHeader
- * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback
+ * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
*/
- fetchAndParse: function(styleSheetHeader, callback)
+ fetchAndParse: function(styleSheetHeader)
{
this._lock();
- this._finishedCallback = callback;
- styleSheetHeader.requestContent(this._innerParse.bind(this));
+ return this._innerParseContentPromise(styleSheetHeader.requestContentPromise());
pfeldman 2014/11/10 10:23:25 return styleSheetHeader.requestContent().then(this
},
/**
* @param {string} text
- * @param {function(!Array.<!WebInspector.CSSParser.Rule>)=} callback
+ * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
*/
- parse: function(text, callback)
+ parse: function(text)
{
this._lock();
- this._finishedCallback = callback;
- this._innerParse(text);
+ return this._innerParseContentPromise(Promise.resolve(text));
pfeldman 2014/11/10 10:23:25 return this._innerParse(text);
},
dispose: function()
@@ -47,6 +72,8 @@ WebInspector.CSSParser.prototype = {
if (this._worker) {
this._worker.terminate();
delete this._worker;
+ } else {
+ this._disposed = true;
}
},
@@ -71,11 +98,26 @@ WebInspector.CSSParser.prototype = {
/**
* @param {?string} text
+ * @return {!Promise.<!Array.<!WebInspector.CSSParser.Rule>>}
*/
_innerParse: function(text)
{
+ if (!this._worker) {
+ this._unlock();
pfeldman 2014/11/10 10:23:25 return new Promise(promiseBody); function promise
+ return Promise.resolve([]);
+ }
this._rules = [];
this._worker.postMessage({ method: "parseCSS", params: { content: text } });
+ return new Promise(rememberFulfillCallback.bind(this));
+
+ /**
+ * @this {!WebInspector.CSSParser}
+ */
+ function rememberFulfillCallback(fulfill)
+ {
+ console.assert(!this._fulfillCallback, "_innerParse() invoked during parsing");
+ this._fulfillCallback = fulfill;
pfeldman 2014/11/10 10:23:25 I am lost here.
+ }
},
/**
@@ -90,14 +132,14 @@ WebInspector.CSSParser.prototype = {
if (data.isLastChunk)
this._onFinishedParsing();
- this.dispatchEventToListeners(WebInspector.CSSParser.Events.RulesParsed);
},
_onFinishedParsing: function()
{
this._unlock();
- if (this._finishedCallback)
- this._finishedCallback(this._rules);
+ if (this._fulfillCallback)
+ this._fulfillCallback(this._rules);
+ delete this._fulfillCallback;
},
__proto__: WebInspector.Object.prototype,

Powered by Google App Engine
This is Rietveld 408576698