diff --git a/src/webkit/wkPage.ts b/src/webkit/wkPage.ts index a413697190..261d4463b5 100644 --- a/src/webkit/wkPage.ts +++ b/src/webkit/wkPage.ts @@ -19,7 +19,7 @@ import * as frames from '../frames'; import { debugError, helper, RegisteredListener } from '../helper'; import * as dom from '../dom'; import * as network from '../network'; -import { WKTargetSession } from './wkConnection'; +import { WKTargetSession, WKTargetSessionEvents } from './wkConnection'; import { Events } from '../events'; import { WKExecutionContext, EVALUATION_SCRIPT_URL } from './wkExecutionContext'; import { WKNetworkManager } from './wkNetworkManager'; @@ -133,7 +133,8 @@ export class WKPage implements PageDelegate { helper.addEventListener(this._session, 'Runtime.executionContextCreated', event => this._onExecutionContextCreated(event.context)), helper.addEventListener(this._session, 'Console.messageAdded', event => this._onConsoleMessage(event)), helper.addEventListener(this._session, 'Dialog.javascriptDialogOpening', event => this._onDialog(event)), - helper.addEventListener(this._session, 'Page.fileChooserOpened', event => this._onFileChooserOpened(event)) + helper.addEventListener(this._session, 'Page.fileChooserOpened', event => this._onFileChooserOpened(event)), + helper.addEventListener(this._session, WKTargetSessionEvents.Disconnected, event => this._page._didDisconnect()), ]; } diff --git a/src/webkit/wkTarget.ts b/src/webkit/wkTarget.ts index 82ec88bdb2..d0e5772bc2 100644 --- a/src/webkit/wkTarget.ts +++ b/src/webkit/wkTarget.ts @@ -64,29 +64,17 @@ export class WKTarget { // old target does not close the page on connection reset. oldTarget._pagePromise = null; oldTarget._wkPage = null; - this._adoptPage(); - } - - private _adoptPage() { - this._session.once(WKTargetSessionEvents.Disconnected, () => { - // Once swapped out, we reset _page and won't call _didDisconnect for old session. - if (this._wkPage) - this._wkPage._page._didDisconnect(); - }); this._wkPage.setSession(this._session); } async page(): Promise { if (!this._pagePromise) { this._wkPage = new WKPage(this._browser, this._browserContext); + this._wkPage.setSession(this._session); // Reference local page variable as |this._frameManager| may be // cleared on swap. const page = this._wkPage._page; - this._pagePromise = new Promise(async f => { - this._adoptPage(); - await this._initializeSession(this._session); - f(page); - }); + this._pagePromise = this._initializeSession(this._session).then(() => page); } return this._pagePromise; }