Skip to content

Commit

Permalink
fix(chromium): lifecycle events race (#4369)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Nov 6, 2020
1 parent c83ac44 commit 3db8b23
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/server/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ class FrameSession {
const { windowId } = await this._client.send('Browser.getWindowForTarget');
this._windowId = windowId;
}
let lifecycleEventsEnabled: Promise<any>;

let isInitialLifecycle = true;
if (!this._isMainFrame())
this._addSessionListeners();
const promises: Promise<any>[] = [
Expand All @@ -406,20 +407,22 @@ class FrameSession {
frame._evaluateExpression(binding.source, false, {}).catch(e => {});
}
const isInitialEmptyPage = this._isMainFrame() && this._page.mainFrame().url() === ':';
if (isInitialEmptyPage) {
if (!isInitialEmptyPage)
this._firstNonInitialNavigationCommittedFulfill();
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', event => {
// Ignore lifecycle events for the initial empty page. It is never the final page
// hence we are going to get more lifecycle updates after the actual navigation has
// started (even if the target url is about:blank).
lifecycleEventsEnabled.then(() => {
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));
});
} else {
this._firstNonInitialNavigationCommittedFulfill();
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', event => this._onLifecycleEvent(event)));
}
// Note: isInitialLifecycle is reset after the Page.setLifecycleEventsEnabled response.
const ignoreLifecycle = isInitialLifecycle && isInitialEmptyPage;
if (!ignoreLifecycle)
this._onLifecycleEvent(event);
}));
}),
this._client.send('Log.enable', {}),
lifecycleEventsEnabled = this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),
this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }).then(() => {
isInitialLifecycle = true;
}),
this._client.send('Runtime.enable', {}),
this._client.send('Page.addScriptToEvaluateOnNewDocument', {
source: sourceMap.generateSourceUrl(),
Expand Down

0 comments on commit 3db8b23

Please sign in to comment.