aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js/popup-proxy.js
diff options
context:
space:
mode:
authorsiikamiika <siikamiika@users.noreply.github.com>2020-03-22 14:11:43 +0200
committersiikamiika <siikamiika@users.noreply.github.com>2020-04-05 19:57:37 +0300
commit9f9ea1d5fb9f383992523a2a9a8a02259ced838e (patch)
treef28de9a694be800cae06bfd5a1f7dcc3f888aa19 /ext/fg/js/popup-proxy.js
parentfa65ab351293fa6af882479e0fad6aaca3152569 (diff)
throttle frame offset getting
Diffstat (limited to 'ext/fg/js/popup-proxy.js')
-rw-r--r--ext/fg/js/popup-proxy.js46
1 files changed, 40 insertions, 6 deletions
diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js
index a25f9183..0cd5bbad 100644
--- a/ext/fg/js/popup-proxy.js
+++ b/ext/fg/js/popup-proxy.js
@@ -21,14 +21,18 @@
*/
class PopupProxy {
- constructor(id, depth, parentId, parentFrameId, url, applyFrameOffset=null) {
+ constructor(id, depth, parentId, parentFrameId, url, getFrameOffset=null) {
this._parentId = parentId;
this._parentFrameId = parentFrameId;
this._id = id;
this._depth = depth;
this._url = url;
this._apiSender = new FrontendApiSender();
- this._applyFrameOffset = applyFrameOffset;
+ this._getFrameOffset = getFrameOffset;
+
+ this._frameOffset = null;
+ this._frameOffsetPromise = null;
+ this._frameOffsetUpdatedAt = null;
}
// Public properties
@@ -81,16 +85,18 @@ class PopupProxy {
}
async containsPoint(x, y) {
- if (this._applyFrameOffset !== null) {
- [x, y] = await this._applyFrameOffset(x, y);
+ if (this._getFrameOffset !== null) {
+ await this._updateFrameOffset();
+ [x, y] = this._applyFrameOffset(x, y);
}
return await this._invokeHostApi('containsPoint', {id: this._id, x, y});
}
async showContent(elementRect, writingMode, type=null, details=null) {
let {x, y, width, height} = elementRect;
- if (this._applyFrameOffset !== null) {
- [x, y] = await this._applyFrameOffset(x, y);
+ if (this._getFrameOffset !== null) {
+ await this._updateFrameOffset();
+ [x, y] = this._applyFrameOffset(x, y);
}
elementRect = {x, y, width, height};
return await this._invokeHostApi('showContent', {id: this._id, elementRect, writingMode, type, details});
@@ -116,4 +122,32 @@ class PopupProxy {
}
return this._apiSender.invoke(action, params, `popup-proxy-host#${this._parentFrameId}`);
}
+
+ async _updateFrameOffset() {
+ const firstRun = this._frameOffsetUpdatedAt === null;
+ const expired = firstRun || this._frameOffsetUpdatedAt < Date.now() - 1000;
+ if (this._frameOffsetPromise === null && !expired) { return; }
+
+ if (this._frameOffsetPromise !== null) {
+ await this._frameOffsetPromise;
+ return;
+ }
+
+ if (firstRun) {
+ this._frameOffsetPromise = this._getFrameOffset();
+ this._frameOffset = await this._frameOffsetPromise;
+ this._frameOffsetPromise = null;
+ this._frameOffsetUpdatedAt = Date.now();
+ } else {
+ this._getFrameOffset().then((offset) => {
+ this._frameOffset = offset;
+ this._frameOffsetUpdatedAt = Date.now();
+ });
+ }
+ }
+
+ _applyFrameOffset(x, y) {
+ const [offsetX, offsetY] = this._frameOffset;
+ return [x + offsetX, y + offsetY];
+ }
}