refactor _updateFrameOffset

This commit is contained in:
siikamiika 2020-04-05 19:21:06 +03:00
parent abd056e563
commit aea7c590d1

View File

@ -120,8 +120,9 @@ class PopupProxy {
} }
async _updateFrameOffset() { async _updateFrameOffset() {
const now = Date.now();
const firstRun = this._frameOffsetUpdatedAt === null; const firstRun = this._frameOffsetUpdatedAt === null;
const expired = firstRun || this._frameOffsetUpdatedAt < Date.now() - PopupProxy._frameOffsetExpireTimeout; const expired = firstRun || this._frameOffsetUpdatedAt < now - PopupProxy._frameOffsetExpireTimeout;
if (this._frameOffsetPromise === null && !expired) { return; } if (this._frameOffsetPromise === null && !expired) { return; }
if (this._frameOffsetPromise !== null) { if (this._frameOffsetPromise !== null) {
@ -131,27 +132,22 @@ class PopupProxy {
return; return;
} }
this._frameOffsetPromise = this._getFrameOffset(); const promise = this._updateFrameOffsetInner(now);
const handleOffset = (offset) => {
this._frameOffset = offset !== null ? offset : [0, 0];
this._frameOffsetUpdatedAt = Date.now();
this._frameOffsetPromise = null;
};
const handleError = (e) => {
console.error(e);
this._frameOffsetPromise = null;
};
if (firstRun) { if (firstRun) {
try { await promise;
handleOffset(await this._frameOffsetPromise);
} catch (e) {
handleError(e);
} }
} else { }
this._frameOffsetPromise.then(handleOffset, handleError);
async _updateFrameOffsetInner(now) {
this._frameOffsetPromise = this._getFrameOffset();
try {
const offset = await this._frameOffsetPromise;
this._frameOffset = offset !== null ? offset : [0, 0];
this._frameOffsetUpdatedAt = now;
} catch (e) {
logError(e);
} finally {
this._frameOffsetPromise = null;
} }
} }