Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chrome-remote-interface] add support for shorthand callback version of events #63570

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions types/chrome-remote-interface/chrome-remote-interface-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@ function assertType<T>(value: T): T {
const loadEvent = await client['Page.loadEventFired'](); // instead of: await Page.loadEventFired();
loadEvent.timestamp;
await client['Page.interstitialHidden'](); // instead of: await Page.interstitialHidden();
// instead of: Network.requestWillBeSent((params, sessionId) => {});
const unsubscribe = client['Network.requestWillBeSent']((params, sessionId) => {
const unsubscribe = Network.requestWillBeSent((params, sessionId) => {
params.request.url;
unsubscribe();
});
const unsubscribe2 = client['Network.requestWillBeSent']((params) => {
const unsubscribeAlt = client['Network.requestWillBeSent']((params, sessionId) => {
params.request.url;
unsubscribeAlt();
});
const unsubscribe2 = Network.requestWillBeSent((params) => {
params.request.url;
unsubscribe2();
});
const unsubscribe3 = client['Page.frameResized'](() => {
const unsubscribeAlt2 = client['Network.requestWillBeSent']((params) => {
params.request.url;
unsubscribeAlt2();
});
const unsubscribe3 = Page.frameResized(() => {
unsubscribe3();
});
const unsubscribeAlt3 = client['Page.frameResized'](() => {
unsubscribeAlt3();
});
await Runtime.enable();
const loc = await Runtime.evaluate({ expression: 'window.location.toString()' });
const targets = await CDP.List(cdpPort);
Expand Down
15 changes: 14 additions & 1 deletion types/chrome-remote-interface/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ declare namespace CDP {
// Generated content end.
/////////////////////////////////////////////////

type GetEventFromString<D extends string, S extends string> = S extends `${D}.${infer E}` ? E : never;
type GetEvent<D extends string> = GetEventFromString<D, keyof ProtocolMappingApi.Events>;
type GetReturnType<D extends string, E extends string> =
`${D}.${E}` extends keyof ProtocolMappingApi.Events ?
ProtocolMappingApi.Events[`${D}.${E}`][0] : never;
type DoEventProps<D extends string> = {
[event in GetEvent<D>]: (listener: GetReturnType<D, event> extends undefined ?
(sessionId?: string) => void :
(params: GetReturnType<D, event>, sessionId?: string) => void)
=> () => Client};
type DoEventObj<D> = D extends string ? DoEventProps<D> : Record<keyof any, never>;

type IsNullableObj<T> = Record<keyof T, undefined> extends T ? true : false;
/**
* Checks whether the only parameter of `T[key]` is nullable i.e. all of
Expand All @@ -165,7 +177,8 @@ declare namespace CDP {
T[key] :
T[key]
};
type ImproveAPI<T> = {[key in keyof T]: OptIfParamNullable<T[key]>};

type ImproveAPI<T> = {[key in keyof T]: OptIfParamNullable<T[key]> & DoEventObj<key>};
interface StableDomains {
Browser: ProtocolProxyApi.BrowserApi;
Debugger: ProtocolProxyApi.DebuggerApi;
Expand Down