Refactor the Preferences classes to utilize async methods rather than manually returning Promises

This commit is contained in:
Jonas Jenwald 2018-07-30 16:48:16 +02:00
parent 64e70fc16f
commit 233b3274bf
4 changed files with 53 additions and 61 deletions

View File

@ -300,7 +300,7 @@ function setReferer(url, callback) {
let storageArea = chrome.storage.sync || chrome.storage.local; let storageArea = chrome.storage.sync || chrome.storage.local;
class ChromePreferences extends BasePreferences { class ChromePreferences extends BasePreferences {
_writeToStorage(prefObj) { async _writeToStorage(prefObj) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (prefObj === this.defaults) { if (prefObj === this.defaults) {
let keysToRemove = Object.keys(this.defaults); let keysToRemove = Object.keys(this.defaults);
@ -317,7 +317,7 @@ class ChromePreferences extends BasePreferences {
}); });
} }
_readFromStorage(prefObj) { async _readFromStorage(prefObj) {
return new Promise((resolve) => { return new Promise((resolve) => {
let getPreferences = (defaultPrefs) => { let getPreferences = (defaultPrefs) => {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {

View File

@ -124,13 +124,13 @@ class DownloadManager {
} }
class FirefoxPreferences extends BasePreferences { class FirefoxPreferences extends BasePreferences {
_writeToStorage(prefObj) { async _writeToStorage(prefObj) {
return new Promise(function(resolve) { return new Promise(function(resolve) {
FirefoxCom.request('setPreferences', prefObj, resolve); FirefoxCom.request('setPreferences', prefObj, resolve);
}); });
} }
_readFromStorage(prefObj) { async _readFromStorage(prefObj) {
return new Promise(function(resolve) { return new Promise(function(resolve) {
FirefoxCom.request('getPreferences', prefObj, function(prefStr) { FirefoxCom.request('getPreferences', prefObj, function(prefStr) {
let readPrefs = JSON.parse(prefStr); let readPrefs = JSON.parse(prefStr);

View File

@ -26,18 +26,12 @@ if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
let GenericCom = {}; let GenericCom = {};
class GenericPreferences extends BasePreferences { class GenericPreferences extends BasePreferences {
_writeToStorage(prefObj) { async _writeToStorage(prefObj) {
return new Promise(function(resolve) {
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj)); localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
resolve();
});
} }
_readFromStorage(prefObj) { async _readFromStorage(prefObj) {
return new Promise(function(resolve) { return JSON.parse(localStorage.getItem('pdfjs.preferences'));
let readPrefs = JSON.parse(localStorage.getItem('pdfjs.preferences'));
resolve(readPrefs);
});
} }
} }

View File

@ -83,8 +83,8 @@ class BasePreferences {
* @return {Promise} A promise that is resolved when the preference values * @return {Promise} A promise that is resolved when the preference values
* have been written. * have been written.
*/ */
_writeToStorage(prefObj) { async _writeToStorage(prefObj) {
return Promise.reject(new Error('Not implemented: _writeToStorage')); throw new Error('Not implemented: _writeToStorage');
} }
/** /**
@ -93,8 +93,8 @@ class BasePreferences {
* @return {Promise} A promise that is resolved with an {Object} containing * @return {Promise} A promise that is resolved with an {Object} containing
* the preferences that have been read. * the preferences that have been read.
*/ */
_readFromStorage(prefObj) { async _readFromStorage(prefObj) {
return Promise.reject(new Error('Not implemented: _readFromStorage')); throw new Error('Not implemented: _readFromStorage');
} }
/** /**
@ -102,11 +102,10 @@ class BasePreferences {
* @return {Promise} A promise that is resolved when the preference values * @return {Promise} A promise that is resolved when the preference values
* have been reset. * have been reset.
*/ */
reset() { async reset() {
return this._initializedPromise.then(() => { await this._initializedPromise;
this.prefs = Object.assign(Object.create(null), this.defaults); this.prefs = Object.assign(Object.create(null), this.defaults);
return this._writeToStorage(this.defaults); return this._writeToStorage(this.defaults);
});
} }
/** /**
@ -116,15 +115,17 @@ class BasePreferences {
* @return {Promise} A promise that is resolved when the value has been set, * @return {Promise} A promise that is resolved when the value has been set,
* provided that the preference exists and the types match. * provided that the preference exists and the types match.
*/ */
set(name, value) { async set(name, value) {
return this._initializedPromise.then(() => { await this._initializedPromise;
if (this.defaults[name] === undefined) { let defaultValue = this.defaults[name];
if (defaultValue === undefined) {
throw new Error(`Set preference: "${name}" is undefined.`); throw new Error(`Set preference: "${name}" is undefined.`);
} else if (value === undefined) { } else if (value === undefined) {
throw new Error('Set preference: no value is specified.'); throw new Error('Set preference: no value is specified.');
} }
let valueType = typeof value; let valueType = typeof value;
let defaultType = typeof this.defaults[name]; let defaultType = typeof defaultValue;
if (valueType !== defaultType) { if (valueType !== defaultType) {
if (valueType === 'number' && defaultType === 'string') { if (valueType === 'number' && defaultType === 'string') {
@ -140,7 +141,6 @@ class BasePreferences {
} }
this.prefs[name] = value; this.prefs[name] = value;
return this._writeToStorage(this.prefs); return this._writeToStorage(this.prefs);
});
} }
/** /**
@ -149,8 +149,8 @@ class BasePreferences {
* @return {Promise} A promise that is resolved with a {boolean|number|string} * @return {Promise} A promise that is resolved with a {boolean|number|string}
* containing the value of the preference. * containing the value of the preference.
*/ */
get(name) { async get(name) {
return this._initializedPromise.then(() => { await this._initializedPromise;
let defaultValue = this.defaults[name]; let defaultValue = this.defaults[name];
if (defaultValue === undefined) { if (defaultValue === undefined) {
@ -163,7 +163,6 @@ class BasePreferences {
} }
} }
return defaultValue; return defaultValue;
});
} }
/** /**
@ -171,10 +170,9 @@ class BasePreferences {
* @return {Promise} A promise that is resolved with an {Object} containing * @return {Promise} A promise that is resolved with an {Object} containing
* the values of all preferences. * the values of all preferences.
*/ */
getAll() { async getAll() {
return this._initializedPromise.then(() => { await this._initializedPromise;
return Object.assign(Object.create(null), this.defaults, this.prefs); return Object.assign(Object.create(null), this.defaults, this.prefs);
});
} }
} }