Title: [210261] trunk
Revision
210261
Author
[email protected]
Date
2017-01-03 17:32:13 -0800 (Tue, 03 Jan 2017)

Log Message

Web Inspector: WrappedPromise constructor should behave like the Promise constructor
https://bugs.webkit.org/show_bug.cgi?id=166523

Reviewed by Joseph Pecoraro.

Source/WebInspectorUI:

* UserInterface/Models/WrappedPromise.js:
(WebInspector.WrappedPromise):
- Return the result of 'work' from the inner promise
so WrappedPromise.promise can be chained.
- Provide shim resolve, reject callbacks as parameters.

(WebInspector.WrappedPromise.prototype.get settled): Added.
Tells whether we already resolved or rejected the promise.

(WebInspector.WrappedPromise.prototype.resolve):
(WebInspector.WrappedPromise.prototype.reject):
Throw an error if already settled and update the flag.

LayoutTests:

* inspector/unit-tests/wrapped-promise-expected.txt: Added.
* inspector/unit-tests/wrapped-promise.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (210260 => 210261)


--- trunk/LayoutTests/ChangeLog	2017-01-04 01:26:58 UTC (rev 210260)
+++ trunk/LayoutTests/ChangeLog	2017-01-04 01:32:13 UTC (rev 210261)
@@ -1,3 +1,13 @@
+2017-01-03  Brian Burg  <[email protected]>
+
+        Web Inspector: WrappedPromise constructor should behave like the Promise constructor
+        https://bugs.webkit.org/show_bug.cgi?id=166523
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/unit-tests/wrapped-promise-expected.txt: Added.
+        * inspector/unit-tests/wrapped-promise.html: Added.
+
 2017-01-03  Joseph Pecoraro  <[email protected]>
 
         Web Inspector: Address failures under LayoutTests/inspector/debugger/stepping

Added: trunk/LayoutTests/inspector/unit-tests/wrapped-promise-expected.txt (0 => 210261)


--- trunk/LayoutTests/inspector/unit-tests/wrapped-promise-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/unit-tests/wrapped-promise-expected.txt	2017-01-04 01:32:13 UTC (rev 210261)
@@ -0,0 +1,12 @@
+Testing methods of WrappedPromise.
+
+
+== Running test suite: WrappedPromise
+-- Running test case: WrappedPromise.constructor resolve
+-- Running test case: WrappedPromise.constructor reject
+-- Running test case: WrappedPromise.prototype.resolve
+-- Running test case: WrappedPromise.prototype.reject
+-- Running test case: WrappedPromise.prototype.get settled
+PASS: Should not be settled yet.
+PASS: Should be settled now.
+

Added: trunk/LayoutTests/inspector/unit-tests/wrapped-promise.html (0 => 210261)


--- trunk/LayoutTests/inspector/unit-tests/wrapped-promise.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/unit-tests/wrapped-promise.html	2017-01-04 01:32:13 UTC (rev 210261)
@@ -0,0 +1,80 @@
+<!doctype html>
+<html>
+<head>
+<script src=""
+<script>
+function test()
+{
+    let suite = InspectorTest.createAsyncSuite("WrappedPromise");
+
+    suite.addTestCase({
+        name: "WrappedPromise.constructor resolve",
+        description: "Should be able to resolve the promise using the 'resolve' callback argument.",
+        test(resolve, reject) {
+            let result = new WebInspector.WrappedPromise((resolve, reject) => {
+                resolve();
+            });
+
+            result.promise.then(resolve, reject);
+        }
+    });
+
+    suite.addTestCase({
+        name: "WrappedPromise.constructor reject",
+        description: "Should be able to reject the promise using the 'reject' callback argument.",
+        test(resolve, reject) {
+            let result = new WebInspector.WrappedPromise((resolve, reject) => {
+                reject();
+            });
+
+            result.promise.then(reject, resolve);
+        }
+    });
+
+    suite.addTestCase({
+        name: "WrappedPromise.prototype.resolve",
+        description: "Should be able to resolve the promise using resolve().",
+        test(resolve, reject) {
+            let result = new WebInspector.WrappedPromise;
+            result.promise.then(resolve, reject);
+
+            result.resolve();
+        }
+    });
+
+    suite.addTestCase({
+        name: "WrappedPromise.prototype.reject",
+        description: "Should be able to reject the promise using reject().",
+        test(resolve, reject) {
+            let result = new WebInspector.WrappedPromise;
+            result.promise.then(reject, resolve);
+
+            result.reject();
+        }
+    });
+
+    suite.addTestCase({
+        name: "WrappedPromise.prototype.get settled",
+        description: "Should be able to see that the promise is settled.",
+        test(resolve, reject) {
+            let result = new WebInspector.WrappedPromise;
+            InspectorTest.expectFalse(result.settled, "Should not be settled yet.");
+
+            result.promise.then(() => {
+                InspectorTest.expectThat(result.settled, "Should be settled now.");
+                resolve();
+            })
+            .catch(reject);
+
+            result.resolve();
+        }
+    });
+
+    suite.runTestCasesAndFinish();
+}
+</script>
+</head>
+<body _onload_="runTest()">
+    <p>Testing methods of WrappedPromise.</p>
+</body>
+</html>

