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

Reuse iframe #8

Merged
merged 6 commits into from
Oct 27, 2017
Merged

Reuse iframe #8

merged 6 commits into from
Oct 27, 2017

Conversation

kafkahw
Copy link
Contributor

@kafkahw kafkahw commented Oct 25, 2017

Summary

This PR adds a new method called load to frame so that user can use existing iframe to load a new page and skip the mounting step.
There's also a small improvement made in this PR to avoid potential memory leak.

* Cleans up references of detached nodes by setting them to null
* to avoid potential memory leak
*/
cleanup() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Users may create tons of global variables that reference created frames and iframes and wrappers will be dangling in memory even after calling unmount method in old code. Though it's very unlikely to happen, this cleanup method ensures that we won't have memory leak issues even that does happen.

super(props);

// Binds 'this' for methods called internally
this.handleProviderMessage = this.handleProviderMessage.bind(this);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving all of bindings out of init method. Following current ES6 coding pattern and putting them in constructor.

* to avoid potential memory leak
*/
cleanup() {
this.iframe = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

i'd probably just move this inline into the one place its used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, that's what I did initially, but found it hard to mock setting to nulls in tests (without mocking tests will fail). And that's the reason why I created a method.

Copy link
Contributor

Choose a reason for hiding this comment

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

can we at least prepend the method name with __ or move this method off the class so that its "private".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was kinda hesitated on this because current eslint rule (no-underscore-dangle) discourages us to use prepended underscores. Their argument is that there's no truly private members in javascript. I'm on the fence. It looks like a matter of team preference and won't affect code performance, readability, or complexity. Personally I'd like to have underscores to indicate privacy as other language does (e.g. python), however, eslint rules can be considered as somewhat official style guide of JS maybe. Any thoughts you'd like to share?

@sdivi
Copy link

sdivi commented Oct 25, 2017

+1.This option will help us a lot

@sdivi
Copy link

sdivi commented Oct 25, 2017

@poloka @tbiethman

@@ -141,6 +149,26 @@ class Frame extends EventEmitter {
if (this.wrapper.parentNode === this.container) {
this.container.removeChild(this.wrapper);
Copy link
Contributor

Choose a reason for hiding this comment

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

After removing the wrapper do we want to set it to null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the wrapper is set to null in clean up method.

*/
load(url) {
if (url) {
this.iframe.src = url;
Copy link
Contributor

@mhemesath mhemesath Oct 26, 2017

Choose a reason for hiding this comment

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

Don't you have to set this.origin to the origin of the new url if they differ? I suggest move the loadPage logic into the load() method and having loadPage call this instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd make sure we test this with loading a new src from a different origin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call! I've moved the logic of loadPage to load method and had loadPage call load instead.
As for test, we do have a test page in example that shows how provider triggers an loadPage event to load a new url with a different origin (from localprovider.com to localhost). Since we've made loadPage call load method, I think that may be good to cover tests for load as well.

@kafkahw kafkahw merged commit c497919 into master Oct 27, 2017
@kafkahw kafkahw deleted the reuse-iframe branch October 27, 2017 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants