1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

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).
This commit is contained in:
Jonas Jenwald 2021-02-18 13:51:08 +01:00
parent 0fa9976268
commit e9038cc3d1
4 changed files with 133 additions and 89 deletions

View file

@ -348,9 +348,10 @@ class Annotation {
}
isHidden(annotationStorage) {
const data = annotationStorage && annotationStorage[this.data.id];
if (data && "hidden" in data) {
return data.hidden;
const storageEntry =
annotationStorage && annotationStorage.get(this.data.id);
if (storageEntry && storageEntry.hidden !== undefined) {
return storageEntry.hidden;
}
return this._hasFlag(this.flags, AnnotationFlag.HIDDEN);
}
@ -1206,8 +1207,11 @@ class WidgetAnnotation extends Annotation {
}
async save(evaluator, task, annotationStorage) {
const value =
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
if (!annotationStorage) {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === this.data.fieldValue || value === undefined) {
return null;
}
@ -1289,8 +1293,8 @@ class WidgetAnnotation extends Annotation {
if (!annotationStorage || isPassword) {
return null;
}
let value =
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
const storageEntry = annotationStorage.get(this.data.id);
let value = storageEntry && storageEntry.value;
if (value === undefined) {
// The annotation hasn't been rendered so use the appearance
return null;
@ -1769,9 +1773,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
if (annotationStorage) {
const value =
annotationStorage[this.data.id] &&
annotationStorage[this.data.id].value;
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === undefined) {
return super.getOperatorList(
evaluator,
@ -1826,8 +1829,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
async _saveCheckbox(evaluator, task, annotationStorage) {
const value =
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
if (!annotationStorage) {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === undefined) {
return null;
}
@ -1869,8 +1875,11 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}
async _saveRadioButton(evaluator, task, annotationStorage) {
const value =
annotationStorage[this.data.id] && annotationStorage[this.data.id].value;
if (!annotationStorage) {
return null;
}
const storageEntry = annotationStorage.get(this.data.id);
const value = storageEntry && storageEntry.value;
if (value === undefined) {
return null;
}

View file

@ -91,10 +91,7 @@ class AnnotationStorage {
}
getAll() {
if (this._storage.size === 0) {
return null;
}
return objectFromEntries(this._storage);
return this._storage.size > 0 ? objectFromEntries(this._storage) : null;
}
get size() {
@ -121,6 +118,14 @@ class AnnotationStorage {
}
}
}
/**
* PLEASE NOTE: Only intended for usage within the API itself.
* @ignore
*/
get serializable() {
return this._storage.size > 0 ? this._storage : null;
}
}
export { AnnotationStorage };

View file

@ -1214,7 +1214,7 @@ class PDFPageProxy {
pageIndex: this._pageIndex,
intent: renderingIntent,
renderInteractiveForms: renderInteractiveForms === true,
annotationStorage: annotationStorage?.getAll() || null,
annotationStorage: annotationStorage?.serializable || null,
});
}
@ -2613,7 +2613,7 @@ class WorkerTransport {
return this.messageHandler
.sendWithPromise("SaveDocument", {
numPages: this._numPages,
annotationStorage: annotationStorage?.getAll() || null,
annotationStorage: annotationStorage?.serializable || null,
filename: this._fullReader?.filename ?? null,
})
.finally(() => {