2020-11-23 20:41:26 +00:00
|
|
|
import * as plugins from './domtools.plugins';
|
|
|
|
import { LitElement } from 'lit-element';
|
|
|
|
|
|
|
|
export class ElementInstrumenter {
|
|
|
|
connectedElements = new plugins.lik.ObjectMap<LitElement>();
|
|
|
|
|
|
|
|
public instrumentElement(elementArg: LitElement) {
|
2020-11-25 14:43:23 +00:00
|
|
|
const addToConnectedElementsFunc = (eventArg: CustomEvent) => {
|
2020-11-23 20:41:26 +00:00
|
|
|
this.connectedElements.add(elementArg);
|
2020-11-25 14:43:23 +00:00
|
|
|
};
|
|
|
|
const removeFromConnectedElementsFunc = eventArg => {
|
2020-11-23 20:41:26 +00:00
|
|
|
this.connectedElements.remove(elementArg);
|
2020-11-25 14:43:23 +00:00
|
|
|
elementArg.removeEventListener('domtools-connected', addToConnectedElementsFunc);
|
|
|
|
elementArg.removeEventListener('domtools-disconnected', removeFromConnectedElementsFunc);
|
2020-11-23 20:41:26 +00:00
|
|
|
};
|
2020-11-25 14:43:23 +00:00
|
|
|
elementArg.addEventListener('domtools-connected', addToConnectedElementsFunc);
|
|
|
|
elementArg.addEventListener('domtools-disconnected', removeFromConnectedElementsFunc);
|
2020-11-24 16:55:57 +00:00
|
|
|
|
2020-11-25 14:56:08 +00:00
|
|
|
if (elementArg.parentElement) {
|
2020-11-25 14:43:23 +00:00
|
|
|
this.connectedElements.add(elementArg);
|
|
|
|
}
|
2020-11-23 20:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async forEachelement(eachFuncArg: (elementArg: LitElement) => Promise<void>) {
|
|
|
|
for (const elementArg of this.connectedElements.getArray()) {
|
|
|
|
await eachFuncArg(elementArg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|