UNPKG

4.53 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 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});
27
28var _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; }; }();
29
30function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
32var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
33
34var ViewHistory = function () {
35 function ViewHistory(fingerprint) {
36 var _this = this;
37
38 var cacheSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_VIEW_HISTORY_CACHE_SIZE;
39
40 _classCallCheck(this, ViewHistory);
41
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 }
49 if (database.files.length >= _this.cacheSize) {
50 database.files.shift();
51 }
52 var index = void 0;
53 for (var i = 0, length = database.files.length; i < length; i++) {
54 var branch = database.files[i];
55 if (branch.fingerprint === _this.fingerprint) {
56 index = i;
57 break;
58 }
59 }
60 if (typeof index !== 'number') {
61 index = database.files.push({ fingerprint: _this.fingerprint }) - 1;
62 }
63 _this.file = database.files[index];
64 _this.database = database;
65 });
66 }
67
68 _createClass(ViewHistory, [{
69 key: '_writeToStorage',
70 value: function _writeToStorage() {
71 var _this2 = this;
72
73 return new Promise(function (resolve) {
74 var databaseStr = JSON.stringify(_this2.database);
75 localStorage.setItem('pdfjs.history', databaseStr);
76 resolve();
77 });
78 }
79 }, {
80 key: '_readFromStorage',
81 value: function _readFromStorage() {
82 return new Promise(function (resolve) {
83 resolve(localStorage.getItem('pdfjs.history'));
84 });
85 }
86 }, {
87 key: 'set',
88 value: function set(name, val) {
89 var _this3 = this;
90
91 return this._initializedPromise.then(function () {
92 _this3.file[name] = val;
93 return _this3._writeToStorage();
94 });
95 }
96 }, {
97 key: 'setMultiple',
98 value: function setMultiple(properties) {
99 var _this4 = this;
100
101 return this._initializedPromise.then(function () {
102 for (var name in properties) {
103 _this4.file[name] = properties[name];
104 }
105 return _this4._writeToStorage();
106 });
107 }
108 }, {
109 key: 'get',
110 value: function get(name, defaultValue) {
111 var _this5 = this;
112
113 return this._initializedPromise.then(function () {
114 var val = _this5.file[name];
115 return val !== undefined ? val : defaultValue;
116 });
117 }
118 }, {
119 key: 'getMultiple',
120 value: function getMultiple(properties) {
121 var _this6 = this;
122
123 return this._initializedPromise.then(function () {
124 var values = Object.create(null);
125 for (var name in properties) {
126 var val = _this6.file[name];
127 values[name] = val !== undefined ? val : properties[name];
128 }
129 return values;
130 });
131 }
132 }]);
133
134 return ViewHistory;
135}();
136
137exports.ViewHistory = ViewHistory;
\No newline at end of file