cache invalidation

This commit is contained in:
siikamiika 2020-04-18 22:26:11 +03:00
parent a81c33b60a
commit d66ca93ce4
2 changed files with 27 additions and 4 deletions

View File

@ -22,6 +22,8 @@
class FrameOffsetForwarder { class FrameOffsetForwarder {
constructor() { constructor() {
this._started = false; this._started = false;
this._cacheMaxSize = 1000;
this._frameCache = new Set(); this._frameCache = new Set();
this._unreachableContentWindowCache = new Set(); this._unreachableContentWindowCache = new Set();
@ -81,7 +83,7 @@ class FrameOffsetForwarder {
} }
if (sourceFrame === null) { if (sourceFrame === null) {
// closed shadow root etc. // closed shadow root etc.
this._unreachableContentWindowCache.add(e.source); this._addToCache(this._unreachableContentWindowCache, e.source);
this._forwardFrameOffsetOrigin(null, uniqueId); this._forwardFrameOffsetOrigin(null, uniqueId);
return; return;
} }
@ -99,7 +101,7 @@ class FrameOffsetForwarder {
while (elements.length > 0) { while (elements.length > 0) {
const element = elements.shift(); const element = elements.shift();
if (element.contentWindow === contentWindow) { if (element.contentWindow === contentWindow) {
this._frameCache.add(element); this._addToCache(this._frameCache, element);
return element; return element;
} }
@ -124,12 +126,33 @@ class FrameOffsetForwarder {
} }
*_getFrameElementSources() { *_getFrameElementSources() {
yield [...this._frameCache]; const frameCache = [];
for (const frame of this._frameCache) {
// removed from DOM
if (!frame.isConnected) {
this._frameCache.delete(frame);
continue;
}
frameCache.push(frame);
}
yield frameCache;
// will contain duplicates, but frame elements are cheap to handle // will contain duplicates, but frame elements are cheap to handle
yield [...document.querySelectorAll('frame, iframe:not(.yomichan-float)')]; yield [...document.querySelectorAll('frame, iframe:not(.yomichan-float)')];
yield [document.documentElement]; yield [document.documentElement];
} }
_addToCache(cache, value) {
let freeSlots = this._cacheMaxSize - cache.size;
if (freeSlots <= 0) {
for (const cachedValue of cache) {
cache.delete(cachedValue);
++freeSlots;
if (freeSlots > 0) { break; }
}
}
cache.add(value);
}
_forwardFrameOffsetParent(offset, uniqueId) { _forwardFrameOffsetParent(offset, uniqueId) {
window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*'); window.parent.postMessage({action: 'getFrameOffset', params: {offset, uniqueId}}, '*');
} }

View File

@ -71,7 +71,7 @@
"applications": { "applications": {
"gecko": { "gecko": {
"id": "alex@foosoft.net", "id": "alex@foosoft.net",
"strict_min_version": "52.0" "strict_min_version": "53.0"
} }
} }
} }