Browse Source

Convert the `ViewHistory` to an ES6 class

Jonas Jenwald 8 years ago
parent
commit
16b4132ebf
  1. 44
      web/view_history.js

44
web/view_history.js

@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
* limitations under the License.
*/
var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
const DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
/**
* View History - This is a utility for saving various view parameters for
@ -24,13 +24,12 @@ var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20; @@ -24,13 +24,12 @@ var DEFAULT_VIEW_HISTORY_CACHE_SIZE = 20;
* - FIREFOX or MOZCENTRAL - uses sessionStorage.
* - GENERIC or CHROME - uses localStorage, if it is available.
*/
var ViewHistory = (function ViewHistoryClosure() {
function ViewHistory(fingerprint, cacheSize) {
class ViewHistory {
constructor(fingerprint, cacheSize = DEFAULT_VIEW_HISTORY_CACHE_SIZE) {
this.fingerprint = fingerprint;
this.cacheSize = cacheSize || DEFAULT_VIEW_HISTORY_CACHE_SIZE;
this.cacheSize = cacheSize;
this.isInitializedPromiseResolved = false;
this.initializedPromise =
this._readFromStorage().then(function (databaseStr) {
this.initializedPromise = this._readFromStorage().then((databaseStr) => {
this.isInitializedPromiseResolved = true;
var database = JSON.parse(databaseStr || '{}');
@ -53,12 +52,11 @@ var ViewHistory = (function ViewHistoryClosure() { @@ -53,12 +52,11 @@ var ViewHistory = (function ViewHistoryClosure() {
}
this.file = database.files[index];
this.database = database;
}.bind(this));
});
}
ViewHistory.prototype = {
_writeToStorage: function ViewHistory_writeToStorage() {
return new Promise(function (resolve) {
_writeToStorage() {
return new Promise((resolve) => {
var databaseStr = JSON.stringify(this.database);
if (typeof PDFJSDev !== 'undefined' &&
@ -68,10 +66,10 @@ var ViewHistory = (function ViewHistoryClosure() { @@ -68,10 +66,10 @@ var ViewHistory = (function ViewHistoryClosure() {
localStorage.setItem('pdfjs.history', databaseStr);
}
resolve();
}.bind(this));
},
});
}
_readFromStorage: function ViewHistory_readFromStorage() {
_readFromStorage() {
return new Promise(function(resolve) {
if (typeof PDFJSDev !== 'undefined' &&
PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
@ -81,7 +79,7 @@ var ViewHistory = (function ViewHistoryClosure() { @@ -81,7 +79,7 @@ var ViewHistory = (function ViewHistoryClosure() {
// TODO: Remove this key-name conversion after a suitable time-frame.
// Note that we only remove the old 'database' entry if it looks like
// it was created by PDF.js. to avoid removing someone else's data.
// it was created by PDF.js, to avoid removing someone else's data.
if (!value) {
var databaseStr = localStorage.getItem('database');
if (databaseStr) {
@ -95,21 +93,20 @@ var ViewHistory = (function ViewHistoryClosure() { @@ -95,21 +93,20 @@ var ViewHistory = (function ViewHistoryClosure() {
} catch (ex) { }
}
}
resolve(value);
}
});
},
}
set: function ViewHistory_set(name, val) {
set(name, val) {
if (!this.isInitializedPromiseResolved) {
return;
}
this.file[name] = val;
return this._writeToStorage();
},
}
setMultiple: function ViewHistory_setMultiple(properties) {
setMultiple(properties) {
if (!this.isInitializedPromiseResolved) {
return;
}
@ -117,18 +114,15 @@ var ViewHistory = (function ViewHistoryClosure() { @@ -117,18 +114,15 @@ var ViewHistory = (function ViewHistoryClosure() {
this.file[name] = properties[name];
}
return this._writeToStorage();
},
}
get: function ViewHistory_get(name, defaultValue) {
get(name, defaultValue) {
if (!this.isInitializedPromiseResolved) {
return defaultValue;
}
return this.file[name] || defaultValue;
}
};
return ViewHistory;
})();
}
export {
ViewHistory,

Loading…
Cancel
Save