Merge pull request #9898 from Snuffleupagus/rm-createPromiseCapability-waitOnEventOrTimeout

Change `waitOnEventOrTimeout`, in web/ui_utils.js, to return a regular `Promise` and remove the `createPromiseCapability` import
This commit is contained in:
Tim van der Meij 2018-07-16 22:55:53 +02:00 committed by GitHub
commit 7e13977669
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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;
} }
/** /**