view_history.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. var value = localStorage.getItem('pdfjs.history');
  67. if (!value) {
  68. var databaseStr = localStorage.getItem('database');
  69. if (databaseStr) {
  70. try {
  71. var database = JSON.parse(databaseStr);
  72. if (typeof database.files[0].fingerprint === 'string') {
  73. localStorage.setItem('pdfjs.history', databaseStr);
  74. localStorage.removeItem('database');
  75. value = databaseStr;
  76. }
  77. } catch (ex) {}
  78. }
  79. }
  80. resolve(value);
  81. });
  82. }
  83. }, {
  84. key: 'set',
  85. value: function set(name, val) {
  86. var _this3 = this;
  87. return this._initializedPromise.then(function () {
  88. _this3.file[name] = val;
  89. return _this3._writeToStorage();
  90. });
  91. }
  92. }, {
  93. key: 'setMultiple',
  94. value: function setMultiple(properties) {
  95. var _this4 = this;
  96. return this._initializedPromise.then(function () {
  97. for (var name in properties) {
  98. _this4.file[name] = properties[name];
  99. }
  100. return _this4._writeToStorage();
  101. });
  102. }
  103. }, {
  104. key: 'get',
  105. value: function get(name, defaultValue) {
  106. var _this5 = this;
  107. return this._initializedPromise.then(function () {
  108. var val = _this5.file[name];
  109. return val !== undefined ? val : defaultValue;
  110. });
  111. }
  112. }, {
  113. key: 'getMultiple',
  114. value: function getMultiple(properties) {
  115. var _this6 = this;
  116. return this._initializedPromise.then(function () {
  117. var values = Object.create(null);
  118. for (var name in properties) {
  119. var val = _this6.file[name];
  120. values[name] = val !== undefined ? val : properties[name];
  121. }
  122. return values;
  123. });
  124. }
  125. }]);
  126. return ViewHistory;
  127. }();
  128. exports.ViewHistory = ViewHistory;