Bug 161451 - Web Inspector: add Object.awaitEvent which is like singleFireEventListener but returns a promise
Summary: Web Inspector: add Object.awaitEvent which is like singleFireEventListener bu...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Devin Rousso
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2016-08-31 12:27 PDT by BJ Burg
Modified: 2016-09-01 15:05 PDT (History)
10 users (show)

See Also:


Attachments
Patch (2.28 KB, patch)
2016-08-31 14:25 PDT, Devin Rousso
no flags Details | Formatted Diff | Diff
Patch (4.70 KB, patch)
2016-08-31 15:36 PDT, Devin Rousso
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews100 for mac-yosemite (845.19 KB, application/zip)
2016-08-31 16:26 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews106 for mac-yosemite-wk2 (1.13 MB, application/zip)
2016-08-31 16:26 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews112 for mac-yosemite (1.50 MB, application/zip)
2016-08-31 16:33 PDT, Build Bot
no flags Details
Patch (5.02 KB, patch)
2016-08-31 17:22 PDT, Devin Rousso
no flags Details | Formatted Diff | Diff
Patch (5.57 KB, patch)
2016-09-01 00:04 PDT, Devin Rousso
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description BJ Burg 2016-08-31 12:27:09 PDT
This will make a bunch of tests easier to read.
Comment 1 Radar WebKit Bug Importer 2016-08-31 12:28:22 PDT
<rdar://problem/28100779>
Comment 2 Devin Rousso 2016-08-31 14:25:50 PDT
Created attachment 287542 [details]
Patch
Comment 3 Matt Baker 2016-08-31 14:43:25 PDT
Comment on attachment 287542 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287542&action=review

> Source/WebInspectorUI/UserInterface/Base/Object.js:105
> +                resolve(...arguments);

This can be written with an arrow:
this.singleFireEventListener(eventType, () => resolve(...arguments));
Comment 4 BJ Burg 2016-08-31 15:10:00 PDT
Comment on attachment 287542 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287542&action=review

>> Source/WebInspectorUI/UserInterface/Base/Object.js:105
>> +                resolve(...arguments);
> 
> This can be written with an arrow:
> this.singleFireEventListener(eventType, () => resolve(...arguments));

Resolve can only be passed one argument, the others are ignored. Please add a named argument 'event' and pass it.
Comment 5 Devin Rousso 2016-08-31 15:36:07 PDT
Created attachment 287551 [details]
Patch
Comment 6 BJ Burg 2016-08-31 16:13:21 PDT
Comment on attachment 287551 [details]
Patch

Regressions: Unexpected text-only failures (1)
  inspector/unit-tests/object.html [ Failure ]
Comment 7 Build Bot 2016-08-31 16:26:26 PDT
Comment on attachment 287551 [details]
Patch

Attachment 287551 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.webkit.org/results/1982987

New failing tests:
inspector/unit-tests/object.html
Comment 8 Build Bot 2016-08-31 16:26:29 PDT
Created attachment 287556 [details]
Archive of layout-test-results from ews100 for mac-yosemite

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews100  Port: mac-yosemite  Platform: Mac OS X 10.10.5
Comment 9 Build Bot 2016-08-31 16:26:39 PDT
Comment on attachment 287551 [details]
Patch

Attachment 287551 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.webkit.org/results/1982977

New failing tests:
inspector/unit-tests/object.html
Comment 10 Build Bot 2016-08-31 16:26:42 PDT
Created attachment 287558 [details]
Archive of layout-test-results from ews106 for mac-yosemite-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews106  Port: mac-yosemite-wk2  Platform: Mac OS X 10.10.5
Comment 11 Build Bot 2016-08-31 16:33:55 PDT
Comment on attachment 287551 [details]
Patch

Attachment 287551 [details] did not pass mac-debug-ews (mac):
Output: http://webkit-queues.webkit.org/results/1982990

New failing tests:
inspector/unit-tests/object.html
Comment 12 Build Bot 2016-08-31 16:33:57 PDT
Created attachment 287562 [details]
Archive of layout-test-results from ews112 for mac-yosemite

