Skip to content

Commit 3db8b23

Browse files
authored
fix(chromium): lifecycle events race (microsoft#4369)
1 parent c83ac44 commit 3db8b23

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/server/chromium/crPage.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ class FrameSession {
384384
const { windowId } = await this._client.send('Browser.getWindowForTarget');
385385
this._windowId = windowId;
386386
}
387-
let lifecycleEventsEnabled: Promise<any>;
387+
388+
let isInitialLifecycle = true;
388389
if (!this._isMainFrame())
389390
this._addSessionListeners();
390391
const promises: Promise<any>[] = [
@@ -406,20 +407,22 @@ class FrameSession {
406407
frame._evaluateExpression(binding.source, false, {}).catch(e => {});
407408
}
408409
const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':';
409-
if (isInitialEmptyPage) {
410+
if (!isInitialEmptyPage)
411+
this._firstNonInitialNavigationCommittedFulfill();
412+
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', event => {
410413
// Ignore lifecycle events for the initial empty page. It is never the final page
411414
// hence we are going to get more lifecycle updates after the actual navigation has
412415
// started (even if the target url is about:blank).
413-
lifecycleEventsEnabled.then(() => {
414-
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));
415-
});
416-
} else {
417-
this._firstNonInitialNavigationCommittedFulfill();
418-
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));
419-
}
416+
// Note: isInitialLifecycle is reset after the Page.setLifecycleEventsEnabled response.
417+
const ignoreLifecycle = isInitialLifecycle && isInitialEmptyPage;
418+
if (!ignoreLifecycle)
419+
this._onLifecycleEvent(event);
420+
}));
420421
}),
421422
this._client.send('Log.enable', {}),
422-
lifecycleEventsEnabled = this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),
423+
this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }).then(() => {
424+
isInitialLifecycle = true;
425+
}),
423426
this._client.send('Runtime.enable', {}),
424427
this._client.send('Page.addScriptToEvaluateOnNewDocument', {
425428
source: sourceMap.generateSourceUrl(),

0 commit comments

Comments
 (0)