annotation_storage.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. var _util = require("../shared/util.js");
  28. class AnnotationStorage {
  29. constructor() {
  30. this._storage = new Map();
  31. this._timeStamp = Date.now();
  32. this._modified = false;
  33. this.onSetModified = null;
  34. this.onResetModified = null;
  35. }
  36. getValue(key, defaultValue) {
  37. const value = this._storage.get(key);
  38. if (value === undefined) {
  39. return defaultValue;
  40. }
  41. return Object.assign(defaultValue, value);
  42. }
  43. setValue(key, value) {
  44. const obj = this._storage.get(key);
  45. let modified = false;
  46. if (obj !== undefined) {
  47. for (const [entry, val] of Object.entries(value)) {
  48. if (obj[entry] !== val) {
  49. modified = true;
  50. obj[entry] = val;
  51. }
  52. }
  53. } else {
  54. modified = true;
  55. this._storage.set(key, value);
  56. }
  57. if (modified) {
  58. this._timeStamp = Date.now();
  59. this._setModified();
  60. }
  61. }
  62. getAll() {
  63. return this._storage.size > 0 ? (0, _util.objectFromMap)(this._storage) : null;
  64. }
  65. get size() {
  66. return this._storage.size;
  67. }
  68. _setModified() {
  69. if (!this._modified) {
  70. this._modified = true;
  71. if (typeof this.onSetModified === "function") {
  72. this.onSetModified();
  73. }
  74. }
  75. }
  76. resetModified() {
  77. if (this._modified) {
  78. this._modified = false;
  79. if (typeof this.onResetModified === "function") {
  80. this.onResetModified();
  81. }
  82. }
  83. }
  84. get serializable() {
  85. return this._storage.size > 0 ? this._storage : null;
  86. }
  87. get lastModified() {
  88. return this._timeStamp.toString();
  89. }
  90. }
  91. exports.AnnotationStorage = AnnotationStorage;