mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-08-02 04:17:24 +02:00
Merge pull request #20944 from Snuffleupagus/PDFObjects-resolve-once
Avoid resolving an `objId` more than once in the `PDFObjects` class
This commit is contained in:
commit
1aa95d28d0
@ -28,16 +28,6 @@ const dataObj = () => ({
|
|||||||
class PDFObjects {
|
class PDFObjects {
|
||||||
#objs = new Map();
|
#objs = new Map();
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensures there is an object defined for `objId`.
|
|
||||||
*
|
|
||||||
* @param {string} objId
|
|
||||||
* @returns {Object}
|
|
||||||
*/
|
|
||||||
#ensureObj(objId) {
|
|
||||||
return this.#objs.getOrInsertComputed(objId, dataObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 method throws.
|
* object needs to be resolved. If it isn't, this method throws.
|
||||||
@ -54,7 +44,7 @@ class PDFObjects {
|
|||||||
// 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) {
|
||||||
const obj = this.#ensureObj(objId);
|
const obj = this.#objs.getOrInsertComputed(objId, dataObj);
|
||||||
obj.promise.then(() => callback(obj.data));
|
obj.promise.then(() => callback(obj.data));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -99,7 +89,10 @@ class PDFObjects {
|
|||||||
* @param {any} [data]
|
* @param {any} [data]
|
||||||
*/
|
*/
|
||||||
resolve(objId, data = null) {
|
resolve(objId, data = null) {
|
||||||
const obj = this.#ensureObj(objId);
|
const obj = this.#objs.getOrInsertComputed(objId, dataObj);
|
||||||
|
if (obj.data !== INITIAL_DATA) {
|
||||||
|
throw new Error(`Object already resolved ${objId}.`);
|
||||||
|
}
|
||||||
obj.data = data;
|
obj.data = data;
|
||||||
obj.resolve();
|
obj.resolve();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user