1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | 'use strict';
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 |
|
28 | 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; }; }();
|
29 |
|
30 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
31 |
|
32 | var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
|
33 |
|
34 | var 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 |
|
137 | exports.ViewHistory = ViewHistory; |
\ | No newline at end of file |