The attached test failures were seen while running run-webkit-tests on the mac-debug-ews.
Bot: ews112  Port: mac-yosemite  Platform: Mac OS X 10.10.5
Comment 13 Joseph Pecoraro 2016-08-31 16:59:22 PDT
Comment on attachment 287551 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287551&action=review

> LayoutTests/inspector/unit-tests/object.html:38
> +            object.awaitEvent(eventName).then(() => dispatchCount++);

In a Synchronous test suite this will not work, because any Promise's then microtask is guaranteed to run asynchronously. I'm not sure how you had gotten this to work locally. Either way, switching to an Async test suite (and updating all the tests to appropriately resolve/reject may do better.

Maybe you could even have a synchronous suite runTestCasesAndNOTFinish() then run async tests right after.
Comment 14 Devin Rousso 2016-08-31 17:22:50 PDT
Created attachment 287575 [details]
Patch
Comment 15 Joseph Pecoraro 2016-08-31 18:54:28 PDT
Comment on attachment 287575 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287575&action=review

r- only because I want to see a new test! Sorry for the back and forth on this =/

> LayoutTests/inspector/unit-tests/object.html:51
> +        name: "Single Fire Promise",
> +        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.log("Dispatch count: " + dispatchCount);
> +            });
> +
> +            object.dispatchEventToListeners(eventName);
> +            object.dispatchEventToListeners(eventName);
> +
> +            resolve();
>          }
>      });

Err, resolve doesn't this call resolve before the awaitEvent's then block fires?

There should probably be a separate event that ends up resolving.

For example: (Assumes dispatchEvent is some microtask that happens before the Promise.resolve's)

    object.dispatchEventToListeners(eventName);
    object.dispatchEventToListeners(eventName);
    Promise.resolve().then(resolve, reject);

Or: (assumes the timeout fires after all event listeners have fired)

    object.dispatchEventToListeners(eventName);
    object.dispatchEventToListeners(eventName);
    setTimeout(() => { resolve() }, 10);

Or: (also best, this ensures the original is not dispatched more than once, and resolves the test after handling the second event)

    object.awaitEvent(eventName).then(() => {
        dispatchCount++;
        InspectorTest.expectThat(dispatchCount === 1, "Await event handler should be dispatched just once.");
        InspectorTest.log("Dispatch count: " + dispatchCount);
        object.awaitEvent(eventName).then(() => {
            resolve();
        });
    });

    object.dispatchEventToListeners(eventName);
    object.dispatchEventToListeners(eventName);

I prefer the last version, since it verifies both that the event is actually dispatched twice and that the original awaitEvent was dispatched just once.
Comment 16 Joseph Pecoraro 2016-08-31 18:55:43 PDT
Comment on attachment 287575 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287575&action=review

> LayoutTests/inspector/unit-tests/object.html:32
> +        name: "Single Fire Promise",

And for Unit tests I've been giving the name the name of the method they test. This could be WebInspector.Object.prototype.awaitEvent. The one above could be dispatchEventToListeners.
Comment 17 Devin Rousso 2016-09-01 00:04:08 PDT
Created attachment 287607 [details]
Patch
Comment 18 BJ Burg 2016-09-01 10:44:15 PDT
Comment on attachment 287607 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287607&action=review

r=me

> LayoutTests/inspector/unit-tests/object.html:48
> +                    resolve();

You should add the dispatchCount === 1 assertion down here too, to check that the listener didn't fire a second time even though its a single fire event listener.
Comment 19 Joseph Pecoraro 2016-09-01 11:37:16 PDT
Comment on attachment 287607 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=287607&action=review

>> LayoutTests/inspector/unit-tests/object.html:48
>> +                    resolve();
> 
> You should add the dispatchCount === 1 assertion down here too, to check that the listener didn't fire a second time even though its a single fire event listener.

Would the assertion down here matter? The only place dispatchCount changes is up above. If it changed again it would fail up above. Shrug.
Comment 20 WebKit Commit Bot 2016-09-01 15:05:03 PDT
Comment on attachment 287607 [details]
Patch

Clearing flags on attachment: 287607

Committed r205319: <http://trac.webkit.org/changeset/205319>
Comment 21 WebKit Commit Bot 2016-09-01 15:05:08 PDT
All reviewed patches have been landed.  Closing bug.