{"version":3,"file":"EmptyRootInstrumentation.cjs","names":["EmbraceInstrumentationBase"],"sources":["../../../../src/instrumentations/empty-root/EmptyRootInstrumentation/EmptyRootInstrumentation.ts"],"sourcesContent":["import { EmbraceInstrumentationBase } from '../../EmbraceInstrumentationBase/index.ts';\nimport type { EmptyRootInstrumentationArgs } from './types.ts';\n\n/*\n  EmptyRootInstrumentation observes changes to what is considered the root node of the application and emits a span\n  event on the session part span if it ever finds it empty.\n */\nexport class EmptyRootInstrumentation extends EmbraceInstrumentationBase {\n  private readonly _observer: MutationObserver;\n  private readonly _emptyCheckDelayMs: number;\n  private readonly _rootNode: Node | null;\n\n  public constructor({\n    diag,\n    perf,\n    rootNode,\n    emptyCheckDelayMs = 500,\n  }: EmptyRootInstrumentationArgs) {\n    super({\n      instrumentationName: 'EmptyRootInstrumentation',\n      instrumentationVersion: '1.0.0',\n      diag,\n      perf,\n      config: {},\n    });\n\n    this._rootNode = rootNode;\n    this._emptyCheckDelayMs = emptyCheckDelayMs;\n\n    this._observer = new MutationObserver(\n      (mutationList: MutationRecord[], _observer: MutationObserver) => {\n        this._observerCallback(mutationList);\n      },\n    );\n\n    if (this._config.enabled) {\n      this.enable();\n    }\n  }\n\n  public override enable(): void {\n    if (this._rootNode) {\n      super.enable();\n    } else {\n      this._diag.warn(\n        \"supplied root node was null, this instrumentation won't be enabled\",\n      );\n    }\n  }\n\n  public override onDisable(): void {\n    this._observer.disconnect();\n  }\n\n  public override onEnable(): void {\n    if (this._rootNode) {\n      this._observer.observe(this._rootNode, { childList: true });\n    }\n  }\n\n  protected _observerCallback(mutationList: MutationRecord[]): void {\n    let removedNodesFromRoot = false;\n    let addedNodesToRoot = false;\n\n    for (const mutation of mutationList) {\n      if (mutation.target === this._rootNode) {\n        if (mutation.removedNodes.length > 0) {\n          removedNodesFromRoot = true;\n        }\n\n        if (mutation.addedNodes.length > 0) {\n          addedNodesToRoot = true;\n        }\n      }\n    }\n\n    if (removedNodesFromRoot && !addedNodesToRoot) {\n      this._diag.debug(\n        'root node had child nodes removed without new ones being added, ' +\n          `checking if it's empty in ${this._emptyCheckDelayMs}ms`,\n      );\n\n      window.setTimeout(() => {\n        this._checkForEmptyRootNode();\n      }, this._emptyCheckDelayMs);\n    }\n  }\n\n  protected _checkForEmptyRootNode(): void {\n    if (this._rootNode?.childNodes.length === 0) {\n      this._diag.debug('root node was found to be empty');\n\n      const currentSessionPartSpan =\n        this.userSessionManager.getSessionPartSpan();\n      if (currentSessionPartSpan) {\n        currentSessionPartSpan.addEvent('empty-root-node', {\n          'emb.type': 'ux.empty_root_node',\n        });\n      } else {\n        this._diag.debug('no active session found to emit event on');\n      }\n    }\n  }\n}\n"],"mappings":";;;AAOA,IAAa,2BAAb,cAA8CA,+EAAAA,2BAA2B;CACvE;CACA;CACA;CAEA,YAAmB,EACjB,MACA,MACA,UACA,oBAAoB,OACW;EAC/B,MAAM;GACJ,qBAAqB;GACrB,wBAAwB;GACxB;GACA;GACA,QAAQ,CAAC;EACX,CAAC;EAED,KAAK,YAAY;EACjB,KAAK,qBAAqB;EAE1B,KAAK,YAAY,IAAI,kBAClB,cAAgC,cAAgC;GAC/D,KAAK,kBAAkB,YAAY;EACrC,CACF;EAEA,IAAI,KAAK,QAAQ,SACf,KAAK,OAAO;CAEhB;CAEA,SAA+B;EAC7B,IAAI,KAAK,WACP,MAAM,OAAO;OAEb,KAAK,MAAM,KACT,oEACF;CAEJ;CAEA,YAAkC;EAChC,KAAK,UAAU,WAAW;CAC5B;CAEA,WAAiC;EAC/B,IAAI,KAAK,WACP,KAAK,UAAU,QAAQ,KAAK,WAAW,EAAE,WAAW,KAAK,CAAC;CAE9D;CAEA,kBAA4B,cAAsC;EAChE,IAAI,uBAAuB;EAC3B,IAAI,mBAAmB;EAEvB,KAAK,MAAM,YAAY,cACrB,IAAI,SAAS,WAAW,KAAK,WAAW;GACtC,IAAI,SAAS,aAAa,SAAS,GACjC,uBAAuB;GAGzB,IAAI,SAAS,WAAW,SAAS,GAC/B,mB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