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

Issues with patch that removed disableWorker #9443

Closed
tonyjin opened this issue Feb 6, 2018 · 3 comments
Closed

Issues with patch that removed disableWorker #9443

tonyjin opened this issue Feb 6, 2018 · 3 comments
Labels

Comments

@tonyjin
Copy link

tonyjin commented Feb 6, 2018

Thanks for all the great work!

I'm running into a slight issue with my particular setup. The recent patch to remove PDFJS.disableWorker (56a8c93) updated one of the checks for a fake worker to use

 if (typeof Worker !== 'undefined' && !isWorkerDisabled &&
          !getMainThreadWorkerMessageHandler()) {

I prefetch the worker file while users are not on a page that requires PDF preview, and when they do come to a document preview, I point the workerSrc to that same file that I prefetched earlier. Because of browser caching, the worker can be loaded from the cache. With this patch, getMainThreadWorkerMessageHandler() will return a handler because I prefetched the worker file at an earlier time and then this block returns false and goes into _setupFakeWorker(). Can this last handler check be entirely removed?

On a related, but slightly different note, would you folks be open to a PR that swaps these two in

function getWorkerSrc() {
?

if (typeof workerSrc !== 'undefined') {
      return workerSrc;
}
if (getDefaultSetting('workerSrc')) {
     return getDefaultSetting('workerSrc');
}

The library I build can sometimes be run in an application that uses RequireJS (in which workerSrc will be set to requirejs.url(...something)). I'd explicitly like to keep using the workerSrc I define using PDFJS.workerSrc = 'someUrl'.

@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented Feb 6, 2018

Can this last handler check be entirely removed?

If what you're suggesting is to completely remove line

!getMainThreadWorkerMessageHandler()) {

then that would make it totally impossible, rather than the intended difficult, for users to disable workers.
This would also imply that it's no longer possible to simulate the functionality of the (now removed) pdf.combined.js file, by manually loading pdf.worker.js into the main-thread.

Please note that, as part of the upcoming version 2.0 of PDF.js, we wanted to make it more difficult (but not impossible) to disable workers. Keep in mind that using PDF.js without workers will result in worse performance.
Unfortunately, since the pdf.combined.js file appeares to be somewhat popular in practice we still wanted a way to support that use-case; hence why this code was re-factored in PR #9385.

I prefetch the worker file while users are not on a page that requires PDF preview, and when they do come to a document preview, I point the workerSrc to that same file that I prefetched earlier.

In that case, why don't you simply use workerPort instead to load the worker file?

/**
* Defines global port for worker process. Overrides `workerSrc` setting.
*/
PDFJS.workerPort = (PDFJS.workerPort === undefined ? null : PDFJS.workerPort);

@tonyjin
Copy link
Author

tonyjin commented Feb 6, 2018

Ah perfect, let me try workerPort, looks like what I need :)

Edit: Unfortunately it looks like SharedWorkers (for the exposed port) aren't supported in IE11, which we have to support for the foreseeable future. I'm going to look into using preload instead of prefetch for the worker script, which should prevent execution.

Do you foresee any issue with swapping the order of:

if (typeof workerSrc !== 'undefined') {
      return workerSrc;
}
if (getDefaultSetting('workerSrc')) {
     return getDefaultSetting('workerSrc');
}

So if workerSrc is manually set, we always use that instead of letting the RequireJS check override it? (from

workerSrc = requirejs.toUrl('pdfjs-dist/build/pdf.worker.js');
)

@Snuffleupagus
Copy link
Collaborator

Do you foresee any issue with swapping the order of:

That seems somewhat orthogonal to the rest of the issue, but I suppose that would be OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants