annotation_storage.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.AnnotationStorage = void 0;
  27. class AnnotationStorage {
  28. constructor() {
  29. this._storage = new Map();
  30. this._modified = false;
  31. this.onSetModified = null;
  32. this.onResetModified = null;
  33. }
  34. getOrCreateValue(key, defaultValue) {
  35. if (this._storage.has(key)) {
  36. return this._storage.get(key);
  37. }
  38. this._storage.set(key, defaultValue);
  39. return defaultValue;
  40. }
  41. setValue(key, value) {
  42. if (this._storage.get(key) !== value) {
  43. this._setModified();
  44. }
  45. this._storage.set(key, value);
  46. }
  47. getAll() {
  48. if (this._storage.size === 0) {
  49. return null;
  50. }
  51. return Object.fromEntries(this._storage);
  52. }
  53. get size() {
  54. return this._storage.size;
  55. }
  56. _setModified() {
  57. if (!this._modified) {
  58. this._modified = true;
  59. if (typeof this.onSetModified === "function") {
  60. this.onSetModified();
  61. }
  62. }
  63. }
  64. resetModified() {
  65. if (this._modified) {
  66. this._modified = false;
  67. if (typeof this.onResetModified === "function") {
  68. this.onResetModified();
  69. }
  70. }
  71. }
  72. }
  73. exports.AnnotationStorage = AnnotationStorage;