Improve caching of Annotations-data, by using a Map, in the API

Rather than caching only the *last* `PDFPageProxy.getAnnotations` call, and having to handle the intent separately, we can instead implement the caching in exactly the same way as done in the `PDFPageProxy.{render, getOperatorList}` methods.
This commit is contained in:
Jonas Jenwald 2021-08-08 08:14:51 +02:00
parent 036b81496e
commit 7f2d524df5

View File

@ -1234,6 +1234,7 @@ class PDFPageProxy {
this.cleanupAfterRender = false; this.cleanupAfterRender = false;
this.pendingCleanup = false; this.pendingCleanup = false;
this._intentStates = new Map(); this._intentStates = new Map();
this._annotationPromises = new Map();
this.destroyed = false; this.destroyed = false;
} }
@ -1303,17 +1304,15 @@ class PDFPageProxy {
getAnnotations({ intent = "display" } = {}) { getAnnotations({ intent = "display" } = {}) {
const renderingIntent = getRenderingIntent(intent, {}); const renderingIntent = getRenderingIntent(intent, {});
if ( let promise = this._annotationPromises.get(renderingIntent);
!this._annotationsPromise || if (!promise) {
this._annotationsIntent !== renderingIntent promise = this._transport.getAnnotations(
) {
this._annotationsPromise = this._transport.getAnnotations(
this._pageIndex, this._pageIndex,
renderingIntent renderingIntent
); );
this._annotationsIntent = renderingIntent; this._annotationPromises.set(renderingIntent, promise);
} }
return this._annotationsPromise; return promise;
} }
/** /**
@ -1623,7 +1622,7 @@ class PDFPageProxy {
} }
} }
this.objs.clear(); this.objs.clear();
this._annotationsPromise = null; this._annotationPromises.clear();
this._jsActionsPromise = null; this._jsActionsPromise = null;
this._structTreePromise = null; this._structTreePromise = null;
this.pendingCleanup = false; this.pendingCleanup = false;
@ -1658,7 +1657,7 @@ class PDFPageProxy {
this._intentStates.clear(); this._intentStates.clear();
this.objs.clear(); this.objs.clear();
this._annotationsPromise = null; this._annotationPromises.clear();
this._jsActionsPromise = null; this._jsActionsPromise = null;
this._structTreePromise = null; this._structTreePromise = null;
if (resetStats && this._stats) { if (resetStats && this._stats) {