annotation_storage.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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 _murmurhash = require("../shared/murmurhash3.js");
  28. var _util = require("../shared/util.js");
  29. class AnnotationStorage {
  30. constructor() {
  31. this._storage = new Map();
  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. getRawValue(key) {
  44. return this._storage.get(key);
  45. }
  46. setValue(key, value) {
  47. const obj = this._storage.get(key);
  48. let modified = false;
  49. if (obj !== undefined) {
  50. for (const [entry, val] of Object.entries(value)) {
  51. if (obj[entry] !== val) {
  52. modified = true;
  53. obj[entry] = val;
  54. }
  55. }
  56. } else {
  57. modified = true;
  58. this._storage.set(key, value);
  59. }
  60. if (modified) {
  61. this._setModified();
  62. }
  63. }
  64. getAll() {
  65. return this._storage.size > 0 ? (0, _util.objectFromMap)(this._storage) : null;
  66. }
  67. get size() {
  68. return this._storage.size;
  69. }
  70. _setModified() {
  71. if (!this._modified) {
  72. this._modified = true;
  73. if (typeof this.onSetModified === "function") {
  74. this.onSetModified();
  75. }
  76. }
  77. }
  78. resetModified() {
  79. if (this._modified) {
  80. this._modified = false;
  81. if (typeof this.onResetModified === "function") {
  82. this.onResetModified();
  83. }
  84. }
  85. }
  86. get serializable() {
  87. return this._storage.size > 0 ? this._storage : null;
  88. }
  89. get hash() {
  90. const hash = new _murmurhash.MurmurHash3_64();
  91. for (const [key, value] of this._storage) {
  92. hash.update(`${key}:${JSON.stringify(value)}`);
  93. }
  94. return hash.hexdigest();
  95. }
  96. }
  97. exports.AnnotationStorage = AnnotationStorage;