Cleanup references to instance when disconnecting!

This commit is contained in:
trading_peter 2025-01-19 16:54:15 +01:00
parent 274eb54990
commit 1bcdb09b54
2 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@tp/tp-store",
"version": "1.1.1",
"version": "1.1.2",
"description": "",
"main": "tp-store.js",
"scripts": {

View File

@ -37,6 +37,20 @@ export const Store = function(superClass) {
this._writeKey(key, value);
}
disconnectedCallback() {
super.disconnectedCallback();
// Clean up all references to this instance.
instancesPerKey.forEach((instances, key) => {
const remainingInstances = instances.filter(entry => entry.instance !== this);
if (remainingInstances.length === 0) {
instancesPerKey.delete(key);
} else {
instancesPerKey.set(key, remainingInstances);
}
});
}
_addInstance(instance, key, targetProperty) {
if (instancesPerKey.has(key) === false) {
instancesPerKey.set(key, [ { instance, targetProperty } ]);