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

Move the Preferences initialization/fetching code to the top of PDFViewerApplication.initialize, to enable using them when initializing e.g. the viewer components #7886

Merged
merged 4 commits into from
Dec 14, 2016

Conversation

Snuffleupagus
Copy link
Collaborator

@Snuffleupagus Snuffleupagus commented Dec 9, 2016

This is a new attempt at implementing, the main feature of, PR #7586. With the changes made to the 'localized' event in PR #7789, and the first commits here, this should avoid the issues with the Firefox addon that caused the previous PR to be backed out; see #7586 (comment).

I'd suggest looking at one commit at a time, and using the ?w=1 flag should help reduce (or at least simplify) the larger diffs somewhat. Please also note that GitHub may list the commits in a sub-optimal order, according to date, but they should be reviewed as follows:

  • Refactor PDFViewerApplication.initialize into two methods, one that reads the Preferences and one that initializes the various viewer components

  • Move the Preferences initialization/fetching code to the top of PDFViewerApplication.initialize, to enable using them when initializing e.g. the viewer components

    Note that in quick testing using console.time/timeEnd, both locally and with the Firefox addon, the total run time of the entire PDFViewerApplication.initialize function does not seem to change with this patch.

  • Don't call bindEvents() until PDFViewerApplication has been initialized, and move binding of window event listeners to a helper method, to prevent errors if an event manages to arrive too soon

    With bindEvents() now being called after the viewer has been initialized, we no longer need to have PDFViewerApplication.initialized checks in the event handler functions.
    Furthermore by moving the window.addEventListeners to a helper method, PDFViewerApplication.initialized checks are no longer necessary in the event handlers, hence we thus address part of issue Non-determinism during viewer initialization. #7797 here as well.

  • Convert the only remaining consumer (in hand_tool.js) of the 'localized' event to use the localized Promise instead, and only re-dispatch the 'localized' event on the eventBus for GENERIC builds

    Ideally we'd remove the 'localized' event from the eventBus, but for backwards compatibility we keep it in GENERIC builds.
    Note that while we want to ensure that the direction attribute of the HTML is updated as soon as the localized Promise is resolved, we purposely wait until the viewer has been initialized to ensure that the 'localized' event will always be dispatched.


This change is Reviewable

Copy link
Contributor

@yurydelendik yurydelendik left a comment

Choose a reason for hiding this comment

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

Looks good with following changes:

  • we still need to dispatch('localized') event even if we will not handle it (for backwards compatibility)
  • we have lots of closures inside the initialize, can we refactor and move them in some private smaller methods, and just chain calls there.

@Snuffleupagus
Copy link
Collaborator Author

Snuffleupagus commented Dec 10, 2016

Looks good with following changes:

Thank you for the review comments!

we still need to dispatch('localized') event even if we will not handle it (for backwards compatibility)

Sure, I'll undo the removal. A couple of questions first though:
Would it be enough to only re-dispatch in GENERIC builds, or do we still need it everywhere?
Also, since the eventBus will now be initialized ever so slightly later than before, do we need to consider that the localized Promise might be resolved before the eventBus is defined, i.e. should I move the re-dispatching such that we know for sure that it always works?

we have lots of closures inside the initialize, can we refactor and move them in some private smaller methods, and just chain calls there.

Good point, I'll try to do that.

@yurydelendik
Copy link
Contributor

Would it be enough to only re-dispatch in GENERIC builds, or do we still need it everywhere?
Also, since the eventBus will now be initialized ever so slightly later than before, do we need to consider that the localized Promise might be resolved before the eventBus is defined

Just GENERIC build is fine and it can done sometime during initialization (in addition to what you have now)

@Snuffleupagus
Copy link
Collaborator Author

I've now no restored the 'localized' event in GENERIC builds, and also re-factored PDFViewerApplication into the two methods _readPreferences and _initializeViewerComponents (both async).

/botio-linux preview

