simplify popup proxy prepare

This commit is contained in:
siikamiika 2020-03-19 15:55:12 +02:00
parent b996d0b1e0
commit 09151a1a86
2 changed files with 12 additions and 36 deletions

View File

@ -48,8 +48,10 @@ async function main() {
const {popupId, frameId} = await rootPopupInformationPromise; const {popupId, frameId} = await rootPopupInformationPromise;
popup = new PopupProxy(popupId, 0, null, frameId, url); popup = new PopupProxy(popupId, 0, null, frameId, url);
await popup.prepare();
} else if (proxy) { } else if (proxy) {
popup = new PopupProxy(null, depth + 1, id, parentFrameId, url); popup = new PopupProxy(null, depth + 1, id, parentFrameId, url);
await popup.prepare();
} else { } else {
const popupHost = new PopupProxyHost(); const popupHost = new PopupProxyHost();
await popupHost.prepare(); await popupHost.prepare();

View File

@ -25,7 +25,6 @@ class PopupProxy {
this._parentId = parentId; this._parentId = parentId;
this._parentFrameId = parentFrameId; this._parentFrameId = parentFrameId;
this._id = id; this._id = id;
this._idPromise = null;
this._depth = depth; this._depth = depth;
this._url = url; this._url = url;
this._apiSender = new FrontendApiSender(); this._apiSender = new FrontendApiSender();
@ -57,6 +56,11 @@ class PopupProxy {
// Public functions // Public functions
async prepare() {
const {id} = await this._invokeHostApi('getOrCreatePopup', {id: this._id, parentId: this._parentId});
this._id = id;
}
isProxy() { isProxy() {
return true; return true;
} }
@ -66,33 +70,22 @@ class PopupProxy {
} }
async setOptions(options) { async setOptions(options) {
const id = await this._getPopupId(); return await this._invokeHostApi('setOptions', {id: this._id, options});
return await this._invokeHostApi('setOptions', {id, options});
} }
hide(changeFocus) { hide(changeFocus) {
if (this._id === null) {
return;
}
this._invokeHostApi('hide', {id: this._id, changeFocus}); this._invokeHostApi('hide', {id: this._id, changeFocus});
} }
async isVisible() { async isVisible() {
const id = await this._getPopupId(); return await this._invokeHostApi('isVisible', {id: this._id});
return await this._invokeHostApi('isVisible', {id});
} }
setVisibleOverride(visible) { setVisibleOverride(visible) {
if (this._id === null) {
return;
}
this._invokeHostApi('setVisibleOverride', {id: this._id, visible}); this._invokeHostApi('setVisibleOverride', {id: this._id, visible});
} }
async containsPoint(x, y) { async containsPoint(x, y) {
if (this._id === null) {
return false;
}
if (this._depth === 0) { if (this._depth === 0) {
[x, y] = await PopupProxy._convertIframePointToRootPagePoint(x, y); [x, y] = await PopupProxy._convertIframePointToRootPagePoint(x, y);
} }
@ -100,30 +93,24 @@ class PopupProxy {
} }
async showContent(elementRect, writingMode, type=null, details=null) { async showContent(elementRect, writingMode, type=null, details=null) {
const id = await this._getPopupId();
let {x, y, width, height} = elementRect; let {x, y, width, height} = elementRect;
if (this._depth === 0) { if (this._depth === 0) {
[x, y] = await PopupProxy._convertIframePointToRootPagePoint(x, y); [x, y] = await PopupProxy._convertIframePointToRootPagePoint(x, y);
} }
elementRect = {x, y, width, height}; elementRect = {x, y, width, height};
return await this._invokeHostApi('showContent', {id, elementRect, writingMode, type, details}); return await this._invokeHostApi('showContent', {id: this._id, elementRect, writingMode, type, details});
} }
async setCustomCss(css) { async setCustomCss(css) {
const id = await this._getPopupId(); return await this._invokeHostApi('setCustomCss', {id: this._id, css});
return await this._invokeHostApi('setCustomCss', {id, css});
} }
clearAutoPlayTimer() { clearAutoPlayTimer() {
if (this._id === null) {
return;
}
this._invokeHostApi('clearAutoPlayTimer', {id: this._id}); this._invokeHostApi('clearAutoPlayTimer', {id: this._id});
} }
async setContentScale(scale) { async setContentScale(scale) {
const id = await this._getPopupId(); this._invokeHostApi('setContentScale', {id: this._id, scale});
this._invokeHostApi('setContentScale', {id, scale});
} }
// Window message handlers // Window message handlers
@ -153,19 +140,6 @@ class PopupProxy {
// Private // Private
_getPopupId() {
if (this._idPromise === null) {
this._idPromise = this._getPopupIdAsync();
}
return this._idPromise;
}
async _getPopupIdAsync() {
const {id} = await this._invokeHostApi('getOrCreatePopup', {id: this._id, parentId: this._parentId});
this._id = id;
return id;
}
_invokeHostApi(action, params={}) { _invokeHostApi(action, params={}) {
if (typeof this._parentFrameId !== 'number') { if (typeof this._parentFrameId !== 'number') {
return Promise.reject(new Error('Invalid frame')); return Promise.reject(new Error('Invalid frame'));