Change waitOnEventOrTimeout, in web/ui_utils.js, to return a regular Promise and remove the createPromiseCapability import

*Another small piece of clean-up of code I've previously written; follow-up to PR 8775.*

Importing `createPromiseCapability`, and then using it in just *one* spot, seems unnecessary since the `waitOnEventOrTimeout` function may just as well return a regular `Promise` directly.
This commit is contained in:
Jonas Jenwald 2018-07-16 13:40:51 +02:00
parent 61db85ab64
commit 647fa74793

View File

@ -13,8 +13,6 @@
* limitations under the License. * limitations under the License.
*/ */
import { createPromiseCapability } from 'pdfjs-lib';
const CSS_UNITS = 96.0 / 72.0; const CSS_UNITS = 96.0 / 72.0;
const DEFAULT_SCALE_VALUE = 'auto'; const DEFAULT_SCALE_VALUE = 'auto';
const DEFAULT_SCALE = 1.0; const DEFAULT_SCALE = 1.0;
@ -634,12 +632,11 @@ const WaitOnType = {
* @returns {Promise} A promise that is resolved with a {WaitOnType} value. * @returns {Promise} A promise that is resolved with a {WaitOnType} value.
*/ */
function waitOnEventOrTimeout({ target, name, delay = 0, }) { function waitOnEventOrTimeout({ target, name, delay = 0, }) {
return new Promise(function(resolve, reject) {
if (typeof target !== 'object' || !(name && typeof name === 'string') || if (typeof target !== 'object' || !(name && typeof name === 'string') ||
!(Number.isInteger(delay) && delay >= 0)) { !(Number.isInteger(delay) && delay >= 0)) {
return Promise.reject( throw new Error('waitOnEventOrTimeout - invalid parameters.');
new Error('waitOnEventOrTimeout - invalid parameters.'));
} }
let capability = createPromiseCapability();
function handler(type) { function handler(type) {
if (target instanceof EventBus) { if (target instanceof EventBus) {
@ -651,20 +648,19 @@ function waitOnEventOrTimeout({ target, name, delay = 0, }) {
if (timeout) { if (timeout) {
clearTimeout(timeout); clearTimeout(timeout);
} }
capability.resolve(type); resolve(type);
} }
let eventHandler = handler.bind(null, WaitOnType.EVENT); const eventHandler = handler.bind(null, WaitOnType.EVENT);
if (target instanceof EventBus) { if (target instanceof EventBus) {
target.on(name, eventHandler); target.on(name, eventHandler);
} else { } else {
target.addEventListener(name, eventHandler); target.addEventListener(name, eventHandler);
} }
let timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT); const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
let timeout = setTimeout(timeoutHandler, delay); let timeout = setTimeout(timeoutHandler, delay);
});
return capability.promise;
} }
/** /**