-
-
Notifications
You must be signed in to change notification settings - Fork 893
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
Clean cancel #843
base: main
Are you sure you want to change the base?
Clean cancel #843
Conversation
Don't call pdf.getPage() if Document is destroyed
Woo, lots of goodness in this PR! I'm especially curious about the Please allow more time for me to review - extremely busy in personal life recently. |
src/Page.jsx
Outdated
// eslint-disable-next-line no-underscore-dangle | ||
if (pdf && pdf._transport && pdf._transport.destroyed) { | ||
this.setState({ page: false }); | ||
this.onLoadError('Attempted to load a page, but the document was destroyed'); |
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.
onLoadError
expects Error
.
this.onLoadError('Attempted to load a page, but the document was destroyed'); | |
this.onLoadError(new Error('Attempted to load a page, but the document was destroyed')); |
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.
changed it
src/Document.jsx
Outdated
// If pdfjs loading is in progress, let's destroy it | ||
if (this.loadingTask) this.loadingTask.destroy(); | ||
|
||
// If FileReader loading is in progress, let's abort it | ||
if (this.loadFromFileTask) { | ||
this.loadFromFileTask.abort(); | ||
} |
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.
Not a fan of this formatting inconsistency, but honestly, that should have been linter's job.
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.
changed it
src/shared/utils.js
Outdated
return { | ||
promise, | ||
abort: () => { | ||
reader.abort(); | ||
}, | ||
}; |
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.
I'm wondering, if perhaps making the API consistent with make-cancelable-promise by changing abort
to cancel
wouldn't be a bettter choice.
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.
Both ok for me. I am thinking: 'cancel' is something like "I did the request, but I don't want it anymore, don't send what ever you have, no matter if finished or not" (which is what cancelable-promise does). "Abort" on the other hand is "Stop what you are doing right now". But from our perspective I guess what we want indeed is the same in both cases: don't call any callbacks. So 'cancel' would make sense.
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.
changed it in an extra commit
- call onLoadError() with Error() - inline if() for code consistency
5e59480
to
41d6126
Compare
ccad668
to
6bb29a9
Compare
653aa2c
to
9bb8a85
Compare
2d488c1
to
a077571
Compare
1f758cc
to
ce6db03
Compare
42a8c3c
to
0d68fee
Compare
d0aabb2
to
736f3f3
Compare
I have a suggestion about cleaner error handling, if you like.
(Note that this is my first pull request ever, sorry if I do something wrong)
I know that react-pdf is not meant to be used like this anyway, but it seems it would be an easy fix.
(commit "cancel Page load if Document destroyed")
(commit "abort file load on document reload/unmount")
(commit "add test for Page outside Document")
Testing:
1. "cancel Page load if Document destroyed"
BEFORE:
TypeError: "this.messageHandler is null"
NOW:
callback .onLoadError("Attempted to load a page, but the document was destroyed")
2. "abort file load on document reload/unmount"
BEFORE:
"Warning: Can't perform a React state update on an unmounted component"
NOW:
no error in console (file load was aborted, setState wasn't called)