Skip to content

Convert examples/learning/helloworld.html to await/async #14138

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

Merged
merged 1 commit into from
Oct 14, 2021

Conversation

cathydev
Copy link

@cathydev cathydev commented Oct 14, 2021

Issue this pull request solves:

closes #14121

@cathydev cathydev changed the title Convert examples/learning/helloworld.html to await/async #14121 Convert examples/learning/helloworld.html to await/async Oct 14, 2021
Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the inline comments, please improve the commit message to say what you're actually changing since it's not entirely clear otherwise without looking at the code; e.g. "Convert examples/learning/helloworld.html to use async/await".

@@ -1,3 +1,4 @@

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change, please revert.

// Support HiDPI-screens.
var outputScale = window.devicePixelRatio || 1;
(async () => {
const pdf = await loadingTask;
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this doesn't actually work!
The loadingTask is not a Promise, but rather a structure with a promise-property; see

pdf.js/src/display/api.js

Lines 527 to 601 in f6d9d91

/**
* The loading task controls the operations required to load a PDF document
* (such as network requests) and provides a way to listen for completion,
* after which individual pages can be rendered.
*/
class PDFDocumentLoadingTask {
static get idCounters() {
return shadow(this, "idCounters", { doc: 0 });
}
constructor() {
this._capability = createPromiseCapability();
this._transport = null;
this._worker = null;
/**
* Unique identifier for the document loading task.
* @type {string}
*/
this.docId = `d${PDFDocumentLoadingTask.idCounters.doc++}`;
/**
* Whether the loading task is destroyed or not.
* @type {boolean}
*/
this.destroyed = false;
/**
* Callback to request a password if a wrong or no password was provided.
* The callback receives two parameters: a function that should be called
* with the new password, and a reason (see {@link PasswordResponses}).
* @type {function}
*/
this.onPassword = null;
/**
* Callback to be able to monitor the loading progress of the PDF file
* (necessary to implement e.g. a loading bar).
* The callback receives an {@link OnProgressParameters} argument.
* @type {function}
*/
this.onProgress = null;
/**
* Callback for when an unsupported feature is used in the PDF document.
* The callback receives an {@link UNSUPPORTED_FEATURES} argument.
* @type {function}
*/
this.onUnsupportedFeature = null;
}
/**
* Promise for document loading task completion.
* @type {Promise<PDFDocumentProxy>}
*/
get promise() {
return this._capability.promise;
}
/**
* Abort all network requests and destroy the worker.
* @returns {Promise<void>} A promise that is resolved when destruction is
* completed.
*/
async destroy() {
this.destroyed = true;
await this._transport?.destroy();
this._transport = null;
if (this._worker) {
this._worker.destroy();
this._worker = null;
}
}
}

Hence the correct change would be const pdf = await loadingTask.promise;.

Furthermore, given that the example cannot possibly work as-is: please make sure to (in this case manually) test the changes that you make to ensure that your code actually works.
Testing this example locally, after making changes, includes:

Comment on lines 34 to 66
const pdf = await loadingTask;
//
// Fetch the first page
//
const page = await pdf.getPage(1);
var scale = 1.5;
var viewport = page.getViewport({ scale });
// Support HiDPI-screens.
var outputScale = window.devicePixelRatio || 1;

//
// Prepare canvas using PDF page dimensions
//
var canvas = document.getElementById("the-canvas");
var context = canvas.getContext("2d");

//
// Prepare canvas using PDF page dimensions
//
var canvas = document.getElementById('the-canvas');
var context = canvas.getContext('2d');
canvas.width = Math.floor(viewport.width * outputScale);
canvas.height = Math.floor(viewport.height * outputScale);
canvas.style.width = Math.floor(viewport.width) + "px";
canvas.style.height = Math.floor(viewport.height) + "px";

canvas.width = Math.floor(viewport.width * outputScale);
canvas.height = Math.floor(viewport.height * outputScale);
canvas.style.width = Math.floor(viewport.width) + "px";
canvas.style.height = Math.floor(viewport.height) + "px";
var transform =
outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;

var transform = outputScale !== 1
? [outputScale, 0, 0, outputScale, 0, 0]
: null;
//
// Render PDF page into canvas context
//
var renderContext = {
canvasContext: context,
transform,
viewport,
};
page.render(renderContext);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this entire block of code should be indented with two more spaces.

@cathydev
Copy link
Author

@Snuffleupagus Hi! Thank you so much for the feedback. I made all the changes you requested but there's an error in the console that says "Uncaught ReferenceError: pdfjsLib is not defined" and I tried with the master branch but it's printing the same error. I went to interactive examples and changed the fiddle file with my code and it's working
https://jsfiddle.net/Lu59de7m/

@Snuffleupagus
Copy link
Collaborator

I made all the changes you requested but there's an error in the console that says "Uncaught ReferenceError: pdfjsLib is not defined" and I tried with the master branch but it's printing the same error.

That might suggest that you didn't follow all of the steps outlined at the bottom of #14138 (comment), since it should definitely work when run against the master branch.

Please also see https://github.com/mozilla/pdf.js/wiki/Squashing-Commits, since we don't use separate fixup commits in this project.

Comment on lines 54 to 56
var transform =
outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0]
: null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a trailing space added at the end of a line above, and let's keep the existing formatting; i.e.

    var transform = outputScale !== 1
      ? [outputScale, 0, 0, outputScale, 0, 0]
      : null;

Finally, please remember https://github.com/mozilla/pdf.js/wiki/Squashing-Commits

@cathydev
Copy link
Author

Thanks, I had a little problem with npm install and that was the reason why of the error but everything is working now

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better now, but there's still a few small things to fix here.
Also, in the commit message the same sentence is repeated more than once; please ensure that the final commit message reads only "Convert examples/learning/helloworld.html to await/async".

Comment on lines 54 to 56
const transform =
outputScale !== 1
? [outputScale, 0, 0, outputScale, 0, 0]
: null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in #14138 (comment), please ensure that this reads as follows (and without any trailing spaces):

const transform = outputScale !== 1
  ? [outputScale, 0, 0, outputScale, 0, 0]
  : null;

@@ -75,4 +76,4 @@ <h2>JavaScript code:</h2>
document.getElementById('script').text;
</script>
</body>
</html>
</html>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change, i.e. re-add the line-break at the end here.

package.json Outdated
Comment on lines 72 to 75
"license": "Apache-2.0",
"dependencies": {
"pdfjs-dist": "file:build/dist"
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert all changes to the package-lock.json and package.json files.

loadingTask.promise.then(function(pdf) {
const loadingTask = pdfjsLib.getDocument(url);
(async () => {
const pdf = await loadingTask.promise;;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One semi-colon too many here.

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me; thanks for the patch!

@Snuffleupagus Snuffleupagus merged commit 4b8496e into mozilla:master Oct 14, 2021
@cathydev cathydev deleted the await/async-issue branch October 15, 2021 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert examples/learning/helloworld.html to await/async
3 participants