Merge pull request #11169 from Snuffleupagus/Dict-inline-Ref-checks

Reduce the number of function calls in the `Dict` class
This commit is contained in:
Tim van der Meij 2019-09-24 23:33:37 +02:00 committed by GitHub
commit cd909c531f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,59 +81,47 @@ var Dict = (function DictClosure() {
}, },
// automatically dereferences Ref objects // automatically dereferences Ref objects
get: function Dict_get(key1, key2, key3) { get(key1, key2, key3) {
var value; let value = this._map[key1];
var xref = this.xref, suppressEncryption = this.suppressEncryption; if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
if (typeof (value = this._map[key1]) !== 'undefined' || value = this._map[key2];
key1 in this._map || typeof key2 === 'undefined') { if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
return xref ? xref.fetchIfRef(value, suppressEncryption) : value; value = this._map[key3];
}
} }
if (typeof (value = this._map[key2]) !== 'undefined' || if (value instanceof Ref && this.xref) {
key2 in this._map || typeof key3 === 'undefined') { return this.xref.fetch(value, this.suppressEncryption);
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
} }
value = this._map[key3]; return value;
return xref ? xref.fetchIfRef(value, suppressEncryption) : value;
}, },
// Same as get(), but returns a promise and uses fetchIfRefAsync(). // Same as get(), but returns a promise and uses fetchIfRefAsync().
getAsync: function Dict_getAsync(key1, key2, key3) { async getAsync(key1, key2, key3) {
var value; let value = this._map[key1];
var xref = this.xref, suppressEncryption = this.suppressEncryption; if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
if (typeof (value = this._map[key1]) !== 'undefined' || value = this._map[key2];
key1 in this._map || typeof key2 === 'undefined') { if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
if (xref) { value = this._map[key3];
return xref.fetchIfRefAsync(value, suppressEncryption);
} }
return Promise.resolve(value);
} }
if (typeof (value = this._map[key2]) !== 'undefined' || if (value instanceof Ref && this.xref) {
key2 in this._map || typeof key3 === 'undefined') { return this.xref.fetchAsync(value, this.suppressEncryption);
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
}
return Promise.resolve(value);
} }
value = this._map[key3]; return value;
if (xref) {
return xref.fetchIfRefAsync(value, suppressEncryption);
}
return Promise.resolve(value);
}, },
// Same as get(), but dereferences all elements if the result is an Array. // Same as get(), but dereferences all elements if the result is an Array.
getArray: function Dict_getArray(key1, key2, key3) { getArray(key1, key2, key3) {
var value = this.get(key1, key2, key3); let value = this.get(key1, key2, key3);
var xref = this.xref, suppressEncryption = this.suppressEncryption; if (!Array.isArray(value) || !this.xref) {
if (!Array.isArray(value) || !xref) {
return value; return value;
} }
value = value.slice(); // Ensure that we don't modify the Dict data. value = value.slice(); // Ensure that we don't modify the Dict data.
for (var i = 0, ii = value.length; i < ii; i++) { for (let i = 0, ii = value.length; i < ii; i++) {
if (!isRef(value[i])) { if (!(value[i] instanceof Ref)) {
continue; continue;
} }
value[i] = xref.fetch(value[i], suppressEncryption); value[i] = this.xref.fetch(value[i], this.suppressEncryption);
} }
return value; return value;
}, },