-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Conversation
web/app.js
Outdated
|
||
eventBus.off('resize', webViewerResize); | ||
eventBus.off('hashchange', webViewerHashchange); | ||
eventBus.off('beforeprint', this.beforePrint.bind(this)); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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().
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() { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 () { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 | ||
*/ |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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');
};
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
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) => { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
c60fd63
to
8326304
Compare
/botio-linux preview |
From: Bot.io (Linux m4)ReceivedCommand cmd_preview from @timvandermeij received. Current queue size: 0 Live output at: http://54.67.70.0:8877/59996c6877a990c/output.txt |
From: Bot.io (Linux m4)SuccessFull output at http://54.67.70.0:8877/59996c6877a990c/output.txt Total script time: 2.22 mins Published |
Thank you for the patch! |
Allow for unbinding of events in web application
Allows for single page apps to unbind listeners when DOM element is destroyed in #8524