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

Allow for unbinding of events in web application #8525

Merged
merged 1 commit into from
Jun 17, 2017

Conversation

curiosity26
Copy link
Contributor

Allows for single page apps to unbind listeners when DOM element is destroyed in #8524

web/app.js Outdated

eventBus.off('resize', webViewerResize);
eventBus.off('hashchange', webViewerHashchange);
eventBus.off('beforeprint', this.beforePrint.bind(this));
Copy link
Contributor

Choose a reason for hiding this comment

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

.bind() returns different function every time it is called, but eventBus.on and .off require the same method to be used.

web/app.js Outdated
window.removeEventListener('click', webViewerClick);
window.removeEventListener('keydown', webViewerKeyDown);

window.removeEventListener('resize', function windowResize() {
Copy link
Contributor

Choose a reason for hiding this comment

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

the methods windowResize is scoped only here and was not used for addEventListener.

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.

The off() and removeEventListener() methods shall use the exact same methods that where used in on() and addEventListener().

@curiosity26
Copy link
Contributor Author

Thanks Yury for the feedback. I'll make sure I cleanup those event listeners. I must be a little brain dead today because as I look at it now, it's glaringly obvious.

web/app.js Outdated
@@ -1330,6 +1330,78 @@ var PDFViewerApplication = {
});
}
},

unbindEvents: function pdfViewBindEvents() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we support ES6 now, please use unbindEvents() { instead here (and similar in the method signature below as well).

web/app.js Outdated
@@ -1330,6 +1330,78 @@ var PDFViewerApplication = {
});
}
},

unbindEvents: function pdfViewBindEvents() {
var eventBus = this.eventBus;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use let instead of var when adding new code (applies elsewhere in the patch as well).

web/app.js Outdated
eventBus.off('find', webViewerFind);
eventBus.off('findfromurlhash', webViewerFindFromUrlHash);
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
eventBus.off('fileinputchange', webViewerFileInputChange);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please correct the indentation, since the standard in PDF.js is two spaces for indentation (applies in multiple places below as well).

web/app.js Outdated
@@ -1300,34 +1309,44 @@ var PDFViewerApplication = {
bindWindowEvents: function pdfViewBindWindowEvents() {
var eventBus = this.eventBus;

// Window Resize
this._boundEvents.windowResize = (function () {
Copy link
Contributor

@yurydelendik yurydelendik Jun 14, 2017

Choose a reason for hiding this comment

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

let's prefer ES6 syntax here, e.g.:

this._boundEvents.windowResize = () => {
  this.dispatch('resize');
};

web/app.js Outdated
this.dispatch('afterprint');
}).bind(eventBus);
// Window Change
this._boundEvents.windowChange = (function (evt) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this code shall not be present in non-GENERIC build, can we keep this._boundEvents assignments interleaved with addEventListener

web/app.js Outdated
}

delete this._boundEvents.windowResize;
Copy link
Contributor

@yurydelendik yurydelendik Jun 14, 2017

Choose a reason for hiding this comment

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

Don't delete, just reset _boundEvents this._boundEvents = {}; at the end.

P.S. Or individual assignments this._boundEvents.windowHashChange = null;

web/app.js Outdated
* Hold Event Listeners bound to an object here to be unbound later
* @type {{}}
* @private
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure that we need such a verbose comment here (or a comment at all really).

web/app.js Outdated
bindWindowEvents() {
let eventBus = this.eventBus;

// Window Resize
Copy link
Collaborator

Choose a reason for hiding this comment

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

These comments doesn't seem to add much value here!?

web/app.js Outdated
// Window Resize
this._boundEvents.windowResize = (function () {
this.dispatch('resize');
}).bind(eventBus);
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Jun 14, 2017

Choose a reason for hiding this comment

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

Please don't use bind needlessly; I think the following ought to work instead:

this._boundEvents.windowResize = () => {
  eventBus.dispatch('resize');
};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

eventListener not eventBus?

web/app.js Outdated
fileInput: evt.target,
});
});
eventBus.off('fileinputchange', webViewerFileInputChange);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, and elsewhere, please remember to use two spaces when indenting.

@Snuffleupagus
Copy link
Collaborator

Also, please remember to squash the commits when addressing review comments; see https://github.com/mozilla/pdf.js/wiki/Squashing-Commits for information on this matter.

web/app.js Outdated
fileInput: evt.target,
});
};
this._boundEvents.windowChange = (evt) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

#8525 (comment) still appears to be unresolved in the latest version of the patch.

web/app.js Outdated
delete this._boundEvents.windowChange;
}

delete this._boundEvents.windowResize;
Copy link
Collaborator

Choose a reason for hiding this comment

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

#8525 (comment) still appears to be unresolved in the latest version of the patch.

web/app.js Outdated
window.removeEventListener('click', webViewerClick);
window.removeEventListener('keydown', webViewerKeyDown);
window.removeEventListener('resize',
this._boundEvents.windowResize);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: More indentation issues here, and below.
(And please don't forget to squash the commits.)

Hold bound event listeners for later unbinding

ES6 styling

More ES6 styling and code cleanup

Remove 4 space indents and remove delete statements.
@curiosity26 curiosity26 force-pushed the master branch 2 times, most recently from c60fd63 to 8326304 Compare June 15, 2017 14:05
@timvandermeij
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.67.70.0:8877/59996c6877a990c/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/59996c6877a990c/output.txt

Total script time: 2.22 mins

Published

@timvandermeij timvandermeij merged commit 7215e9c into mozilla:master Jun 17, 2017
@timvandermeij
Copy link
Contributor

Thank you for the patch!

movsb pushed a commit to movsb/pdf.js that referenced this pull request Jul 14, 2018
Allow for unbinding of events in web application
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.

5 participants