stephanrauh / ngx-extended-pdf-viewer

A full-blown PDF viewer for Angular 16, 17, and beyond
https://pdfviewer.net
Apache License 2.0
474 stars 181 forks source link

Bugs when creating and directly destroying an instance #2571

Closed patrick-mf closed 2 days ago

patrick-mf commented 1 week ago

Hello,

we have a problem when the instance is directly destroyed again that various errors such as “TypeError: Cannot read properties of undefined (reading ‘registerFormSupportWithPdfjs’)” occur.

For a better understanding, here is a simple Stackblitz example that creates an instance by clicking on a button and deletes it again with the next click on the button. It may be necessary to press the button several times until the errors are triggered.

https://stackblitz.com/edit/stackblitz-starters-y8x6wd?file=package.json

stephanrauh commented 1 week ago

I suppose that's not the only error message you've seen. Here's a small collection of error messages I saw when testing your reproducer:

viewer-4.6.687.min.mjs:46 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'addEventListener')
    at new AltTextManager (viewer-4.6.687.min.mjs:46:68812)
    at Object._initializeViewerComponents (viewer-4.6.687.min.mjs:46:276895)
    at Object.initialize (viewer-4.6.687.min.mjs:46:273820)
    at async Object.run (viewer-4.6.687.min.mjs:46:281143)

.localservice@runtime.216f7361.js:26 renderView: "TypeError: Cannot read properties of undefined (reading 'initialFormDataStoredInThePDF')"

core.mjs:7188 ERROR Error: Transport destroyed
    at :4200/assets/viewer-4.6.688.mjs:12654:15 [angular]
    at Object.onInvoke (http://localhost:4200/chunk-E4GMGAFF.js:7782:25) [angular]
    at :4200/polyfills.js:2033:30 [angular]
    at :4200/chunk-E4GMGAFF.js:7581:49 [angular]
    at AsyncStackTaggingZoneSpec.onInvokeTask (http://localhost:4200/chunk-E4GMGAFF.js:7581:30) [angular]
    at Object.onInvokeTask (http://localhost:4200/chunk-E4GMGAFF.js:7771:25) [angular]
    at drainMicroTaskQueue (http://localhost:4200/polyfills.js:492:23) [<root>]

.localservice@runtime.216f7361.js:26 Unable to initialize thumbnail viewer Error: Transport destroyed
    at viewer-4.6.687.min.mjs:23:204165
    at _ZoneDelegate.invoke (zone.js:369:28)
    at Object.onInvoke (chunk-TG7MZPJF.js?v=408e8599:8584:25)
    at _ZoneDelegate.invoke (zone.js:368:34)
    at _ZoneImpl.run (zone.js:111:43)
    at zone.js:2538:40
    at _ZoneDelegate.invokeTask (zone.js:402:33)
    at chunk-TG7MZPJF.js?v=408e8599:8383:49
    at AsyncStackTaggingZoneSpec.onInvokeTask (chunk-TG7MZPJF.js?v=408e8599:8383:30)
    at _ZoneDelegate.invokeTask (zone.js:401:38)
stephanrauh commented 1 week ago

Generally speaking, there's little I can do about it. The base library, pdf.js, is highly asynchronously, so it's pretty normal that the component is already destroyed before finishing the initialization. Basically, the solution is to wait until the PDF viewer has initialized. There are several events you can use to detect wether the library has initialized. For example, you can use (pdfLoaded) and (pdfLoadingStarts).

However, the particular error message you've reported can be fixed easily. You improvement is going to land soon with version 21.4.2.

Enjoy! Stephan

stephanrauh commented 4 days ago

Just a short update: I haven't published your bug fix yet. I'm sorry about the delay. I don't know when I manage to publish it - probably after recovering from my nasty flu.

Stay tuned! Stephan

stephanrauh commented 2 days ago

Your bugfix has landed with version 21.4.2.

Enjoy! Stephan