mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-01 16:49:39 +02:00
code review
This commit is contained in:
parent
2518a3d17c
commit
e487f0f500
@ -1870,56 +1870,46 @@ var httpObserver = {
|
|||||||
// +-------+-------+-------+-------+-------+-------+-------
|
// +-------+-------+-------+-------+-------+-------+-------
|
||||||
//
|
//
|
||||||
// URL to ring buffer index map:
|
// URL to ring buffer index map:
|
||||||
// { k = URL, v = ring buffer index }
|
// { k = URL, s = ring buffer indices }
|
||||||
//
|
//
|
||||||
// v can an integer or an Array of integer (for when the same URL is
|
// s is a string which character codes map to ring buffer indices -- for
|
||||||
// received multiple times by shouldLoadListener() before the existing one
|
// when the same URL is received multiple times by shouldLoadListener()
|
||||||
// is serviced by the network request observer.
|
// before the existing one is serviced by the network request observer.
|
||||||
|
// I believe the use of a string in lieu of an array reduces memory
|
||||||
|
// churning.
|
||||||
|
|
||||||
createPendingRequest: function(url) {
|
createPendingRequest: function(url) {
|
||||||
var bucket;
|
var bucket;
|
||||||
var i = this.pendingWritePointer;
|
var i = this.pendingWritePointer;
|
||||||
this.pendingWritePointer = i + 1 & 31;
|
this.pendingWritePointer = i + 1 & 31;
|
||||||
var preq = this.pendingRingBuffer[i];
|
var preq = this.pendingRingBuffer[i];
|
||||||
|
var si = String.fromCharCode(i);
|
||||||
// Cleanup unserviced pending request
|
// Cleanup unserviced pending request
|
||||||
if ( preq._key !== '' ) {
|
if ( preq._key !== '' ) {
|
||||||
bucket = this.pendingURLToIndex.get(preq._key);
|
bucket = this.pendingURLToIndex.get(preq._key);
|
||||||
if ( Array.isArray(bucket) ) {
|
if ( bucket.length === 1 ) {
|
||||||
var pos = bucket.indexOf(i);
|
|
||||||
bucket.splice(pos, 1);
|
|
||||||
if ( bucket.length === 1 ) {
|
|
||||||
this.pendingURLToIndex.set(preq._key, bucket[0]);
|
|
||||||
}
|
|
||||||
} else if ( typeof bucket === 'number' ) {
|
|
||||||
this.pendingURLToIndex.delete(preq._key);
|
this.pendingURLToIndex.delete(preq._key);
|
||||||
|
} else {
|
||||||
|
var pos = bucket.indexOf(si);
|
||||||
|
this.pendingURLToIndex.set(preq._key, bucket.slice(0, pos) + bucket.slice(pos + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Would be much simpler if a url could not appear more than once.
|
|
||||||
bucket = this.pendingURLToIndex.get(url);
|
bucket = this.pendingURLToIndex.get(url);
|
||||||
if ( bucket === undefined ) {
|
this.pendingURLToIndex.set(url, bucket === undefined ? si : bucket + si);
|
||||||
this.pendingURLToIndex.set(url, i);
|
|
||||||
} else if ( Array.isArray(bucket) ) {
|
|
||||||
bucket = bucket.push(i);
|
|
||||||
} else {
|
|
||||||
bucket = [bucket, i];
|
|
||||||
}
|
|
||||||
preq._key = url;
|
preq._key = url;
|
||||||
return preq;
|
return preq;
|
||||||
},
|
},
|
||||||
|
|
||||||
lookupPendingRequest: function(url) {
|
lookupPendingRequest: function(url) {
|
||||||
var i = this.pendingURLToIndex.get(url);
|
var bucket = this.pendingURLToIndex.get(url);
|
||||||
if ( i === undefined ) {
|
if ( bucket === undefined ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if ( Array.isArray(i) ) {
|
var i = bucket.charCodeAt(0);
|
||||||
var bucket = i;
|
if ( bucket.length === 1 ) {
|
||||||
i = bucket.shift();
|
|
||||||
if ( bucket.length === 1 ) {
|
|
||||||
this.pendingURLToIndex.set(url, bucket[0]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.pendingURLToIndex.delete(url);
|
this.pendingURLToIndex.delete(url);
|
||||||
|
} else {
|
||||||
|
this.pendingURLToIndex.set(url, bucket.slice(1));
|
||||||
}
|
}
|
||||||
var preq = this.pendingRingBuffer[i];
|
var preq = this.pendingRingBuffer[i];
|
||||||
preq._key = ''; // mark as "serviced"
|
preq._key = ''; // mark as "serviced"
|
||||||
@ -3151,11 +3141,17 @@ vAPI.contextMenu.register = (function() {
|
|||||||
// Be sure document.readyState is 'complete': it could happen at launch
|
// Be sure document.readyState is 'complete': it could happen at launch
|
||||||
// time that we are called by vAPI.contextMenu.create() directly before
|
// time that we are called by vAPI.contextMenu.create() directly before
|
||||||
// the environment is properly initialized.
|
// the environment is properly initialized.
|
||||||
var registerSafely = function(doc) {
|
var registerSafely = function(doc, tryCount) {
|
||||||
if ( doc.readyState !== 'complete' ) {
|
if ( doc.readyState === 'complete' ) {
|
||||||
vAPI.setTimeout(registerSafely.bind(this, doc), 200);
|
|
||||||
} else {
|
|
||||||
register.call(this, doc);
|
register.call(this, doc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( typeof tryCount !== 'number' ) {
|
||||||
|
tryCount = 0;
|
||||||
|
}
|
||||||
|
tryCount += 1;
|
||||||
|
if ( tryCount < 8 ) {
|
||||||
|
vAPI.setTimeout(registerSafely.bind(this, doc, tryCount), 200);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user