Skip to content

Commit

Permalink
Convert PDFObjects, in src/display/api.js, to an ES6 class
Browse files Browse the repository at this point in the history
Also changes all occurrences of `var` to `const`, and marks internal properties/methods as "private".
  • Loading branch information
Snuffleupagus committed Nov 8, 2018
1 parent 3e34255 commit d32321d
Showing 1 changed file with 75 additions and 91 deletions.
166 changes: 75 additions & 91 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2205,108 +2205,92 @@ class WorkerTransport {
}

/**
* A PDF document and page is built of many objects. E.g. there are objects
* for fonts, images, rendering code and such. These objects might get processed
* inside of a worker. The `PDFObjects` implements some basic functions to
* manage these objects.
* A PDF document and page is built of many objects. E.g. there are objects for
* fonts, images, rendering code, etc. These objects may get processed inside of
* a worker. This class implements some basic methods to manage these objects.
* @ignore
*/
var PDFObjects = (function PDFObjectsClosure() {
function PDFObjects() {
this.objs = Object.create(null);
class PDFObjects {
constructor() {
this._objs = Object.create(null);
}

PDFObjects.prototype = {
/**
* Internal function.
* Ensures there is an object defined for `objId`.
*/
ensureObj: function PDFObjects_ensureObj(objId) {
if (this.objs[objId]) {
return this.objs[objId];
}

var obj = {
capability: createPromiseCapability(),
data: null,
resolved: false,
};
this.objs[objId] = obj;

return obj;
},

/**
* If called *without* callback, this returns the data of `objId` but the
* object needs to be resolved. If it isn't, this function throws.
*
* 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
* function and the object is already resolved, the callback gets called
* right away.
*/
get: function PDFObjects_get(objId, callback) {
// If there is a callback, then the get can be async and the object is
// not required to be resolved right now
if (callback) {
this.ensureObj(objId).capability.promise.then(callback);
return null;
}

// If there isn't a callback, the user expects to get the resolved data
// directly.
var obj = this.objs[objId];

// If there isn't an object yet or the object isn't resolved, then the
// data isn't ready yet!
if (!obj || !obj.resolved) {
throw new Error(`Requesting object that isn't resolved yet ${objId}`);
}

return obj.data;
},
/**
* Ensures there is an object defined for `objId`.
* @private
*/
_ensureObj(objId) {
if (this._objs[objId]) {
return this._objs[objId];
}
return this._objs[objId] = {
capability: createPromiseCapability(),
data: null,
resolved: false,
};
}

/**
* Resolves the object `objId` with optional `data`.
*/
resolve: function PDFObjects_resolve(objId, data) {
var obj = this.ensureObj(objId);
/**
* If called *without* callback, this returns the data of `objId` but the
* 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
* object once the object is resolved. That means, if you call this method
* and the object is already resolved, the callback gets called right away.
*/
get(objId, callback = null) {
// If there is a callback, then the get can be async and the object is
// not required to be resolved right now.
if (callback) {
this._ensureObj(objId).capability.promise.then(callback);
return null;
}
// If there isn't a callback, the user expects to get the resolved data
// directly.
const obj = this._objs[objId];
// If there isn't an object yet or the object isn't resolved, then the
// data isn't ready yet!
if (!obj || !obj.resolved) {
throw new Error(`Requesting object that isn't resolved yet ${objId}.`);
}
return obj.data;
}

obj.resolved = true;
obj.data = data;
obj.capability.resolve(data);
},
/**
* Resolves the object `objId` with optional `data`.
*/
resolve(objId, data) {
const obj = this._ensureObj(objId);

isResolved: function PDFObjects_isResolved(objId) {
var objs = this.objs;
obj.resolved = true;
obj.data = data;
obj.capability.resolve(data);
}

if (!objs[objId]) {
return false;
}
return objs[objId].resolved;
},
isResolved(objId) {
const obj = this._objs[objId];
return (obj ? obj.resolved : false);
}

hasData: function PDFObjects_hasData(objId) {
return this.isResolved(objId);
},
hasData(objId) {
return this.isResolved(objId);
}

/**
* Returns the data of `objId` if object exists, null otherwise.
*/
getData: function PDFObjects_getData(objId) {
var objs = this.objs;
if (!objs[objId] || !objs[objId].resolved) {
return null;
}
return objs[objId].data;
},
/**
* Returns the data of `objId` if the object exists, null otherwise.
*/
getData(objId) {
const obj = this._objs[objId];
if (!obj || !obj.resolved) {
return null;
}
return obj.data;
}

clear: function PDFObjects_clear() {
this.objs = Object.create(null);
},
};
return PDFObjects;
})();
clear() {
this._objs = Object.create(null);
}
}

/**
* Allows controlling of the rendering tasks.
Expand Down

0 comments on commit d32321d

Please sign in to comment.