Skip to content

Commit 3635a9a

Browse files
Merge pull request #14585 from Snuffleupagus/PDFObjects-private
Improve the `PDFObjects` class
2 parents dce10de + bad1589 commit 3635a9a

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/display/api.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const DefaultStandardFontDataFactory =
9191
*/
9292

9393
/**
94-
* @type IPDFStreamFactory
94+
* @type {IPDFStreamFactory}
9595
* @private
9696
*/
9797
let createPDFNetworkStream;
@@ -1229,6 +1229,7 @@ class PDFPageProxy {
12291229
this._transport = transport;
12301230
this._stats = pdfBug ? new StatTimer() : null;
12311231
this._pdfBug = pdfBug;
1232+
/** @type {PDFObjects} */
12321233
this.commonObjs = transport.commonObjs;
12331234
this.objs = new PDFObjects();
12341235

@@ -3046,25 +3047,24 @@ class WorkerTransport {
30463047
* A PDF document and page is built of many objects. E.g. there are objects for
30473048
* fonts, images, rendering code, etc. These objects may get processed inside of
30483049
* a worker. This class implements some basic methods to manage these objects.
3049-
* @ignore
30503050
*/
30513051
class PDFObjects {
3052-
constructor() {
3053-
this._objs = Object.create(null);
3054-
}
3052+
#objs = Object.create(null);
30553053

30563054
/**
30573055
* Ensures there is an object defined for `objId`.
3058-
* @private
3056+
*
3057+
* @param {string} objId
3058+
* @returns {Object}
30593059
*/
3060-
_ensureObj(objId) {
3061-
if (this._objs[objId]) {
3062-
return this._objs[objId];
3060+
#ensureObj(objId) {
3061+
const obj = this.#objs[objId];
3062+
if (obj) {
3063+
return obj;
30633064
}
3064-
return (this._objs[objId] = {
3065+
return (this.#objs[objId] = {
30653066
capability: createPromiseCapability(),
30663067
data: null,
3067-
resolved: false,
30683068
});
30693069
}
30703070

@@ -3075,43 +3075,53 @@ class PDFObjects {
30753075
* If called *with* a callback, the callback is called with the data of the
30763076
* object once the object is resolved. That means, if you call this method
30773077
* and the object is already resolved, the callback gets called right away.
3078+
*
3079+
* @param {string} objId
3080+
* @param {function} [callback]
3081+
* @returns {any}
30783082
*/
30793083
get(objId, callback = null) {
30803084
// If there is a callback, then the get can be async and the object is
30813085
// not required to be resolved right now.
30823086
if (callback) {
3083-
this._ensureObj(objId).capability.promise.then(callback);
3087+
const obj = this.#ensureObj(objId);
3088+
obj.capability.promise.then(() => callback(obj.data));
30843089
return null;
30853090
}
30863091
// If there isn't a callback, the user expects to get the resolved data
30873092
// directly.
3088-
const obj = this._objs[objId];
3093+
const obj = this.#objs[objId];
30893094
// If there isn't an object yet or the object isn't resolved, then the
30903095
// data isn't ready yet!
3091-
if (!obj || !obj.resolved) {
3096+
if (!obj?.capability.settled) {
30923097
throw new Error(`Requesting object that isn't resolved yet ${objId}.`);
30933098
}
30943099
return obj.data;
30953100
}
30963101

3102+
/**
3103+
* @param {string} objId
3104+
* @returns {boolean}
3105+
*/
30973106
has(objId) {
3098-
const obj = this._objs[objId];
3099-
return obj?.resolved || false;
3107+
const obj = this.#objs[objId];
3108+
return obj?.capability.settled || false;
31003109
}
31013110

31023111
/**
31033112
* Resolves the object `objId` with optional `data`.
3113+
*
3114+
* @param {string} objId
3115+
* @param {any} [data]
31043116
*/
3105-
resolve(objId, data) {
3106-
const obj = this._ensureObj(objId);
3107-
3108-
obj.resolved = true;
3117+
resolve(objId, data = null) {
3118+
const obj = this.#ensureObj(objId);
31093119
obj.data = data;
3110-
obj.capability.resolve(data);
3120+
obj.capability.resolve();
31113121
}
31123122

31133123
clear() {
3114-
this._objs = Object.create(null);
3124+
this.#objs = Object.create(null);
31153125
}
31163126
}
31173127

0 commit comments

Comments
 (0)