view_history.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.ViewHistory = void 0;
  27. const DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  28. class ViewHistory {
  29. constructor(fingerprint, cacheSize = DEFAULT_VIEW_HISTORY_CACHE_SIZE) {
  30. this.fingerprint = fingerprint;
  31. this.cacheSize = cacheSize;
  32. this._initializedPromise = this._readFromStorage().then(databaseStr => {
  33. const database = JSON.parse(databaseStr || "{}");
  34. let index = -1;
  35. if (!Array.isArray(database.files)) {
  36. database.files = [];
  37. } else {
  38. while (database.files.length >= this.cacheSize) {
  39. database.files.shift();
  40. }
  41. for (let i = 0, ii = database.files.length; i < ii; i++) {
  42. const branch = database.files[i];
  43. if (branch.fingerprint === this.fingerprint) {
  44. index = i;
  45. break;
  46. }
  47. }
  48. }
  49. if (index === -1) {
  50. index = database.files.push({
  51. fingerprint: this.fingerprint
  52. }) - 1;
  53. }
  54. this.file = database.files[index];
  55. this.database = database;
  56. });
  57. }
  58. async _writeToStorage() {
  59. const databaseStr = JSON.stringify(this.database);
  60. localStorage.setItem("pdfjs.history", databaseStr);
  61. }
  62. async _readFromStorage() {
  63. return localStorage.getItem("pdfjs.history");
  64. }
  65. async set(name, val) {
  66. await this._initializedPromise;
  67. this.file[name] = val;
  68. return this._writeToStorage();
  69. }
  70. async setMultiple(properties) {
  71. await this._initializedPromise;
  72. for (const name in properties) {
  73. this.file[name] = properties[name];
  74. }
  75. return this._writeToStorage();
  76. }
  77. async get(name, defaultValue) {
  78. await this._initializedPromise;
  79. const val = this.file[name];
  80. return val !== undefined ? val : defaultValue;
  81. }
  82. async getMultiple(properties) {
  83. await this._initializedPromise;
  84. const values = Object.create(null);
  85. for (const name in properties) {
  86. const val = this.file[name];
  87. values[name] = val !== undefined ? val : properties[name];
  88. }
  89. return values;
  90. }
  91. }
  92. exports.ViewHistory = ViewHistory;