-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Conversation
There was a problem hiding this 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".
examples/learning/helloworld.html
Outdated
@@ -1,3 +1,4 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change, please revert.
examples/learning/helloworld.html
Outdated
// Support HiDPI-screens. | ||
var outputScale = window.devicePixelRatio || 1; | ||
(async () => { | ||
const pdf = await loadingTask; |
There was a problem hiding this comment.
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
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:
- Running
gulp dist-install
. - Running
gulp server
. - Opening http://localhost:8888/examples/learning/helloworld.html in a browser and check that the example loads and works correctly without e.g. any errors printed in the console.
examples/learning/helloworld.html
Outdated
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); |
There was a problem hiding this comment.
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.
@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 |
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 Please also see https://github.com/mozilla/pdf.js/wiki/Squashing-Commits, since we don't use separate fixup commits in this project. |
examples/learning/helloworld.html
Outdated
var transform = | ||
outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] | ||
: null; |
There was a problem hiding this comment.
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
3133970
to
8c82ed6
Compare
Thanks, I had a little problem with npm install and that was the reason why of the error but everything is working now |
There was a problem hiding this 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".
examples/learning/helloworld.html
Outdated
const transform = | ||
outputScale !== 1 | ||
? [outputScale, 0, 0, outputScale, 0, 0] | ||
: null; |
There was a problem hiding this comment.
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;
examples/learning/helloworld.html
Outdated
@@ -75,4 +76,4 @@ <h2>JavaScript code:</h2> | |||
document.getElementById('script').text; | |||
</script> | |||
</body> | |||
</html> | |||
</html> |
There was a problem hiding this comment.
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
"license": "Apache-2.0", | ||
"dependencies": { | ||
"pdfjs-dist": "file:build/dist" | ||
} |
There was a problem hiding this comment.
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.
examples/learning/helloworld.html
Outdated
loadingTask.promise.then(function(pdf) { | ||
const loadingTask = pdfjsLib.getDocument(url); | ||
(async () => { | ||
const pdf = await loadingTask.promise;; |
There was a problem hiding this comment.
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.
8c82ed6
to
47f60a7
Compare
There was a problem hiding this 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!
Issue this pull request solves:
closes #14121