diff --git a/package.json b/package.json index 5ef1717..4a3ea36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tp/tp-store", - "version": "1.1.1", + "version": "1.1.2", "description": "", "main": "tp-store.js", "scripts": { diff --git a/store.js b/store.js index b24decb..493bf67 100644 --- a/store.js +++ b/store.js @@ -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 } ]);