Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code in the web/ folder to use async/await #9995

Merged
merged 6 commits into from
Sep 8, 2018
505 changes: 241 additions & 264 deletions web/app.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function setReferer(url, callback) {
let storageArea = chrome.storage.sync || chrome.storage.local;

class ChromePreferences extends BasePreferences {
_writeToStorage(prefObj) {
async _writeToStorage(prefObj) {
return new Promise((resolve) => {
if (prefObj === this.defaults) {
let keysToRemove = Object.keys(this.defaults);
Expand All @@ -317,7 +317,7 @@ class ChromePreferences extends BasePreferences {
});
}

_readFromStorage(prefObj) {
async _readFromStorage(prefObj) {
return new Promise((resolve) => {
let getPreferences = (defaultPrefs) => {
if (chrome.runtime.lastError) {
Expand Down
19 changes: 9 additions & 10 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ class DownloadManager {
}

class FirefoxPreferences extends BasePreferences {
_writeToStorage(prefObj) {
async _writeToStorage(prefObj) {
return new Promise(function(resolve) {
FirefoxCom.request('setPreferences', prefObj, resolve);
});
}

_readFromStorage(prefObj) {
async _readFromStorage(prefObj) {
return new Promise(function(resolve) {
FirefoxCom.request('getPreferences', prefObj, function(prefStr) {
let readPrefs = JSON.parse(prefStr);
Expand All @@ -145,21 +145,20 @@ class MozL10n {
this.mozL10n = mozL10n;
}

getLanguage() {
return Promise.resolve(this.mozL10n.getLanguage());
async getLanguage() {
return this.mozL10n.getLanguage();
}

getDirection() {
return Promise.resolve(this.mozL10n.getDirection());
async getDirection() {
return this.mozL10n.getDirection();
}

get(property, args, fallback) {
return Promise.resolve(this.mozL10n.get(property, args, fallback));
async get(property, args, fallback) {
return this.mozL10n.get(property, args, fallback);
}

translate(element) {
async translate(element) {
this.mozL10n.translate(element);
return Promise.resolve();
}
}

Expand Down
14 changes: 4 additions & 10 deletions web/genericcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,12 @@ if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('GENERIC')) {
let GenericCom = {};

class GenericPreferences extends BasePreferences {
_writeToStorage(prefObj) {
return new Promise(function(resolve) {
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
resolve();
});
async _writeToStorage(prefObj) {
localStorage.setItem('pdfjs.preferences', JSON.stringify(prefObj));
}

_readFromStorage(prefObj) {
return new Promise(function(resolve) {
let readPrefs = JSON.parse(localStorage.getItem('pdfjs.preferences'));
resolve(readPrefs);
});
async _readFromStorage(prefObj) {
return JSON.parse(localStorage.getItem('pdfjs.preferences'));
}
}

Expand Down
28 changes: 12 additions & 16 deletions web/genericl10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,24 @@ class GenericL10n {
});
}

getLanguage() {
return this._ready.then((l10n) => {
return l10n.getLanguage();
});
async getLanguage() {
const l10n = await this._ready;
return l10n.getLanguage();
}

getDirection() {
return this._ready.then((l10n) => {
return l10n.getDirection();
});
async getDirection() {
const l10n = await this._ready;
return l10n.getDirection();
}

get(property, args, fallback) {
return this._ready.then((l10n) => {
return l10n.get(property, args, fallback);
});
async get(property, args, fallback) {
const l10n = await this._ready;
return l10n.get(property, args, fallback);
}

translate(element) {
return this._ready.then((l10n) => {
return l10n.translate(element);
});
async translate(element) {
const l10n = await this._ready;
return l10n.translate(element);
}
}

Expand Down
8 changes: 4 additions & 4 deletions web/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ class IL10n {
/**
* @returns {Promise<string>} - Resolves to the current locale.
*/
getLanguage() {}
async getLanguage() {}

/**
* @returns {Promise<string>} - Resolves to 'rtl' or 'ltr'.
*/
getDirection() {}
async getDirection() {}

/**
* Translates text identified by the key and adds/formats data using the args
Expand All @@ -179,12 +179,12 @@ class IL10n {
* @param {string} fallback
* @returns {Promise<string>}
*/
get(key, args, fallback) { }
async get(key, args, fallback) { }

/**
* Translates HTML element.
* @param {HTMLElement} element
* @returns {Promise<void>}
*/
translate(element) { }
async translate(element) { }
}
107 changes: 48 additions & 59 deletions web/overlay_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,89 +37,78 @@ class OverlayManager {
* @returns {Promise} A promise that is resolved when the overlay has been
* registered.
*/
register(name, element, callerCloseMethod = null, canForceClose = false) {
return new Promise((resolve) => {
let container;
if (!name || !element || !(container = element.parentNode)) {
throw new Error('Not enough parameters.');
} else if (this._overlays[name]) {
throw new Error('The overlay is already registered.');
}
this._overlays[name] = {
element,
container,
callerCloseMethod,
canForceClose,
};
resolve();
});
async register(name, element, callerCloseMethod = null,
canForceClose = false) {
let container;
if (!name || !element || !(container = element.parentNode)) {
throw new Error('Not enough parameters.');
} else if (this._overlays[name]) {
throw new Error('The overlay is already registered.');
}
this._overlays[name] = {
element,
container,
callerCloseMethod,
canForceClose,
};
}

/**
* @param {string} name - The name of the overlay that is unregistered.
* @returns {Promise} A promise that is resolved when the overlay has been
* unregistered.
*/
unregister(name) {
return new Promise((resolve) => {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (this._active === name) {
throw new Error('The overlay cannot be removed while it is active.');
}
delete this._overlays[name];
resolve();
});
async unregister(name) {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (this._active === name) {
throw new Error('The overlay cannot be removed while it is active.');
}
delete this._overlays[name];
}

/**
* @param {string} name - The name of the overlay that should be opened.
* @returns {Promise} A promise that is resolved when the overlay has been
* opened.
*/
open(name) {
return new Promise((resolve) => {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (this._active) {
if (this._overlays[name].canForceClose) {
this._closeThroughCaller();
} else if (this._active === name) {
throw new Error('The overlay is already active.');
} else {
throw new Error('Another overlay is currently active.');
}
async open(name) {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (this._active) {
if (this._overlays[name].canForceClose) {
this._closeThroughCaller();
} else if (this._active === name) {
throw new Error('The overlay is already active.');
} else {
throw new Error('Another overlay is currently active.');
}
this._active = name;
this._overlays[this._active].element.classList.remove('hidden');
this._overlays[this._active].container.classList.remove('hidden');
}
this._active = name;
this._overlays[this._active].element.classList.remove('hidden');
this._overlays[this._active].container.classList.remove('hidden');

window.addEventListener('keydown', this._keyDownBound);
resolve();
});
window.addEventListener('keydown', this._keyDownBound);
}

/**
* @param {string} name - The name of the overlay that should be closed.
* @returns {Promise} A promise that is resolved when the overlay has been
* closed.
*/
close(name) {
return new Promise((resolve) => {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (!this._active) {
throw new Error('The overlay is currently not active.');
} else if (this._active !== name) {
throw new Error('Another overlay is currently active.');
}
this._overlays[this._active].container.classList.add('hidden');
this._overlays[this._active].element.classList.add('hidden');
this._active = null;
async close(name) {
if (!this._overlays[name]) {
throw new Error('The overlay does not exist.');
} else if (!this._active) {
throw new Error('The overlay is currently not active.');
} else if (this._active !== name) {
throw new Error('Another overlay is currently active.');
}
this._overlays[this._active].container.classList.add('hidden');
this._overlays[this._active].element.classList.add('hidden');
this._active = null;

window.removeEventListener('keydown', this._keyDownBound);
resolve();
});
window.removeEventListener('keydown', this._keyDownBound);
}

/**
Expand Down
Loading