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

Unified Diff: ui/webui/resources/js/cr.js

Issue 1622663002: WebUI: Replace cr.sendWithCallback with cr.sendWithPromise. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments: Removing new line before anonymous function. Created 4 years, 11 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: ui/webui/resources/js/cr.js
diff --git a/ui/webui/resources/js/cr.js b/ui/webui/resources/js/cr.js
index a38c59f84732e820c00407287b3ef00c37b41bb2..984a5c25df05cf9cd2e9dafe5c79fe86c2843b6a 100644
--- a/ui/webui/resources/js/cr.js
+++ b/ui/webui/resources/js/cr.js
@@ -313,48 +313,50 @@ var cr = function() {
}
/**
- * The mapping used by the sendWithCallback mechanism to tie the callback
- * supplied to an invocation of sendWithCallback with the WebUI response
- * sent by the browser in response to the chrome.send call. The mapping is
- * from ID to callback function; the ID is generated by sendWithCallback and
- * is unique across all invocations of said method.
- * @type {!Object<Function>}
+ * The mapping used by the sendWithPromise mechanism to tie the Promise
+ * returned to callers with the corresponding WebUI response. The mapping is
+ * from ID to the Promise resolver function; the ID is generated by
+ * sendWithPromise and is unique across all invocations of said method.
+ * @type {!Object<!Function>}
*/
- var chromeSendCallbackMap = Object.create(null);
+ var chromeSendResolverMap = {};
/**
* The named method the WebUI handler calls directly in response to a
- * chrome.send call that expects a callback. The handler requires no knowledge
+ * chrome.send call that expects a response. The handler requires no knowledge
* of the specific name of this method, as the name is passed to the handler
* as the first argument in the arguments list of chrome.send. The handler
* must pass the ID, also sent via the chrome.send arguments list, as the
* first argument of the JS invocation; additionally, the handler may
- * supply any number of other arguments that will be forwarded to the
- * callback.
- * @param {string} id The unique ID identifying the callback method this
- * response is tied to.
+ * supply any number of other arguments that will be included in the response.
+ * @param {string} id The unique ID identifying the Promise this response is
+ * tied to.
+ * @param {...*} var_args Variable number of arguments to be included in the
+ * response.
*/
- function webUIResponse(id) {
- chromeSendCallbackMap[id].apply(
- null, Array.prototype.slice.call(arguments, 1));
- delete chromeSendCallbackMap[id];
+ function webUIResponse(id, var_args) {
+ var resolverFn = chromeSendResolverMap[id];
+ delete chromeSendResolverMap[id];
+ // Promise#resolve accepts only one value, therefore wrapping all arguments
+ // passed from C++ to JS in an array.
+ resolverFn(Array.prototype.slice.call(arguments, 1));
Dan Beam 2016/01/26 00:54:52 this seems saner and simpler, double win
}
/**
- * A variation of chrome.send which allows the client to receive a direct
- * callback without requiring the handler to have specific knowledge of any
- * JS internal method names or state. The callback will be removed from the
- * mapping once it has fired.
+ * A variation of chrome.send, suitable for messages that expect a single
+ * response from C++.
* @param {string} methodName The name of the WebUI handler API.
- * @param {Array|undefined} args Arguments for the method call sent to the
- * WebUI handler. Pass undefined if no args should be sent to the handler.
- * @param {Function} callback A callback function which is called (indirectly)
- * by the WebUI handler.
+ * @param {...*} var_args Varibale number of arguments to be forwarded to the
+ * C++ call.
+ * @return {!Promise}
*/
- function sendWithCallback(methodName, args, callback) {
- var id = methodName + createUid();
- chromeSendCallbackMap[id] = callback;
- chrome.send(methodName, ['cr.webUIResponse', id].concat(args || []));
+ function sendWithPromise(methodName, var_args) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ return new Promise(function(resolve, reject) {
+ var id = methodName + '_' + createUid();
+ chromeSendResolverMap[id] = resolve;
+ chrome.send(methodName, ['cr.webUIResponse', id].concat(args));
+ });
}
/**
@@ -403,7 +405,7 @@ var cr = function() {
getUid: getUid,
makePublic: makePublic,
webUIResponse: webUIResponse,
- sendWithCallback: sendWithCallback,
+ sendWithPromise: sendWithPromise,
webUIListenerCallback: webUIListenerCallback,
addWebUIListener: addWebUIListener,
PropertyKind: PropertyKind,

Powered by Google App Engine
This is Rietveld 408576698