Modified: trunk/Source/WebInspectorUI/ChangeLog (210260 => 210261)


--- trunk/Source/WebInspectorUI/ChangeLog	2017-01-04 01:26:58 UTC (rev 210260)
+++ trunk/Source/WebInspectorUI/ChangeLog	2017-01-04 01:32:13 UTC (rev 210261)
@@ -1,3 +1,23 @@
+2017-01-03  Brian Burg  <[email protected]>
+
+        Web Inspector: WrappedPromise constructor should behave like the Promise constructor
+        https://bugs.webkit.org/show_bug.cgi?id=166523
+
+        Reviewed by Joseph Pecoraro.
+
+        * UserInterface/Models/WrappedPromise.js:
+        (WebInspector.WrappedPromise):
+        - Return the result of 'work' from the inner promise
+        so WrappedPromise.promise can be chained.
+        - Provide shim resolve, reject callbacks as parameters.
+
+        (WebInspector.WrappedPromise.prototype.get settled): Added.
+        Tells whether we already resolved or rejected the promise.
+
+        (WebInspector.WrappedPromise.prototype.resolve):
+        (WebInspector.WrappedPromise.prototype.reject):
+        Throw an error if already settled and update the flag.
+
 2017-01-03  Devin Rousso  <[email protected]>
 
         Web Inspector: color picker should feature an editable CSS value

Modified: trunk/Source/WebInspectorUI/UserInterface/Models/WrappedPromise.js (210260 => 210261)


--- trunk/Source/WebInspectorUI/UserInterface/Models/WrappedPromise.js	2017-01-04 01:26:58 UTC (rev 210260)
+++ trunk/Source/WebInspectorUI/UserInterface/Models/WrappedPromise.js	2017-01-04 01:32:13 UTC (rev 210261)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple Inc. All rights reserved.
+ * Copyright (C) 2015, 2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -27,17 +27,25 @@
 {
     constructor(work)
     {
+        this._settled = false;
         this._promise = new Promise((resolve, reject) => {
-            this._resolve = resolve;
-            this._reject = reject;
+            this._resolveCallback = resolve;
+            this._rejectCallback = reject;
 
+            // Allow work to resolve or reject the promise by shimming our
+            // internal callbacks. This ensures that this._settled gets set properly.
             if (work && typeof work === "function")
-                work();
+                return work(this.resolve.bind(this), this.reject.bind(this));
         });
     }
 
     // Public
 
+    get settled()
+    {
+        return this._settled;
+    }
+
     get promise()
     {
         return this._promise;
@@ -45,11 +53,19 @@
 
     resolve(value)
     {
-        this._resolve(value);
+        if (this._settled)
+            throw new Error("Promise is already settled, cannot call resolve().");
+
+        this._settled = true;
+        this._resolveCallback(value);
     }
 
     reject(value)
     {
-        this._reject(value);
+        if (this._settled)
+            throw new Error("Promise is already settled, cannot call reject().");
+
+        this._settled = true;
+        this._rejectCallback(value);
     }
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to