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

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* limitations under the License.
*/

import { createPromiseCapability } from 'pdfjs-lib';

const CSS_UNITS = 96.0 / 72.0;
const DEFAULT_SCALE_VALUE = 'auto';
const DEFAULT_SCALE = 1.0;
Expand Down Expand Up @@ -634,37 +632,35 @@ const WaitOnType = {
* @returns {Promise} A promise that is resolved with a {WaitOnType} value.
*/
function waitOnEventOrTimeout({ target, name, delay = 0, }) {
if (typeof target !== 'object' || !(name && typeof name === 'string') ||
!(Number.isInteger(delay) && delay >= 0)) {
return Promise.reject(
new Error('waitOnEventOrTimeout - invalid parameters.'));
}
let capability = createPromiseCapability();

function handler(type) {
if (target instanceof EventBus) {
target.off(name, eventHandler);
} else {
target.removeEventListener(name, eventHandler);
return new Promise(function(resolve, reject) {
if (typeof target !== 'object' || !(name && typeof name === 'string') ||
!(Number.isInteger(delay) && delay >= 0)) {
throw new Error('waitOnEventOrTimeout - invalid parameters.');
}

if (timeout) {
clearTimeout(timeout);
}
capability.resolve(type);
}
function handler(type) {
if (target instanceof EventBus) {
target.off(name, eventHandler);
} else {
target.removeEventListener(name, eventHandler);
}

let eventHandler = handler.bind(null, WaitOnType.EVENT);
if (target instanceof EventBus) {
target.on(name, eventHandler);
} else {
target.addEventListener(name, eventHandler);
}
if (timeout) {
clearTimeout(timeout);
}
resolve(type);
}

let timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
let timeout = setTimeout(timeoutHandler, delay);
const eventHandler = handler.bind(null, WaitOnType.EVENT);
if (target instanceof EventBus) {
target.on(name, eventHandler);
} else {
target.addEventListener(name, eventHandler);
}

return capability.promise;
const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
let timeout = setTimeout(timeoutHandler, delay);
});
}

/**
Expand Down