view_history.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  20. var ViewHistory = function ViewHistoryClosure() {
  21. function ViewHistory(fingerprint, cacheSize) {
  22. this.fingerprint = fingerprint;
  23. this.cacheSize = cacheSize || DEFAULT_VIEW_HISTORY_CACHE_SIZE;
  24. this.isInitializedPromiseResolved = false;
  25. this.initializedPromise = this._readFromStorage().then(function (databaseStr) {
  26. this.isInitializedPromiseResolved = true;
  27. var database = JSON.parse(databaseStr || '{}');
  28. if (!('files' in database)) {
  29. database.files = [];
  30. }
  31. if (database.files.length >= this.cacheSize) {
  32. database.files.shift();
  33. }
  34. var index;
  35. for (var i = 0, length = database.files.length; i < length; i++) {
  36. var branch = database.files[i];
  37. if (branch.fingerprint === this.fingerprint) {
  38. index = i;
  39. break;
  40. }
  41. }
  42. if (typeof index !== 'number') {
  43. index = database.files.push({ fingerprint: this.fingerprint }) - 1;
  44. }
  45. this.file = database.files[index];
  46. this.database = database;
  47. }.bind(this));
  48. }
  49. ViewHistory.prototype = {
  50. _writeToStorage: function ViewHistory_writeToStorage() {
  51. return new Promise(function (resolve) {
  52. var databaseStr = JSON.stringify(this.database);
  53. localStorage.setItem('pdfjs.history', databaseStr);
  54. resolve();
  55. }.bind(this));
  56. },
  57. _readFromStorage: function ViewHistory_readFromStorage() {
  58. return new Promise(function (resolve) {
  59. var value = localStorage.getItem('pdfjs.history');
  60. if (!value) {
  61. var databaseStr = localStorage.getItem('database');
  62. if (databaseStr) {
  63. try {
  64. var database = JSON.parse(databaseStr);
  65. if (typeof database.files[0].fingerprint === 'string') {
  66. localStorage.setItem('pdfjs.history', databaseStr);
  67. localStorage.removeItem('database');
  68. value = databaseStr;
  69. }
  70. } catch (ex) {}
  71. }
  72. }
  73. resolve(value);
  74. });
  75. },
  76. set: function ViewHistory_set(name, val) {
  77. if (!this.isInitializedPromiseResolved) {
  78. return;
  79. }
  80. this.file[name] = val;
  81. return this._writeToStorage();
  82. },
  83. setMultiple: function ViewHistory_setMultiple(properties) {
  84. if (!this.isInitializedPromiseResolved) {
  85. return;
  86. }
  87. for (var name in properties) {
  88. this.file[name] = properties[name];
  89. }
  90. return this._writeToStorage();
  91. },
  92. get: function ViewHistory_get(name, defaultValue) {
  93. if (!this.isInitializedPromiseResolved) {
  94. return defaultValue;
  95. }
  96. return this.file[name] || defaultValue;
  97. }
  98. };
  99. return ViewHistory;
  100. }();
  101. exports.ViewHistory = ViewHistory;