@embrace-io/web-sdk
Version:
58 lines (57 loc) • 2.2 kB
JavaScript
import { EmbraceInstrumentationBase } from "../../EmbraceInstrumentationBase/EmbraceInstrumentationBase.js";
//#region src/instrumentations/empty-root/EmptyRootInstrumentation/EmptyRootInstrumentation.ts
var EmptyRootInstrumentation = class extends EmbraceInstrumentationBase {
_observer;
_emptyCheckDelayMs;
_rootNode;
constructor({ diag, perf, rootNode, emptyCheckDelayMs = 500 }) {
super({
instrumentationName: "EmptyRootInstrumentation",
instrumentationVersion: "1.0.0",
diag,
perf,
config: {}
});
this._rootNode = rootNode;
this._emptyCheckDelayMs = emptyCheckDelayMs;
this._observer = new MutationObserver((mutationList, _observer) => {
this._observerCallback(mutationList);
});
if (this._config.enabled) this.enable();
}
enable() {
if (this._rootNode) super.enable();
else this._diag.warn("supplied root node was null, this instrumentation won't be enabled");
}
onDisable() {
this._observer.disconnect();
}
onEnable() {
if (this._rootNode) this._observer.observe(this._rootNode, { childList: true });
}
_observerCallback(mutationList) {
let removedNodesFromRoot = false;
let addedNodesToRoot = false;
for (const mutation of mutationList) if (mutation.target === this._rootNode) {
if (mutation.removedNodes.length > 0) removedNodesFromRoot = true;
if (mutation.addedNodes.length > 0) addedNodesToRoot = true;
}
if (removedNodesFromRoot && !addedNodesToRoot) {
this._diag.debug(`root node had child nodes removed without new ones being added, checking if it's empty in ${this._emptyCheckDelayMs}ms`);
window.setTimeout(() => {
this._checkForEmptyRootNode();
}, this._emptyCheckDelayMs);
}
}
_checkForEmptyRootNode() {
if (this._rootNode?.childNodes.length === 0) {
this._diag.debug("root node was found to be empty");
const currentSessionPartSpan = this.userSessionManager.getSessionPartSpan();
if (currentSessionPartSpan) currentSessionPartSpan.addEvent("empty-root-node", { "emb.type": "ux.empty_root_node" });
else this._diag.debug("no active session found to emit event on");
}
}
};
//#endregion
export { EmptyRootInstrumentation };
//# sourceMappingURL=EmptyRootInstrumentation.js.map