Web Inspector: protocol command invocations should return a promise if no callback...
authorburg@cs.washington.edu <burg@cs.washington.edu@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 6 Aug 2014 17:59:06 +0000 (17:59 +0000)
committerburg@cs.washington.edu <burg@cs.washington.edu@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 6 Aug 2014 17:59:06 +0000 (17:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=130702

Reviewed by Timothy Hatcher.

Source/WebInspectorUI:

This allows the trailing Agent.command.promise(args) to be dropped in favor of just
Agent.command(args). It should make it a bit easier to convert code to use promises.

Test: LayoutTests/inspector/protocol-promise-result.html

* UserInterface/Controllers/ReplayManager.js: Drop use of .promise().
* UserInterface/Controllers/TimelineManager.js: Drop use of .promise().
(WebInspector.TimelineManager.prototype.startCapturing):
* UserInterface/Protocol/InspectorBackend.js:
(.callable): Redirect to the promise entry point if the last argument isn't a function.
(InspectorBackend.Command.create):

LayoutTests:

Addd a test for recieving protocol command results through an explicit callback,
via the .promise() entry point, and via an implicitly created promise.

* inspector/protocol-promise-result-expected.txt: Added.
* inspector/protocol-promise-result.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@172158 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/protocol-promise-result-expected.txt [new file with mode: 0644]
LayoutTests/inspector/protocol-promise-result.html [new file with mode: 0644]
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Controllers/ReplayManager.js
Source/WebInspectorUI/UserInterface/Controllers/TimelineManager.js
Source/WebInspectorUI/UserInterface/Protocol/InspectorBackend.js

index 6d12589..bd5a0a7 100644 (file)
@@ -1,3 +1,16 @@
+2014-08-06  Brian J. Burg  <burg@cs.washington.edu>
+
+        Web Inspector: protocol command invocations should return a promise if no callback is supplied
+        https://bugs.webkit.org/show_bug.cgi?id=130702
+
+        Reviewed by Timothy Hatcher.
+
+        Addd a test for recieving protocol command results through an explicit callback,
+        via the .promise() entry point, and via an implicitly created promise.
+
+        * inspector/protocol-promise-result-expected.txt: Added.
+        * inspector/protocol-promise-result.html: Added.
+
 2014-07-28  Myles C. Maxfield  <mmaxfield@apple.com>
 
         Text-shadow with (0, 0) offset and radius = 0 is ugly
diff --git a/LayoutTests/inspector/protocol-promise-result-expected.txt b/LayoutTests/inspector/protocol-promise-result-expected.txt
new file mode 100644 (file)
index 0000000..1eb7a53
--- /dev/null
@@ -0,0 +1,7 @@
+Testing that the inspector backend can return command results using promises.
+
+Using a callback, got RuntimeAgent.evaluate() result: 41
+Setting up two promises for RuntimeAgent.evaluate.
+Using a promise, got RuntimeAgent.evaluate() result: 42
+Using a promise, got RuntimeAgent.evaluate() result: 43
+
diff --git a/LayoutTests/inspector/protocol-promise-result.html b/LayoutTests/inspector/protocol-promise-result.html
new file mode 100644 (file)
index 0000000..52dc706
--- /dev/null
@@ -0,0 +1,38 @@
+<!doctype html>
+<html>
+<head>
+<script type="text/javascript" src="../http/tests/inspector/inspector-test.js"></script>
+<script>
+function test()
+{
+    var c1 = RuntimeAgent.evaluate("41", function(err, result, wasThrown) {
+        InspectorTest.log("Using a callback, got RuntimeAgent.evaluate() result: " + result.value);
+
+        InspectorTest.log("Setting up two promises for RuntimeAgent.evaluate.");
+
+        var p1 = RuntimeAgent.evaluate.promise("42");
+        var p2 = RuntimeAgent.evaluate("43");
+
+        InspectorTest.assert(!(c1 instanceof Promise), "A promise was returned even though a callback was supplied to the command!");
+
+        p1.then(function(payload) {
+            InspectorTest.log("Using a promise, got RuntimeAgent.evaluate() result: " + payload.result.value);
+        });
+        p2.then(function(payload) {
+            InspectorTest.log("Using a promise, got RuntimeAgent.evaluate() result: " + payload.result.value);
+        });
+
+        Promise.all([p1, p2]).then(function() {
+            InspectorTest.completeTest();
+        }).catch(function(error) {
+            InspectorTest.log("ERROR: " + error);
+            InspectorTest.completeTest();
+        })
+    });
+}
+</script>
+</head>
+<body onload="runTest()">
+    <p>Testing that the inspector backend can return command results using promises.</p>
+</body>
+</html>
index 5cf1b63..6e621a8 100644 (file)
@@ -1,3 +1,22 @@
+2014-08-06  Brian J. Burg  <burg@cs.washington.edu>
+
+        Web Inspector: protocol command invocations should return a promise if no callback is supplied
+        https://bugs.webkit.org/show_bug.cgi?id=130702
+
+        Reviewed by Timothy Hatcher.
+
+        This allows the trailing Agent.command.promise(args) to be dropped in favor of just
+        Agent.command(args). It should make it a bit easier to convert code to use promises.
+
+        Test: LayoutTests/inspector/protocol-promise-result.html
+
+        * UserInterface/Controllers/ReplayManager.js: Drop use of .promise().
+        * UserInterface/Controllers/TimelineManager.js: Drop use of .promise().
+        (WebInspector.TimelineManager.prototype.startCapturing):
+        * UserInterface/Protocol/InspectorBackend.js:
+        (.callable): Redirect to the promise entry point if the last argument isn't a function.
+        (InspectorBackend.Command.create):
+
 2014-08-05  James Craig  <jcraig@apple.com>
 
         Web Inspector: AXI: Add label string once AccessibilityObject::computedLabel() is available
index ded6684..c75942b 100644 (file)
@@ -52,7 +52,7 @@ WebInspector.ReplayManager = function()
 
     var instance = this;
 
-    this._initializationPromise = ReplayAgent.currentReplayState.promise()
+    this._initializationPromise = ReplayAgent.currentReplayState()
         .then(function(payload) {
             console.assert(payload.sessionState in WebInspector.ReplayManager.SessionState, "Unknown session state: " + payload.sessionState);
             console.assert(payload.segmentState in WebInspector.ReplayManager.SegmentState, "Unknown segment state: " + payload.segmentState);
@@ -65,7 +65,7 @@ WebInspector.ReplayManager = function()
 
             instance._initialized = true;
         }).then(function() {
-            return ReplayAgent.getAvailableSessions.promise();
+            return ReplayAgent.getAvailableSessions();
         }).then(function(payload) {
             for (var sessionId of payload.ids)
                 instance.sessionCreated(sessionId);
index ac96e17..a7653fd 100644 (file)
@@ -86,7 +86,7 @@ WebInspector.TimelineManager.prototype = {
         if (!this._activeRecording || shouldCreateRecording)
             this._loadNewRecording();
 
-        var result = TimelineAgent.start.promise();
+        var result = TimelineAgent.start();
 
         // COMPATIBILITY (iOS 7): recordingStarted event did not exist yet. Start explicitly.
         if (!TimelineAgent.hasEvent("recordingStarted")) {
index 4d99733..fe4e93e 100644 (file)
@@ -340,7 +340,11 @@ InspectorBackend.Command.create = function(backend, commandName, callSignature,
     var instance = new InspectorBackend.Command(backend, commandName, callSignature, replySignature);
 
     function callable() {
-        instance._invokeWithArguments.apply(instance, arguments);
+        // If the last argument to the command is not a function, return a result promise.
+        if (!arguments.length || typeof arguments[arguments.length - 1] !== "function")
+            return instance.promise.apply(instance, arguments);
+
+        return instance._invokeWithArguments.apply(instance, arguments);
     }
     callable._instance = instance;
     callable.__proto__ = InspectorBackend.Command.prototype;