Skip to content

Commit e9038cc

Browse files
committed
Send the AnnotationStorage-data to the worker-thread as a Map
Rather than converting the `AnnotationStorage`-data to an Object, before sending it to the worker-thread, we should be able to simply send the internal `Map` directly. The "structured clone algorithm" doesn't have a problem with `Map`s, however the `LoopbackPort` used when workers are *disabled* (e.g. in Node.js environments) didn't use to support them. With PR 12997 having lifted that restriction, we should now be able to simply send the `AnnotationStorage`-data as-is rather than having to iterate through it to first create an Object. *Please note:* The changes in `src/core/annotation.js` could have been a lot more compact if we were able to use optional chaining in the `src/core` folder. Unfortunately that's still not possible, since SystemJS is being used in the development viewer (i.g. `gulp server`) and fixing that is *still* blocked by [bug 1247687](https://bugzilla.mozilla.org/show_bug.cgi?id=1247687).
1 parent 0fa9976 commit e9038cc

File tree

4 files changed

+133
-89
lines changed

4 files changed

+133
-89
lines changed

src/core/annotation.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,10 @@ class Annotation {
348348
}
349349

350350
isHidden(annotationStorage) {
351-
const data = annotationStorage && annotationStorage[this.data.id];
352-
if (data && "hidden" in data) {
353-
return data.hidden;
351+
const storageEntry =
352+
annotationStorage && annotationStorage.get(this.data.id);
353+
if (storageEntry && storageEntry.hidden !== undefined) {
354+
return storageEntry.hidden;
354355
}
355356
return this._hasFlag(this.flags, AnnotationFlag.HIDDEN);
356357
}
@@ -1206,8 +1207,11 @@ class WidgetAnnotation extends Annotation {
12061207
}
12071208

12081209
async save(evaluator, task, annotationStorage) {
1209-
const value =
1210-
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
1210+
if (!annotationStorage) {
1211+
return null;
1212+
}
1213+
const storageEntry = annotationStorage.get(this.data.id);
1214+
const value = storageEntry && storageEntry.value;
12111215
if (value === this.data.fieldValue || value === undefined) {
12121216
return null;
12131217
}
@@ -1289,8 +1293,8 @@ class WidgetAnnotation extends Annotation {
12891293
if (!annotationStorage || isPassword) {
12901294
return null;
12911295
}
1292-
let value =
1293-
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
1296+
const storageEntry = annotationStorage.get(this.data.id);
1297+
let value = storageEntry && storageEntry.value;
12941298
if (value === undefined) {
12951299
// The annotation hasn't been rendered so use the appearance
12961300
return null;
@@ -1769,9 +1773,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
17691773
}
17701774

17711775
if (annotationStorage) {
1772-
const value =
1773-
annotationStorage[this.data.id] &&
1774-
annotationStorage[this.data.id].value;
1776+
const storageEntry = annotationStorage.get(this.data.id);
1777+
const value = storageEntry && storageEntry.value;
17751778
if (value === undefined) {
17761779
return super.getOperatorList(
17771780
evaluator,
@@ -1826,8 +1829,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
18261829
}
18271830

18281831
async _saveCheckbox(evaluator, task, annotationStorage) {
1829-
const value =
1830-
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
1832+
if (!annotationStorage) {
1833+
return null;
1834+
}
1835+
const storageEntry = annotationStorage.get(this.data.id);
1836+
const value = storageEntry && storageEntry.value;
18311837
if (value === undefined) {
18321838
return null;
18331839
}
@@ -1869,8 +1875,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
18691875
}
18701876

18711877
async _saveRadioButton(evaluator, task, annotationStorage) {
1872-
const value =
1873-
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
1878+
if (!annotationStorage) {
1879+
return null;
1880+
}
1881+
const storageEntry = annotationStorage.get(this.data.id);
1882+
const value = storageEntry && storageEntry.value;
18741883
if (value === undefined) {
18751884
return null;
18761885
}

src/display/annotation_storage.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ class AnnotationStorage {
9191
}
9292

9393
getAll() {
94-
if (this._storage.size === 0) {
95-
return null;
96-
}
97-
return objectFromEntries(this._storage);
94+
return this._storage.size > 0 ? objectFromEntries(this._storage) : null;
9895
}
9996

10097
get size() {
@@ -121,6 +118,14 @@ class AnnotationStorage {
121118
}
122119
}
123120
}
121+
122+
/**
123+
* PLEASE NOTE: Only intended for usage within the API itself.
124+
* @ignore
125+
*/
126+
get serializable() {
127+
return this._storage.size > 0 ? this._storage : null;
128+
}
124129
}
125130

126131
export { AnnotationStorage };

src/display/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ class PDFPageProxy {
12141214
pageIndex: this._pageIndex,
12151215
intent: renderingIntent,
12161216
renderInteractiveForms: renderInteractiveForms === true,
1217-
annotationStorage: annotationStorage?.getAll() || null,
1217+
annotationStorage: annotationStorage?.serializable || null,
12181218
});
12191219
}
12201220

@@ -2613,7 +2613,7 @@ class WorkerTransport {
26132613
return this.messageHandler
26142614
.sendWithPromise("SaveDocument", {
26152615
numPages: this._numPages,
2616-
annotationStorage: annotationStorage?.getAll() || null,
2616+
annotationStorage: annotationStorage?.serializable || null,
26172617
filename: this._fullReader?.filename ?? null,
26182618
})
26192619
.finally(() => {

0 commit comments

Comments
 (0)