From 7f2d524df58934a42d1b87c9a13860db8c068c30 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 8 Aug 2021 08:14:51 +0200 Subject: [PATCH] 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. --- src/display/api.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index cee9cf640..978b05802 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1234,6 +1234,7 @@ class PDFPageProxy { this.cleanupAfterRender = false; this.pendingCleanup = false; this._intentStates = new Map(); + this._annotationPromises = new Map(); this.destroyed = false; } @@ -1303,17 +1304,15 @@ class PDFPageProxy { getAnnotations({ intent = "display" } = {}) { const renderingIntent = getRenderingIntent(intent, {}); - if ( - !this._annotationsPromise || - this._annotationsIntent !== renderingIntent - ) { - this._annotationsPromise = this._transport.getAnnotations( + let promise = this._annotationPromises.get(renderingIntent); + if (!promise) { + promise = this._transport.getAnnotations( this._pageIndex, renderingIntent ); - this._annotationsIntent = renderingIntent; + this._annotationPromises.set(renderingIntent, promise); } - return this._annotationsPromise; + return promise; } /** @@ -1623,7 +1622,7 @@ class PDFPageProxy { } } this.objs.clear(); - this._annotationsPromise = null; + this._annotationPromises.clear(); this._jsActionsPromise = null; this._structTreePromise = null; this.pendingCleanup = false; @@ -1658,7 +1657,7 @@ class PDFPageProxy { this._intentStates.clear(); this.objs.clear(); - this._annotationsPromise = null; + this._annotationPromises.clear(); this._jsActionsPromise = null; this._structTreePromise = null; if (resetStats && this._stats) {