dees-domtools/ts/domtools.classes.elementinstrumenter.ts

32 lines
1015 B
TypeScript
Raw Normal View History

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) {
const originalConnectedCallback = elementArg.connectedCallback;
const originalDisconnectedCallback = elementArg.disconnectedCallback;
2020-11-24 19:18:59 +00:00
if (!elementArg.parentElement) {
elementArg.connectedCallback = () => {
this.connectedElements.add(elementArg);
originalConnectedCallback.apply(elementArg);
};
} else {
2020-11-23 20:41:26 +00:00
this.connectedElements.add(elementArg);
2020-11-24 19:18:59 +00:00
}
2020-11-23 20:41:26 +00:00
elementArg.disconnectedCallback = () => {
this.connectedElements.remove(elementArg);
originalDisconnectedCallback.apply(elementArg);
};
2020-11-24 16:55:57 +00:00
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);
}
}
}