UNPKG

9 kBJavaScriptView Raw
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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.ViewHistory = void 0;
28
29var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
32
33function 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); } }
34
35function _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); }); }; }
36
37function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
39function _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); } }
40
41function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
42
43var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
44
45var ViewHistory =
46/*#__PURE__*/
47function () {
48 function ViewHistory(fingerprint) {
49 var _this = this;
50
51 var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_VIEW_HISTORY_CACHE_SIZE;
52
53 _classCallCheck(this, ViewHistory);
54
55 this.fingerprint = fingerprint;
56 this.cacheSize = cacheSize;
57 this._initializedPromise = this._readFromStorage().then(function (databaseStr) {
58 var database = JSON.parse(databaseStr || '{}');
59
60 if (!('files' in database)) {
61 database.files = [];
62 } else {
63 while (database.files.length >= _this.cacheSize) {
64 database.files.shift();
65 }
66 }
67
68 var index = -1;
69
70 for (var i = 0, length = database.files.length; i < length; i++) {
71 var branch = database.files[i];
72
73 if (branch.fingerprint === _this.fingerprint) {
74 index = i;
75 break;
76 }
77 }
78
79 if (index === -1) {
80 index = database.files.push({
81 fingerprint: _this.fingerprint
82 }) - 1;
83 }
84
85 _this.file = database.files[index];
86 _this.database = database;
87 });
88 }
89
90 _createClass(ViewHistory, [{
91 key: "_writeToStorage",
92 value: function () {
93 var _writeToStorage2 = _asyncToGenerator(
94 /*#__PURE__*/
95 _regenerator["default"].mark(function _callee() {
96 var databaseStr;
97 return _regenerator["default"].wrap(function _callee$(_context) {
98 while (1) {
99 switch (_context.prev = _context.next) {
100 case 0:
101 databaseStr = JSON.stringify(this.database);
102 localStorage.setItem('pdfjs.history', databaseStr);
103
104 case 2:
105 case "end":
106 return _context.stop();
107 }
108 }
109 }, _callee, this);
110 }));
111
112 function _writeToStorage() {
113 return _writeToStorage2.apply(this, arguments);
114 }
115
116 return _writeToStorage;
117 }()
118 }, {
119 key: "_readFromStorage",
120 value: function () {
121 var _readFromStorage2 = _asyncToGenerator(
122 /*#__PURE__*/
123 _regenerator["default"].mark(function _callee2() {
124 return _regenerator["default"].wrap(function _callee2$(_context2) {
125 while (1) {
126 switch (_context2.prev = _context2.next) {
127 case 0:
128 return _context2.abrupt("return", localStorage.getItem('pdfjs.history'));
129
130 case 1:
131 case "end":
132 return _context2.stop();
133 }
134 }
135 }, _callee2);
136 }));
137
138 function _readFromStorage() {
139 return _readFromStorage2.apply(this, arguments);
140 }
141
142 return _readFromStorage;
143 }()
144 }, {
145 key: "set",
146 value: function () {
147 var _set = _asyncToGenerator(
148 /*#__PURE__*/
149 _regenerator["default"].mark(function _callee3(name, val) {
150 return _regenerator["default"].wrap(function _callee3$(_context3) {
151 while (1) {
152 switch (_context3.prev = _context3.next) {
153 case 0:
154 _context3.next = 2;
155 return this._initializedPromise;
156
157 case 2:
158 this.file[name] = val;
159 return _context3.abrupt("return", this._writeToStorage());
160
161 case 4:
162 case "end":
163 return _context3.stop();
164 }
165 }
166 }, _callee3, this);
167 }));
168
169 function set(_x, _x2) {
170 return _set.apply(this, arguments);
171 }
172
173 return set;
174 }()
175 }, {
176 key: "setMultiple",
177 value: function () {
178 var _setMultiple = _asyncToGenerator(
179 /*#__PURE__*/
180 _regenerator["default"].mark(function _callee4(properties) {
181 var name;
182 return _regenerator["default"].wrap(function _callee4$(_context4) {
183 while (1) {
184 switch (_context4.prev = _context4.next) {
185 case 0:
186 _context4.next = 2;
187 return this._initializedPromise;
188
189 case 2:
190 for (name in properties) {
191 this.file[name] = properties[name];
192 }
193
194 return _context4.abrupt("return", this._writeToStorage());
195
196 case 4:
197 case "end":
198 return _context4.stop();
199 }
200 }
201 }, _callee4, this);
202 }));
203
204 function setMultiple(_x3) {
205 return _setMultiple.apply(this, arguments);
206 }
207
208 return setMultiple;
209 }()
210 }, {
211 key: "get",
212 value: function () {
213 var _get = _asyncToGenerator(
214 /*#__PURE__*/
215 _regenerator["default"].mark(function _callee5(name, defaultValue) {
216 var val;
217 return _regenerator["default"].wrap(function _callee5$(_context5) {
218 while (1) {
219 switch (_context5.prev = _context5.next) {
220 case 0:
221 _context5.next = 2;
222 return this._initializedPromise;
223
224 case 2:
225 val = this.file[name];
226 return _context5.abrupt("return", val !== undefined ? val : defaultValue);
227
228 case 4:
229 case "end":
230 return _context5.stop();
231 }
232 }
233 }, _callee5, this);
234 }));
235
236 function get(_x4, _x5) {
237 return _get.apply(this, arguments);
238 }
239
240 return get;
241 }()
242 }, {
243 key: "getMultiple",
244 value: function () {
245 var _getMultiple = _asyncToGenerator(
246 /*#__PURE__*/
247 _regenerator["default"].mark(function _callee6(properties) {
248 var values, name, val;
249 return _regenerator["default"].wrap(function _callee6$(_context6) {
250 while (1) {
251 switch (_context6.prev = _context6.next) {
252 case 0:
253 _context6.next = 2;
254 return this._initializedPromise;
255
256 case 2:
257 values = Object.create(null);
258
259 for (name in properties) {
260 val = this.file[name];
261 values[name] = val !== undefined ? val : properties[name];
262 }
263
264 return _context6.abrupt("return", values);
265
266 case 5:
267 case "end":
268 return _context6.stop();
269 }
270 }
271 }, _callee6, this);
272 }));
273
274 function getMultiple(_x6) {
275 return _getMultiple.apply(this, arguments);
276 }
277
278 return getMultiple;
279 }()
280 }]);
281
282 return ViewHistory;
283}();
284
285exports.ViewHistory = ViewHistory;
\No newline at end of file