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

Unregister components on teardown #123

Merged
merged 1 commit into from
Jul 23, 2021
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
41 changes: 34 additions & 7 deletions addon/components/g-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ function GMapPublicAPI(source) {
},

get components() {
return source.components;
return source.deprecatedPublicComponents;
},
};
}

export default class GMap extends MapComponent {
@tracked canvas;

components = {};
components = new Set();

get publicAPI() {
return GMapPublicAPI(this);
Expand Down Expand Up @@ -106,15 +106,42 @@ export default class GMap extends MapComponent {
this.canvas = canvas;
}

// TODO: Return remove function
@action
getComponent(component, as = 'other') {
let components = this.components[as] ?? [];
let storedComponent = { component, as };
this.components.add(storedComponent);

this.addToDeprecatedPublicComponents(storedComponent);

return {
context: this.publicAPI,
remove: () => {
this.components.delete(storedComponent);
this.removeFromDeprecatedPublicComponents(storedComponent);
},
};
}

// TODO Deprecate access to this and replace with API methods.
deprecatedPublicComponents = {};

components.push(component);
addToDeprecatedPublicComponents({ as, component }) {
if (!(as in this.deprecatedPublicComponents)) {
this.deprecatedPublicComponents[as] = [];
}

this.components[as] ??= components;
this.deprecatedPublicComponents[as].push(component);
}

removeFromDeprecatedPublicComponents({ as, component }) {
let group = this.deprecatedPublicComponents[as];
let index = group.indexOf(component);

if (index > -1) {
group.splice(index, 1);
}

return this.publicAPI;
// For backwards compatibility, we don't remove the groups when they're
// empty.
}
}
9 changes: 8 additions & 1 deletion addon/components/g-map/map-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ export default class MapComponent {
if (mapComponent) {
mapComponent.setMap?.(null);
}

// Unregister from the parent component
this.onTeardown?.();
}

register() {
this.context = this.args.getContext?.(this.publicAPI, this.name);
if (typeof this.args.getContext === 'function') {
let { context, remove } = this.args.getContext(this.publicAPI, this.name);
this.context = context;
this.onTeardown = remove;
}
}

/* Events */
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/components/g-map/marker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,30 @@ module('Integration | Component | g map/marker', function (hooks) {

assert.equal(marker.draggable, true);
});

test('it unregisters a marker on teardown', async function (assert) {
assert.expect(2);

this.set('showMarker', true);

await render(hbs`
<GMap @lat={{this.lat}} @lng={{this.lng}} as |g|>
{{#if this.showMarker}}
<g.marker @lat={{this.lat}} @lng={{this.lng}} @draggable={{true}} />
{{/if}}
</GMap>
`);

let {
components: { markers },
} = await this.waitForMap();

assert.equal(markers.length, 1, 'marker registered');

this.set('showMarker', false);
await this.waitForMap();

// This tests makes sure that the markers array is updated.
assert.equal(markers.length, 0, 'marker unregistered');
});
});