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

[BUGFIX release] don’t leak last destroyedComponents #14987

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/ember-glimmer/lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class Environment extends GlimmerEnvironment {
this.isInteractive = owner.lookup('-environment:main').isInteractive;

// can be removed once https://github.com/tildeio/glimmer/pull/305 lands
this.destroyedComponents = undefined;
this.destroyedComponents = [];

installPlatformSpecificProtocolForURL(this);

Expand Down Expand Up @@ -275,16 +275,16 @@ export default class Environment extends GlimmerEnvironment {
this.inTransaction = true;

super.begin();

this.destroyedComponents = [];
}

commit() {
let destroyedComponents = this.destroyedComponents;
this.destroyedComponents = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do this assignment here. Why not just reassign to an empty array after the subsequent loop? More of a stylistic thing I think.

Copy link
Member Author

@stefanpenner stefanpenner Mar 8, 2017

Choose a reason for hiding this comment

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

if the loop fails (destroy could throw), we still want to release the contents of the original destroyedComponents. So this way, we ensure they are released before we invoke any further code.

// components queued for destruction must be destroyed before firing
// `didCreate` to prevent errors when removing and adding a component
// with the same name (would throw an error when added to view registry)
for (let i = 0; i < this.destroyedComponents.length; i++) {
this.destroyedComponents[i].destroy();
for (let i = 0; i < destroyedComponents.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you want to cache length here?

Copy link
Member Author

Choose a reason for hiding this comment

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

tell me more

Copy link
Contributor

Choose a reason for hiding this comment

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

lol stop messing with the old guy...

destroyedComponents[i].destroy();
}

super.commit();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { set } from 'ember-metal';
import { Component } from '../../utils/helpers';
import { moduleFor, RenderingTest } from '../../utils/test-case';

moduleFor('Component destroy', class extends RenderingTest {
['@test it correctly releases the destroyed components'](assert) {
let FooBarComponent = Component.extend({});

this.registerComponent('foo-bar', { ComponentClass: FooBarComponent, template: 'hello' });

this.render('{{#if switch}}{{#foo-bar}}{{foo-bar}}{{/foo-bar}}{{/if}}', { switch: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

foobar inception...

Copy link
Member Author

@stefanpenner stefanpenner Mar 8, 2017

Choose a reason for hiding this comment

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


this.assertComponentElement(this.firstChild, { content: 'hello' });

this.runTask(() => set(this.context, 'switch', false));

this.assertText('');

assert.equal(this.env.destroyedComponents.length, 0, 'enviroment.destroyedComponents should be empty');
}
});