2
0

view_history.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  20. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  21. var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  22. var ViewHistory = function () {
  23. function ViewHistory(fingerprint) {
  24. var _this = this;
  25. var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_VIEW_HISTORY_CACHE_SIZE;
  26. _classCallCheck(this, ViewHistory);
  27. this.fingerprint = fingerprint;
  28. this.cacheSize = cacheSize;
  29. this._initializedPromise = this._readFromStorage().then(function (databaseStr) {
  30. var database = JSON.parse(databaseStr || '{}');
  31. if (!('files' in database)) {
  32. database.files = [];
  33. }
  34. if (database.files.length >= _this.cacheSize) {
  35. database.files.shift();
  36. }
  37. var index = void 0;
  38. for (var i = 0, length = database.files.length; i < length; i++) {
  39. var branch = database.files[i];
  40. if (branch.fingerprint === _this.fingerprint) {
  41. index = i;
  42. break;
  43. }
  44. }
  45. if (typeof index !== 'number') {
  46. index = database.files.push({ fingerprint: _this.fingerprint }) - 1;
  47. }
  48. _this.file = database.files[index];
  49. _this.database = database;
  50. });
  51. }
  52. _createClass(ViewHistory, [{
  53. key: '_writeToStorage',
  54. value: function _writeToStorage() {
  55. var _this2 = this;
  56. return new Promise(function (resolve) {
  57. var databaseStr = JSON.stringify(_this2.database);
  58. localStorage.setItem('pdfjs.history', databaseStr);
  59. resolve();
  60. });
  61. }
  62. }, {
  63. key: '_readFromStorage',
  64. value: function _readFromStorage() {
  65. return new Promise(function (resolve) {
  66. resolve(localStorage.getItem('pdfjs.history'));
  67. });
  68. }
  69. }, {
  70. key: 'set',
  71. value: function set(name, val) {
  72. var _this3 = this;
  73. return this._initializedPromise.then(function () {
  74. _this3.file[name] = val;
  75. return _this3._writeToStorage();
  76. });
  77. }
  78. }, {
  79. key: 'setMultiple',
  80. value: function setMultiple(properties) {
  81. var _this4 = this;
  82. return this._initializedPromise.then(function () {
  83. for (var name in properties) {
  84. _this4.file[name] = properties[name];
  85. }
  86. return _this4._writeToStorage();
  87. });
  88. }
  89. }, {
  90. key: 'get',
  91. value: function get(name, defaultValue) {
  92. var _this5 = this;
  93. return this._initializedPromise.then(function () {
  94. var val = _this5.file[name];
  95. return val !== undefined ? val : defaultValue;
  96. });
  97. }
  98. }, {
  99. key: 'getMultiple',
  100. value: function getMultiple(properties) {
  101. var _this6 = this;
  102. return this._initializedPromise.then(function () {
  103. var values = Object.create(null);
  104. for (var name in properties) {
  105. var val = _this6.file[name];
  106. values[name] = val !== undefined ? val : properties[name];
  107. }
  108. return values;
  109. });
  110. }
  111. }]);
  112. return ViewHistory;
  113. }();
  114. exports.ViewHistory = ViewHistory;