dees-domtools/ts/domtools.classes.elementinstrumenter.ts
2020-11-25 14:56:08 +00:00

29 lines
1.1 KiB
TypeScript

import * as plugins from './domtools.plugins';
import { LitElement } from 'lit-element';
export class ElementInstrumenter {
connectedElements = new plugins.lik.ObjectMap<LitElement>();
public instrumentElement(elementArg: LitElement) {
const addToConnectedElementsFunc = (eventArg: CustomEvent) => {
this.connectedElements.add(elementArg);
};
const removeFromConnectedElementsFunc = eventArg => {
this.connectedElements.remove(elementArg);
elementArg.removeEventListener('domtools-connected', addToConnectedElementsFunc);
elementArg.removeEventListener('domtools-disconnected', removeFromConnectedElementsFunc);
};
elementArg.addEventListener('domtools-connected', addToConnectedElementsFunc);
elementArg.addEventListener('domtools-disconnected', removeFromConnectedElementsFunc);
if (elementArg.parentElement) {
this.connectedElements.add(elementArg);
}
}
public async forEachelement(eachFuncArg: (elementArg: LitElement) => Promise<void>) {
for (const elementArg of this.connectedElements.getArray()) {
await eachFuncArg(elementArg);
}
}
}