ChromeDevTools / devtools-protocol

Chrome DevTools Protocol
https://chromedevtools.github.io/devtools-protocol/
BSD 3-Clause "New" or "Revised" License
1.15k stars 226 forks source link

Add `Promise` version of events #249

Closed kazarmy closed 1 year ago

kazarmy commented 3 years ago

This pr extends the generator script to add Promise versions of events, e.g.:

domContentEventFired(): Promise<Protocol.Page.DomContentEventFiredEvent>;

as a complement for

on(event: 'domContentEventFired', listener: (params: Protocol.Page.DomContentEventFiredEvent) => void): void;

The motivation for this pr is to provide typings support for the following syntax:

const {Page} = client;
await Page.domContentEventFired();

Currently, typings support is provided for the following syntax via DefinitelyTyped's chrome-remote-interface typings:

await client['Page.domContentEventFired']();

but it is less elegant and the client part shouldn't really be necessary. Unfortunately, it doesn't seem possible to provide typings for the former syntax via protocol-mapping.d.ts since it is not possible to split something like 'Page.domContentEventFired' into its component parts with the current version of TypeScript (4.3).


In the code, there are two forEach loops for emitApiEvent() so that all the on() overloads are kept together.

Fixes #246.

kazarmy commented 1 year ago

@types/chrome-remote-interface 0.31.8 provides typings for Promise versions of on(event: ...) so this pr will be closed.