view_history.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  29. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  30. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  31. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  32. 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); } }
  33. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  34. var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
  35. var ViewHistory =
  36. /*#__PURE__*/
  37. function () {
  38. function ViewHistory(fingerprint) {
  39. var _this = this;
  40. var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_VIEW_HISTORY_CACHE_SIZE;
  41. _classCallCheck(this, ViewHistory);
  42. this.fingerprint = fingerprint;
  43. this.cacheSize = cacheSize;
  44. this._initializedPromise = this._readFromStorage().then(function (databaseStr) {
  45. var database = JSON.parse(databaseStr || '{}');
  46. if (!('files' in database)) {
  47. database.files = [];
  48. } else {
  49. while (database.files.length >= _this.cacheSize) {
  50. database.files.shift();
  51. }
  52. }
  53. var index = -1;
  54. for (var i = 0, length = database.files.length; i < length; i++) {
  55. var branch = database.files[i];
  56. if (branch.fingerprint === _this.fingerprint) {
  57. index = i;
  58. break;
  59. }
  60. }
  61. if (index === -1) {
  62. index = database.files.push({
  63. fingerprint: _this.fingerprint
  64. }) - 1;
  65. }
  66. _this.file = database.files[index];
  67. _this.database = database;
  68. });
  69. }
  70. _createClass(ViewHistory, [{
  71. key: "_writeToStorage",
  72. value: function () {
  73. var _writeToStorage2 = _asyncToGenerator(
  74. /*#__PURE__*/
  75. _regenerator["default"].mark(function _callee() {
  76. var databaseStr;
  77. return _regenerator["default"].wrap(function _callee$(_context) {
  78. while (1) {
  79. switch (_context.prev = _context.next) {
  80. case 0:
  81. databaseStr = JSON.stringify(this.database);
  82. localStorage.setItem('pdfjs.history', databaseStr);
  83. case 2:
  84. case "end":
  85. return _context.stop();
  86. }
  87. }
  88. }, _callee, this);
  89. }));
  90. function _writeToStorage() {
  91. return _writeToStorage2.apply(this, arguments);
  92. }
  93. return _writeToStorage;
  94. }()
  95. }, {
  96. key: "_readFromStorage",
  97. value: function () {
  98. var _readFromStorage2 = _asyncToGenerator(
  99. /*#__PURE__*/
  100. _regenerator["default"].mark(function _callee2() {
  101. return _regenerator["default"].wrap(function _callee2$(_context2) {
  102. while (1) {
  103. switch (_context2.prev = _context2.next) {
  104. case 0:
  105. return _context2.abrupt("return", localStorage.getItem('pdfjs.history'));
  106. case 1:
  107. case "end":
  108. return _context2.stop();
  109. }
  110. }
  111. }, _callee2);
  112. }));
  113. function _readFromStorage() {
  114. return _readFromStorage2.apply(this, arguments);
  115. }
  116. return _readFromStorage;
  117. }()
  118. }, {
  119. key: "set",
  120. value: function () {
  121. var _set = _asyncToGenerator(
  122. /*#__PURE__*/
  123. _regenerator["default"].mark(function _callee3(name, val) {
  124. return _regenerator["default"].wrap(function _callee3$(_context3) {
  125. while (1) {
  126. switch (_context3.prev = _context3.next) {
  127. case 0:
  128. _context3.next = 2;
  129. return this._initializedPromise;
  130. case 2:
  131. this.file[name] = val;
  132. return _context3.abrupt("return", this._writeToStorage());
  133. case 4:
  134. case "end":
  135. return _context3.stop();
  136. }
  137. }
  138. }, _callee3, this);
  139. }));
  140. function set(_x, _x2) {
  141. return _set.apply(this, arguments);
  142. }
  143. return set;
  144. }()
  145. }, {
  146. key: "setMultiple",
  147. value: function () {
  148. var _setMultiple = _asyncToGenerator(
  149. /*#__PURE__*/
  150. _regenerator["default"].mark(function _callee4(properties) {
  151. var name;
  152. return _regenerator["default"].wrap(function _callee4$(_context4) {
  153. while (1) {
  154. switch (_context4.prev = _context4.next) {
  155. case 0:
  156. _context4.next = 2;
  157. return this._initializedPromise;
  158. case 2:
  159. for (name in properties) {
  160. this.file[name] = properties[name];
  161. }
  162. return _context4.abrupt("return", this._writeToStorage());
  163. case 4:
  164. case "end":
  165. return _context4.stop();
  166. }
  167. }
  168. }, _callee4, this);
  169. }));
  170. function setMultiple(_x3) {
  171. return _setMultiple.apply(this, arguments);
  172. }
  173. return setMultiple;
  174. }()
  175. }, {
  176. key: "get",
  177. value: function () {
  178. var _get = _asyncToGenerator(
  179. /*#__PURE__*/
  180. _regenerator["default"].mark(function _callee5(name, defaultValue) {
  181. var val;
  182. return _regenerator["default"].wrap(function _callee5$(_context5) {
  183. while (1) {
  184. switch (_context5.prev = _context5.next) {
  185. case 0:
  186. _context5.next = 2;
  187. return this._initializedPromise;
  188. case 2:
  189. val = this.file[name];
  190. return _context5.abrupt("return", val !== undefined ? val : defaultValue);
  191. case 4:
  192. case "end":
  193. return _context5.stop();
  194. }
  195. }
  196. }, _callee5, this);
  197. }));
  198. function get(_x4, _x5) {
  199. return _get.apply(this, arguments);
  200. }
  201. return get;
  202. }()
  203. }, {
  204. key: "getMultiple",
  205. value: function () {
  206. var _getMultiple = _asyncToGenerator(
  207. /*#__PURE__*/
  208. _regenerator["default"].mark(function _callee6(properties) {
  209. var values, name, val;
  210. return _regenerator["default"].wrap(function _callee6$(_context6) {
  211. while (1) {
  212. switch (_context6.prev = _context6.next) {
  213. case 0:
  214. _context6.next = 2;
  215. return this._initializedPromise;
  216. case 2:
  217. values = Object.create(null);
  218. for (name in properties) {
  219. val = this.file[name];
  220. values[name] = val !== undefined ? val : properties[name];
  221. }
  222. return _context6.abrupt("return", values);
  223. case 5:
  224. case "end":
  225. return _context6.stop();
  226. }
  227. }
  228. }, _callee6, this);
  229. }));
  230. function getMultiple(_x6) {
  231. return _getMultiple.apply(this, arguments);
  232. }
  233. return getMultiple;
  234. }()
  235. }]);
  236. return ViewHistory;
  237. }();
  238. exports.ViewHistory = ViewHistory;