Use actually private fields in the AnnotationStorage class

These fields were never intended to be public, since modifying them manually would lead to inconsistent state, and with modern EcmaScript features we can now enforce this.
Also, this patch removes a couple of JSDoc comments that we generally don't use.
This commit is contained in:
Jonas Jenwald 2022-11-11 11:59:11 +01:00
parent 595711bd7c
commit d6cd48e12a

View File

@ -21,10 +21,11 @@ import { MurmurHash3_64 } from "../shared/murmurhash3.js";
* Key/value storage for annotation data in forms. * Key/value storage for annotation data in forms.
*/ */
class AnnotationStorage { class AnnotationStorage {
constructor() { #modified = false;
this._storage = new Map();
this._modified = false;
#storage = new Map();
constructor() {
// Callbacks to signal when the modification state is set or reset. // Callbacks to signal when the modification state is set or reset.
// This is used by the viewer to only bind on `beforeunload` if forms // This is used by the viewer to only bind on `beforeunload` if forms
// are actually edited to prevent doing so unconditionally since that // are actually edited to prevent doing so unconditionally since that
@ -36,15 +37,12 @@ class AnnotationStorage {
/** /**
* Get the value for a given key if it exists, or return the default value. * Get the value for a given key if it exists, or return the default value.
*
* @public
* @memberof AnnotationStorage
* @param {string} key * @param {string} key
* @param {Object} defaultValue * @param {Object} defaultValue
* @returns {Object} * @returns {Object}
*/ */
getValue(key, defaultValue) { getValue(key, defaultValue) {
const value = this._storage.get(key); const value = this.#storage.get(key);
if (value === undefined) { if (value === undefined) {
return defaultValue; return defaultValue;
} }
@ -54,14 +52,11 @@ class AnnotationStorage {
/** /**
* Get the value for a given key. * Get the value for a given key.
*
* @public
* @memberof AnnotationStorage
* @param {string} key * @param {string} key
* @returns {Object} * @returns {Object}
*/ */
getRawValue(key) { getRawValue(key) {
return this._storage.get(key); return this.#storage.get(key);
} }
/** /**
@ -69,14 +64,14 @@ class AnnotationStorage {
* @param {string} key * @param {string} key
*/ */
remove(key) { remove(key) {
this._storage.delete(key); this.#storage.delete(key);
if (this._storage.size === 0) { if (this.#storage.size === 0) {
this.resetModified(); this.resetModified();
} }
if (typeof this.onAnnotationEditor === "function") { if (typeof this.onAnnotationEditor === "function") {
for (const value of this._storage.values()) { for (const value of this.#storage.values()) {
if (value instanceof AnnotationEditor) { if (value instanceof AnnotationEditor) {
return; return;
} }
@ -87,14 +82,11 @@ class AnnotationStorage {
/** /**
* Set the value for a given key * Set the value for a given key
*
* @public
* @memberof AnnotationStorage
* @param {string} key * @param {string} key
* @param {Object} value * @param {Object} value
*/ */
setValue(key, value) { setValue(key, value) {
const obj = this._storage.get(key); const obj = this.#storage.get(key);
let modified = false; let modified = false;
if (obj !== undefined) { if (obj !== undefined) {
for (const [entry, val] of Object.entries(value)) { for (const [entry, val] of Object.entries(value)) {
@ -105,7 +97,7 @@ class AnnotationStorage {
} }
} else { } else {
modified = true; modified = true;
this._storage.set(key, value); this.#storage.set(key, value);
} }
if (modified) { if (modified) {
this.#setModified(); this.#setModified();
@ -125,20 +117,20 @@ class AnnotationStorage {
* @returns {boolean} * @returns {boolean}
*/ */
has(key) { has(key) {
return this._storage.has(key); return this.#storage.has(key);
} }
getAll() { getAll() {
return this._storage.size > 0 ? objectFromMap(this._storage) : null; return this.#storage.size > 0 ? objectFromMap(this.#storage) : null;
} }
get size() { get size() {
return this._storage.size; return this.#storage.size;
} }
#setModified() { #setModified() {
if (!this._modified) { if (!this.#modified) {
this._modified = true; this.#modified = true;
if (typeof this.onSetModified === "function") { if (typeof this.onSetModified === "function") {
this.onSetModified(); this.onSetModified();
} }
@ -146,8 +138,8 @@ class AnnotationStorage {
} }
resetModified() { resetModified() {
if (this._modified) { if (this.#modified) {
this._modified = false; this.#modified = false;
if (typeof this.onResetModified === "function") { if (typeof this.onResetModified === "function") {
this.onResetModified(); this.onResetModified();
} }
@ -166,12 +158,12 @@ class AnnotationStorage {
* @ignore * @ignore
*/ */
get serializable() { get serializable() {
if (this._storage.size === 0) { if (this.#storage.size === 0) {
return null; return null;
} }
const clone = new Map(); const clone = new Map();
for (const [key, val] of this._storage) { for (const [key, val] of this.#storage) {
const serialized = const serialized =
val instanceof AnnotationEditor ? val.serialize() : val; val instanceof AnnotationEditor ? val.serialize() : val;
if (serialized) { if (serialized) {