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

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

Issue 1204393002: DevTools: [CSS] promisify CSSStyleModel fetching methods (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 6 months 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/CSSStyleModel.js
diff --git a/Source/devtools/front_end/sdk/CSSStyleModel.js b/Source/devtools/front_end/sdk/CSSStyleModel.js
index 1214ec916a7991f7d842c33e676c605d8546afba..359c415192c44743492841d72ff4231d651a778b 100644
--- a/Source/devtools/front_end/sdk/CSSStyleModel.js
+++ b/Source/devtools/front_end/sdk/CSSStyleModel.js
@@ -83,9 +83,9 @@ WebInspector.CSSStyleModel.MediaTypes = ["all", "braille", "embossed", "handheld
WebInspector.CSSStyleModel.prototype = {
/**
- * @param {function(!Array.<!WebInspector.CSSMedia>)} userCallback
+ * @return {!Promise.<!Array.<!WebInspector.CSSMedia>>}
*/
- getMediaQueries: function(userCallback)
+ mediaQueriesPromise: function()
{
/**
* @param {?Protocol.Error} error
@@ -98,9 +98,7 @@ WebInspector.CSSStyleModel.prototype = {
return !error && payload ? WebInspector.CSSMedia.parseMediaArrayPayload(this, payload) : [];
}
- this._agent.getMediaQueries(parsePayload.bind(this))
- .catchException([])
- .then(userCallback);
+ return this._agent.getMediaQueries(parsePayload.bind(this));
},
/**
@@ -172,31 +170,18 @@ WebInspector.CSSStyleModel.prototype = {
/**
* @param {!DOMAgent.NodeId} nodeId
- * @param {boolean} excludePseudo
- * @param {boolean} excludeInherited
- * @param {function(?WebInspector.CSSStyleModel.MatchedStyleResult)} userCallback
- */
- getMatchedStylesAsync: function(nodeId, excludePseudo, excludeInherited, userCallback)
- {
- this.matchedStylesPromise(nodeId, excludePseudo, excludeInherited)
- .catchException(null)
- .then(userCallback);
- },
-
- /**
- * @param {!DOMAgent.NodeId} nodeId
- * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback
+ * @return {!Promise.<?WebInspector.CSSStyleDeclaration>}
*/
- getComputedStyleAsync: function(nodeId, userCallback)
+ computedStylePromise: function(nodeId)
{
- this._styleLoader.getComputedStyle(nodeId, userCallback);
+ return this._styleLoader.computedStylePromise(nodeId);
},
/**
* @param {number} nodeId
- * @param {function(?Array.<!CSSAgent.PlatformFontUsage>)} callback
+ * @return {!Promise.<?Array.<!CSSAgent.PlatformFontUsage>>}
*/
- getPlatformFontsForNode: function(nodeId, callback)
+ platformFontsPromise: function(nodeId)
{
/**
* @param {?Protocol.Error} error
@@ -208,9 +193,7 @@ WebInspector.CSSStyleModel.prototype = {
return !error && fonts ? fonts : null;
}
- this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback)
- .catchException(null)
- .then(callback)
+ return this._agent.getPlatformFontsForNode(nodeId, platformFontsCallback);
},
/**
@@ -263,17 +246,6 @@ WebInspector.CSSStyleModel.prototype = {
},
/**
- * @param {!DOMAgent.NodeId} nodeId
- * @param {function(?WebInspector.CSSStyleModel.InlineStyleResult)} userCallback
- */
- getInlineStylesAsync: function(nodeId, userCallback)
- {
- this.inlineStylesPromise(nodeId)
- .catchException(null)
- .then(userCallback);
- },
-
- /**
* @param {!WebInspector.DOMNode} node
* @param {string} pseudoClass
* @param {boolean} enable
@@ -1917,27 +1889,21 @@ WebInspector.CSSDispatcher.prototype = {
WebInspector.CSSStyleModel.ComputedStyleLoader = function(cssModel)
{
this._cssModel = cssModel;
- /** @type {!Object.<*, !Array.<function(?WebInspector.CSSStyleDeclaration)>>} */
- this._nodeIdToCallbackData = {};
+ /** @type {!Map.<!DOMAgent.NodeId, !Promise.<?WebInspector.CSSStyleDeclaration>>} */
+ this._nodeIdToPromise = new Map();
}
WebInspector.CSSStyleModel.ComputedStyleLoader.prototype = {
/**
* @param {!DOMAgent.NodeId} nodeId
- * @param {function(?WebInspector.CSSStyleDeclaration)} userCallback
+ * @return {!Promise.<?WebInspector.CSSStyleDeclaration>}
*/
- getComputedStyle: function(nodeId, userCallback)
+ computedStylePromise: function(nodeId)
{
- if (this._nodeIdToCallbackData[nodeId]) {
- this._nodeIdToCallbackData[nodeId].push(userCallback);
- return;
- }
+ if (!this._nodeIdToPromise[nodeId])
+ this._nodeIdToPromise[nodeId] = this._cssModel._agent.getComputedStyleForNode(nodeId, parsePayload.bind(this)).then(cleanUp.bind(this));
- this._nodeIdToCallbackData[nodeId] = [userCallback];
-
- this._cssModel._agent.getComputedStyleForNode(nodeId, parsePayload.bind(this))
- .catchException(null)
- .then(broadcast.bind(this, nodeId))
+ return this._nodeIdToPromise[nodeId];
/**
* @param {?Protocol.Error} error
@@ -1951,21 +1917,14 @@ WebInspector.CSSStyleModel.ComputedStyleLoader.prototype = {
}
/**
- * @param {!DOMAgent.NodeId} nodeId
* @param {?WebInspector.CSSStyleDeclaration} computedStyle
+ * @return {?WebInspector.CSSStyleDeclaration}
* @this {WebInspector.CSSStyleModel.ComputedStyleLoader}
*/
- function broadcast(nodeId, computedStyle)
+ function cleanUp(computedStyle)
{
- var callbacks = this._nodeIdToCallbackData[nodeId];
-
- // The loader has been reset.
- if (!callbacks)
- return;
-
- delete this._nodeIdToCallbackData[nodeId];
- for (var i = 0; i < callbacks.length; ++i)
- callbacks[i](computedStyle);
+ delete this._nodeIdToPromise[nodeId];
+ return computedStyle;
}
}
}

Powered by Google App Engine
This is Rietveld 408576698