nathan-gs / ha-map-card

A Map Card for Home Assistant
https://community.home-assistant.io/t/map-card-a-slightly-improved-map-card/693088
MIT License
19 stars 4 forks source link

Fix card being disposed when reloading the page or switching between dashboards #14

Closed Manuel-Materazzo closed 3 months ago

Manuel-Materazzo commented 3 months ago

This pull request provides a fix for issue #13 and addresses scenarios involving early component disconnections (such as drag and drop events, swap between dashboards containing the same card and HA dashboard "edit ui" exit).

The best choice here should be to always initialize the map (and the resize observer) inside the connectedCallback, so it gets always re-initialized after being disposed. However in our case this is not feasible because leaflet needs an element hook. The official implementation accomplish the task by injecting the map element directly on the shadow dom, i've tried mimiking it, but to no avail (if you switch dashboards, the newly generated shadow dom does not render correctly).

I have added a check in connectedCallback to see if the map element is loaded. If it is, both the map element and the resizeObserver are re-initialized. This way we can both initialize the map normally on firstUpdated and keep the memory leak fix after the card unload.

nathan-gs commented 3 months ago

@Manuel-Materazzo thanks a lot! It's available in v0.5.2.