Convert PDFObjects, in src/display/api.js, to an ES6 class

Also changes all occurrences of `var` to `const`, and marks internal properties/methods as "private".
This commit is contained in:
Jonas Jenwald 2018-11-07 14:17:14 +01:00
parent 3e342554d1
commit d32321d84f

View File

@ -2205,108 +2205,92 @@ class WorkerTransport {
} }
/** /**
* A PDF document and page is built of many objects. E.g. there are objects * A PDF document and page is built of many objects. E.g. there are objects for
* for fonts, images, rendering code and such. These objects might get processed * fonts, images, rendering code, etc. These objects may get processed inside of
* inside of a worker. The `PDFObjects` implements some basic functions to * a worker. This class implements some basic methods to manage these objects.
* manage these objects.
* @ignore * @ignore
*/ */
var PDFObjects = (function PDFObjectsClosure() { class PDFObjects {
function PDFObjects() { constructor() {
this.objs = Object.create(null); this._objs = Object.create(null);
} }
PDFObjects.prototype = {
/** /**
* Internal function.
* Ensures there is an object defined for `objId`. * Ensures there is an object defined for `objId`.
* @private
*/ */
ensureObj: function PDFObjects_ensureObj(objId) { _ensureObj(objId) {
if (this.objs[objId]) { if (this._objs[objId]) {
return this.objs[objId]; return this._objs[objId];
} }
return this._objs[objId] = {
var obj = {
capability: createPromiseCapability(), capability: createPromiseCapability(),
data: null, data: null,
resolved: false, resolved: false,
}; };
this.objs[objId] = obj; }
return obj;
},
/** /**
* If called *without* callback, this returns the data of `objId` but the * If called *without* callback, this returns the data of `objId` but the
* object needs to be resolved. If it isn't, this function throws. * object needs to be resolved. If it isn't, this method throws.
* *
* If called *with* a callback, the callback is called with the data of the * If called *with* a callback, the callback is called with the data of the
* object once the object is resolved. That means, if you call this * object once the object is resolved. That means, if you call this method
* function and the object is already resolved, the callback gets called * and the object is already resolved, the callback gets called right away.
* right away.
*/ */
get: function PDFObjects_get(objId, callback) { get(objId, callback = null) {
// If there is a callback, then the get can be async and the object is // If there is a callback, then the get can be async and the object is
// not required to be resolved right now // not required to be resolved right now.
if (callback) { if (callback) {
this.ensureObj(objId).capability.promise.then(callback); this._ensureObj(objId).capability.promise.then(callback);
return null; return null;
} }
// If there isn't a callback, the user expects to get the resolved data // If there isn't a callback, the user expects to get the resolved data
// directly. // directly.
var obj = this.objs[objId]; const obj = this._objs[objId];
// If there isn't an object yet or the object isn't resolved, then the // If there isn't an object yet or the object isn't resolved, then the
// data isn't ready yet! // data isn't ready yet!
if (!obj || !obj.resolved) { if (!obj || !obj.resolved) {
throw new Error(`Requesting object that isn't resolved yet ${objId}`); throw new Error(`Requesting object that isn't resolved yet ${objId}.`);
} }
return obj.data; return obj.data;
}, }
/** /**
* Resolves the object `objId` with optional `data`. * Resolves the object `objId` with optional `data`.
*/ */
resolve: function PDFObjects_resolve(objId, data) { resolve(objId, data) {
var obj = this.ensureObj(objId); const obj = this._ensureObj(objId);
obj.resolved = true; obj.resolved = true;
obj.data = data; obj.data = data;
obj.capability.resolve(data); obj.capability.resolve(data);
},
isResolved: function PDFObjects_isResolved(objId) {
var objs = this.objs;
if (!objs[objId]) {
return false;
} }
return objs[objId].resolved;
},
hasData: function PDFObjects_hasData(objId) { isResolved(objId) {
const obj = this._objs[objId];
return (obj ? obj.resolved : false);
}
hasData(objId) {
return this.isResolved(objId); return this.isResolved(objId);
}, }
/** /**
* Returns the data of `objId` if object exists, null otherwise. * Returns the data of `objId` if the object exists, null otherwise.
*/ */
getData: function PDFObjects_getData(objId) { getData(objId) {
var objs = this.objs; const obj = this._objs[objId];
if (!objs[objId] || !objs[objId].resolved) { if (!obj || !obj.resolved) {
return null; return null;
} }
return objs[objId].data; return obj.data;
}, }
clear: function PDFObjects_clear() { clear() {
this.objs = Object.create(null); this._objs = Object.create(null);
}, }
}; }
return PDFObjects;
})();
/** /**
* Allows controlling of the rendering tasks. * Allows controlling of the rendering tasks.