/**
* Check if a possible element isConnected.
* The property might not be there, so we check for it.
*
* We want it to return true if isConnected is not a property,
* otherwise we would remove these elements and would not update.
*
* Better leak in Edge than to be useless.
*/
const isConnected = (maybeElement) => !('isConnected' in maybeElement) || maybeElement.isConnected;
const cleanupElements = debounce((map) => {
for (let key of map.keys()) {
map.set(key, map.get(key).filter(isConnected));
}
}, 2000);
const stencilSubscription = ({ on }) => {
const elmsToUpdate = new Map();
if (typeof getRenderingRef === 'function') {
// If we are not in a stencil project, we do nothing.
// This function is not really exported by @stencil/core.
on('dispose', () => {
elmsToUpdate.clear();
});
on('get', (propName) => {
const elm = getRenderingRef();
if (elm) {
appendToMap(elmsToUpdate, propName, elm);
}
});
on('set', (propName) => {
const elements = elmsToUpdate.get(propName);
if (elements) {
elmsToUpdate.set(propName, elements.filter(forceUpdate));
}
cleanupElements(elmsToUpdate);
});
on('reset', () => {
elmsToUpdate.forEach((elms) => elms.forEach(forceUpdate));
cleanupElements(elmsToUpdate);
});
}
};