@Snuffleupagus
Copy link
Collaborator Author

Having just remembered issue #7797, should I simply move the various window.addEventListeners into PDFViewerApplication.bindEvents() instead of adding if (!PDFViewerApplication.initialized) { return; } checks in all the window listeners?

@Snuffleupagus
Copy link
Collaborator Author

Besides re-factoring the viewer initialization into helper methods, the latest version of the also PR moves the window event listeners into a helper method thus addressing part of #7797.

/botio-windows preview

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_preview from @Snuffleupagus received. Current queue size: 0

Live output at: http://107.22.172.223:8877/02730e8a742eeb1/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://107.22.172.223:8877/02730e8a742eeb1/output.txt

Total script time: 3.35 mins

Published

@Snuffleupagus
Copy link
Collaborator Author

Snuffleupagus commented Dec 14, 2016

@yurydelendik Since you reviewed, and approved, this PR a number of changes have been made: PDFViewerApplication.initialize has been re-factored into _readPreferences/_intializeViewerComponents, and the binding of window event listeners was moved to a bindWindowEvents method: hence it'd probably be a good idea if you looked through this once more before landing!

Copy link
Contributor

@yurydelendik yurydelendik left a comment

Choose a reason for hiding this comment

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

Looks good, thank you.


this.toolbar = new Toolbar(appConfig.toolbar, container, eventBus);
return this._readPreferences().then(function () {
return self._initializeViewerComponents().then(function () {
Copy link
Contributor

@yurydelendik yurydelendik Dec 14, 2016

Choose a reason for hiding this comment

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

nit: to reduce indentation level return _initializeViewerComponents promise and move closure to the outer promise then:

  return self._initializeViewerComponents();
}).then(function() {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call, fixed now!

… reads the Preferences and one that initializes the various viewer components
…FViewerApplication.initialize`, to enable using them when initializing e.g. the viewer components

Note that in quick testing using `console.time/timeEnd`, both locally and with the Firefox addon, the total run time of the *entire* `PDFViewerApplication.initialize` function does not seem to change with this patch.
…alized, and move binding of `window` event listeners to a helper method, to prevent errors if an event manages to arrive too soon

With `bindEvents()` now being called after the viewer has been initialized, we no longer need to have `PDFViewerApplication.initialized` checks in the event handler functions.
Furthermore by moving the `window.addEventListener`s to a helper method, `PDFViewerApplication.initialized` checks are no longer necessary in the event handlers, hence we thus address part of issue 7797 here as well.
…ized' event to use the `localized` Promise instead, and only re-dispatch the 'localized' event on the `eventBus` for `GENERIC` builds

Ideally we'd remove the 'localized' event from the `eventBus`, but for backwards compatibility we keep it in `GENERIC` builds.
Note that while we want to ensure that the direction attribute of the HTML is updated as soon as the `localized` Promise is resolved, we purposely wait until the viewer has been initialized to ensure that the 'localized' event will always be dispatched.
@Snuffleupagus
Copy link
Collaborator Author

Thanks for the review!

/botio-linux preview

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

Command cmd_preview from @Snuffleupagus received. Current queue size: 1

Live output at: http://107.21.233.14:8877/96340f6e4b36559/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux)


Success

Full output at http://107.21.233.14:8877/96340f6e4b36559/output.txt

Total script time: 2.15 mins

Published

@Snuffleupagus Snuffleupagus merged commit b629be0 into mozilla:master Dec 14, 2016
@Snuffleupagus Snuffleupagus deleted the viewer-async-initialization branch December 14, 2016 16:34
@timvandermeij
Copy link
Contributor

Nice work! This makes the code much more readable.

movsb pushed a commit to movsb/pdf.js that referenced this pull request Jul 14, 2018
…alization

Move the `Preferences` initialization/fetching code to the top of `PDFViewerApplication.initialize`, to enable using them when initializing e.g. the viewer components
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.

4 participants