Skip to content

Commit 8930eaf

Browse files
fix: type page properly as Page
Removed the unnecessary type cast by typing `#page` properly as `Page`. Casts to `CdpPage` are now isolated to where `_client()` is required. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 33ddb8f commit 8930eaf

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

src/WaitForHelper.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {PredefinedNetworkConditions} from './third_party/index.js';
1010

1111
export class WaitForHelper {
1212
#abortController = new AbortController();
13-
#page: CdpPage;
13+
#page: Page;
1414
#stableDomTimeout: number;
1515
#stableDomFor: number;
1616
#expectNavigationIn: number;
@@ -25,7 +25,7 @@ export class WaitForHelper {
2525
this.#stableDomFor = 100 * cpuTimeoutMultiplier;
2626
this.#expectNavigationIn = 100 * cpuTimeoutMultiplier;
2727
this.#navigationTimeout = 3000 * networkTimeoutMultiplier;
28-
this.#page = page as unknown as CdpPage;
28+
this.#page = page;
2929
}
3030

3131
/**
@@ -101,10 +101,11 @@ export class WaitForHelper {
101101
resolve(true);
102102
};
103103

104-
this.#page._client().on('Page.frameStartedNavigating', listener);
104+
const client = (this.#page as unknown as CdpPage)._client();
105+
client.on('Page.frameStartedNavigating', listener);
105106
this.#abortController.signal.addEventListener('abort', () => {
106107
resolve(false);
107-
this.#page._client().off('Page.frameStartedNavigating', listener);
108+
client.off('Page.frameStartedNavigating', listener);
108109
});
109110
});
110111

@@ -128,16 +129,18 @@ export class WaitForHelper {
128129
action: () => Promise<unknown>,
129130
options?: {timeout?: number; dialog?: 'accept' | 'dismiss'},
130131
): Promise<void> {
131-
let dialogHandler: ((dialog: Dialog) => void) | undefined;
132132
if (options?.dialog) {
133-
dialogHandler = (dialog: Dialog) => {
133+
const dialogHandler = (dialog: Dialog) => {
134134
if (options.dialog === 'dismiss') {
135135
void dialog.dismiss();
136136
} else {
137137
void dialog.accept();
138138
}
139139
};
140-
(this.#page as unknown as Page).on('dialog', dialogHandler);
140+
this.#page.on('dialog', dialogHandler);
141+
this.#abortController.signal.addEventListener('abort', () => {
142+
this.#page.off('dialog', dialogHandler);
143+
});
141144
}
142145

143146
const navigationFinished = this.waitForNavigationStarted()
@@ -170,9 +173,6 @@ export class WaitForHelper {
170173
logger(error);
171174
} finally {
172175
this.#abortController.abort();
173-
if (options?.dialog && dialogHandler) {
174-
(this.#page as unknown as Page).off('dialog', dialogHandler);
175-
}
176176
}
177177
}
178178
}

0 commit comments

Comments
 (0)