Web Inspector: add Object.awaitEvent which is like singleFireEventListener but return...
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Sep 2016 22:03:10 +0000 (22:03 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Sep 2016 22:03:10 +0000 (22:03 +0000)
https://bugs.webkit.org/show_bug.cgi?id=161451

Patch by Devin Rousso <dcrousso+webkit@gmail.com> on 2016-09-01
Reviewed by Brian Burg.

Source/WebInspectorUI:

* UserInterface/Base/Object.js:
(WebInspector.Object.awaitEvent):
(WebInspector.Object.prototype.awaitEvent):

LayoutTests:

* inspector/unit-tests/object-expected.txt:
* inspector/unit-tests/object.html:

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

LayoutTests/ChangeLog
LayoutTests/inspector/unit-tests/object-expected.txt
LayoutTests/inspector/unit-tests/object.html
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Base/Object.js

index 321cb96..2a72450 100644 (file)
@@ -1,3 +1,13 @@
+2016-09-01  Devin Rousso  <dcrousso+webkit@gmail.com>
+
+        Web Inspector: add Object.awaitEvent which is like singleFireEventListener but returns a promise
+        https://bugs.webkit.org/show_bug.cgi?id=161451
+
+        Reviewed by Brian Burg.
+
+        * inspector/unit-tests/object-expected.txt:
+        * inspector/unit-tests/object.html:
+
 2016-09-01  Ryosuke Niwa  <rniwa@webkit.org>
 
         Fix a typo in the previous commit. I didn't mean to comment out this test case I added per a review comment.
index 76784c9..6f1e9c9 100644 (file)
@@ -2,6 +2,10 @@ Testing WebInspector.Object.
 
 
 == Running test suite: Object
--- Running test case: Events propagation
+-- Running test case: WebInspector.Object.prototype.dispatchEventToListeners
+Dispatch count: 1
+
+-- Running test case: WebInspector.Object.prototype.awaitEvent
+PASS: Await event handler should be dispatched just once.
 Dispatch count: 1
 
index 19c3e8c..b8c5f90 100644 (file)
@@ -5,12 +5,12 @@
 <script>
 function test()
 {
-    let suite = InspectorTest.createSyncSuite("Object");
+    let suite = InspectorTest.createAsyncSuite("Object");
 
     suite.addTestCase({
-        name: "Events propagation",
+        name: "WebInspector.Object.prototype.dispatchEventToListeners",
         description: "WebInspector.Object shouldn't dispatch constructor-level events multiple times",
-        test: function() {
+        test(resolve, reject) {
             class Parent extends WebInspector.Object {}
             class Child extends Parent {}
 
@@ -23,6 +23,35 @@ function test()
             child.dispatchEventToListeners(eventName);
 
             InspectorTest.log("Dispatch count: " + dispatchCount);
+
+            resolve();
+        }
+    });
+
+    suite.addTestCase({
+        name: "WebInspector.Object.prototype.awaitEvent",
+        description: "awaitEvent should only trigger once",
+        test(resolve, reject) {
+            const eventName = "test";
+            let dispatchCount = 0;
+
+            let object = new WebInspector.Object;
+
+            object.awaitEvent(eventName).then(() => {
+                dispatchCount++;
+
+                InspectorTest.expectThat(dispatchCount === 1, "Await event handler should be dispatched just once.");
+                InspectorTest.log("Dispatch count: " + dispatchCount);
+
+                // Resolve test on second event dispatch.
+                object.awaitEvent(eventName).then(() => {
+                    resolve();
+                });
+
+                object.dispatchEventToListeners(eventName);
+            });
+
+            object.dispatchEventToListeners(eventName);
         }
     });
 
index b24be9a..ee4e431 100644 (file)
@@ -1,3 +1,14 @@
+2016-09-01  Devin Rousso  <dcrousso+webkit@gmail.com>
+
+        Web Inspector: add Object.awaitEvent which is like singleFireEventListener but returns a promise
+        https://bugs.webkit.org/show_bug.cgi?id=161451
+
+        Reviewed by Brian Burg.
+
+        * UserInterface/Base/Object.js:
+        (WebInspector.Object.awaitEvent):
+        (WebInspector.Object.prototype.awaitEvent):
+
 2016-09-01  Nikita Vasilyev  <nvasilyev@apple.com>
 
         Web Inspector: Minification detection produces false positives for small resources
index 56e5cec..87c19f6 100644 (file)
@@ -98,6 +98,13 @@ WebInspector.Object = class WebInspectorObject
         console.assert(didDelete, "removeEventListener cannot remove " + eventType.toString() + " because it doesn't exist.");
     }
 
+    static awaitEvent(eventType)
+    {
+        return new Promise((resolve, reject) => {
+            this.singleFireEventListener(eventType, (event) => resolve(event), null);
+        });
+    }
+
     // Only used by tests.
     static hasEventListeners(eventType)
     {
@@ -131,6 +138,7 @@ WebInspector.Object = class WebInspectorObject
     addEventListener() { return WebInspector.Object.addEventListener.apply(this, arguments); }
     singleFireEventListener() { return WebInspector.Object.singleFireEventListener.apply(this, arguments); }
     removeEventListener() { return WebInspector.Object.removeEventListener.apply(this, arguments); }
+    awaitEvent() { return WebInspector.Object.awaitEvent.apply(this, arguments); }
     hasEventListeners() { return WebInspector.Object.hasEventListeners.apply(this, arguments); }
     retainedObjectsWithPrototype() { return WebInspector.Object.retainedObjectsWithPrototype.apply(this, arguments); }