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

Convert the overlay manager to a class #8119

Closed

Conversation

timvandermeij
Copy link
Contributor

@timvandermeij timvandermeij commented Feb 28, 2017

This work makes the code similar to the other components and reduces the number of dependencies for those components because the overlay manager is no longer a global.

I kept the FIXME comments equal to the already existing ones as the way we amend configuration objects should be improved in a follow-up. This way it is at least clear which parts should be refactored.

Much easier to review with https://github.com/mozilla/pdf.js/pull/8119/files?w=1.

This work makes the code similar to the other components and reduces the
number of dependencies for those components because the overlay manager
is no longer a global.
@timvandermeij
Copy link
Contributor Author

/botio-linux preview

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

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

Live output at: http://107.21.233.14:8877/a4540a2e1621378/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux)


Success

Full output at http://107.21.233.14:8877/a4540a2e1621378/output.txt

Total script time: 2.33 mins

Published

@timvandermeij
Copy link
Contributor Author

/botio lint

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

Command cmd_lint from @timvandermeij received. Current queue size: 0

Live output at: http://107.21.233.14:8877/3a3aa8643aa4a40/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_lint from @timvandermeij received. Current queue size: 0

Live output at: http://54.215.176.217:8877/112b3b739bf8a52/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux)


Success

Full output at http://107.21.233.14:8877/3a3aa8643aa4a40/output.txt

Total script time: 1.73 mins

  • Lint: Passed

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

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

I can't help wondering if it wouldn't be better to try and get Babel running as part of the builds, and then just convert the OverlayManager directly to a proper ES6 class instead since that'd reduce the churn in this code considerably (e.g. it'd remove the need for most indentation changes).

Edit: Using a basic ES6 adjusted .eslintrc file, such as this diff, the total diff for the OverlayManager would then be reduced to something like this.

@@ -171,6 +168,7 @@

var chromeFileAccessOverlayPromise;
function requestAccessToLocalFile(fileUrl) {
var overlayManager = PDFViewerApplication.overlayManager;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I'm wondering if it might not be a bit cleaner to just have this definition at the top of the file instead (like it currently is).

@@ -133,10 +132,11 @@
this.scratchCanvas = null;
activeService = null;
ensureOverlay().then(function () {
if (OverlayManager.active !== 'printServiceOverlay') {
var overlayManager = PDFViewerApplication.overlayManager;
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Mar 1, 2017

Choose a reason for hiding this comment

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

Nit: I'm wondering if it might not be a bit cleaner to just have this definition at the top of the file instead (like it currently is, since that'd reduce the total diff a bit in this file).

this.overlays[this.active].element.classList.remove('hidden');
this.overlays[this.active].container.classList.remove('hidden');

window.addEventListener('keydown', this._keyDown.bind(this));
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Mar 1, 2017

Choose a reason for hiding this comment

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

Unfortunately you'll not be able to remove the event listener if you define it like this.
You'll need to add this._keyDownBound = this._keyDown.bind(this); to e.g. the constructor and then use this._keyDownBound here (and below).

@timvandermeij
Copy link
Contributor Author

Closing. I agree that it's better to do this once ES6 is up and running as that will make the change quite a bit smaller. Hopefully the UMD headers will be gone/reduced at that point as well.

@timvandermeij timvandermeij deleted the overlay-manager-class branch March 1, 2017 21:35
@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented Apr 14, 2017

@timvandermeij If you've got time, now might be a good time to revisit this PR :-)

Given that https://bugzilla.mozilla.org/show_bug.cgi?id=1167472 (and its dependant bugs) is still open, we may not want to use ES6 classes in parsing/rendering code for the time being (i.e. not in the /src folder).

However, having a couple of (smaller) ES6 classes in /web code ought to be fine (and removes quite a bit of boilerplate compared to our current "classes"); note e.g. that ES6 classes are already used quite a bit in the Firefox code-base.

@yurydelendik
Copy link
Contributor

ES6 classes in /web code ought to be fine

This blocked by #8287

@timvandermeij
Copy link
Contributor Author

Thank you for reminding me, @Snuffleupagus, and thank you both for the amazing ES6 work! I think I can get to this really soon.

@Snuffleupagus
Copy link
Collaborator

Thank you for reminding me, @Snuffleupagus, and thank you both for the amazing ES6 work! I think I can get to this really soon.

@timvandermeij Gentle ping, since it would be nice to get fixed considering that the OverlayManager is now the only thing that's not a class/classlike object :-)

@timvandermeij
Copy link
Contributor Author

timvandermeij commented May 9, 2017

I looked at this some time ago, but got stuck because the overlay manager has to be initialized in a central place (like app.js), but files like https://github.com/mozilla/pdf.js/blob/master/web/chromecom.js could not access that. The weird thing was that I could access PDFViewerApplication, but not PDFViewerApplication.overlayManager because the initialization sequence was not yet complete. I did not find a way to solve that yet, but it looked like somehow chromecom.js was included before app.js had a chance to initialize complete. Do you have ideas for that?

@yurydelendik
Copy link
Contributor

yurydelendik commented May 9, 2017

The weird thing was that I could access PDFViewerApplication, but not PDFViewerApplication.overlayManager because the initialization sequence was not yet complete.

At what point? Looks like initPassiveLoading is called from webViewerInitialized, which is called after we initialized UI.

@Snuffleupagus
Copy link
Collaborator

Do you have ideas for that?

The first thing that comes to mind is perhaps not conceptually ideal, considering how we initialize all the other viewer components, but it's quite simple:
Just add overlayManager: new OverlayManager(), at line https://github.com/mozilla/pdf.js/blob/master/web/app.js#L147. Since the OverlayManager doesn't take any parameters, and only needs to be initialized, this ought to work. However, I'll readily admit that this is a slightly hacky solution!

@timvandermeij
Copy link
Contributor Author

Right, I think that would work. Perhaps it's not the best solution, but it would allow us to rewrite that code and improve this part later. Hopefully I have time this weekend to refactor the overlay manager, which could lead to more insights on why things did not work previously. It has been a while, so I don't remember exactly at what point the initialization was not complete yet. I'll have to look into that again.

@yurydelendik
Copy link
Contributor

As a tip, try not to use PDFViewerApplication too often, cache its properties somewhere once (e.g. at https://github.com/timvandermeij/pdf.js/blob/ac1b78628d16322a95c11f8662057a373905876b/web/chromecom.js#L353 let overlayManager = PDFViewerApplication.overlayManager;) and pass it via object properties or arguments.

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