{"version":3,"file":"SpanSessionBrowserActivityInstrumentation.cjs","names":["EmbraceInstrumentationBase","throttle","EVENT_THROTTLING_TIME_WINDOW","WINDOW_USER_EVENTS","TIMEOUT_TIME"],"sources":["../../../../src/instrumentations/session/SpanSessionBrowserActivityInstrumentation/SpanSessionBrowserActivityInstrumentation.ts"],"sourcesContent":["import type { TimeoutRef } from '../../../utils/index.ts';\nimport {\n  bulkAddEventListener,\n  bulkRemoveEventListener,\n  throttle,\n} from '../../../utils/index.ts';\nimport { EmbraceInstrumentationBase } from '../../EmbraceInstrumentationBase/index.ts';\nimport {\n  EVENT_THROTTLING_TIME_WINDOW,\n  TIMEOUT_TIME,\n  WINDOW_USER_EVENTS,\n} from './constants.ts';\nimport type { SpanSessionBrowserActivityInstrumentationArgs } from './types.ts';\n\n/**\n *  SpanSessionBrowserActivityInstrumentation will track the user activity and end the session span if there is no\n *  activity for a certain amount of time.\n *  SpanSessionBrowserActivityInstrumentation will initialize new sessions if new activity is detected and there is no\n *  active session.\n * */\nexport class SpanSessionBrowserActivityInstrumentation extends EmbraceInstrumentationBase {\n  private readonly _onActivityThrottled: () => void;\n  private _activityTimeout: TimeoutRef | null;\n\n  public constructor({\n    diag,\n  }: SpanSessionBrowserActivityInstrumentationArgs = {}) {\n    super({\n      instrumentationName: 'SpanSessionBrowserActivityInstrumentation',\n      instrumentationVersion: '1.0.0',\n      diag,\n      config: {},\n    });\n    this._activityTimeout = null;\n    this._onActivityThrottled = throttle(\n      this._onActivity,\n      EVENT_THROTTLING_TIME_WINDOW,\n    );\n    if (this._config.enabled) {\n      this.enable();\n    }\n  }\n\n  public disable = () => {\n    bulkRemoveEventListener({\n      target: window,\n      events: WINDOW_USER_EVENTS,\n      callback: this._onActivityThrottled,\n    });\n    if (this._activityTimeout) {\n      clearTimeout(this._activityTimeout);\n    }\n    this._activityTimeout = null;\n  };\n\n  public enable = () => {\n    bulkAddEventListener({\n      target: window,\n      events: WINDOW_USER_EVENTS,\n      callback: this._onActivityThrottled,\n    });\n  };\n\n  private readonly _onInactivity = () => {\n    this._diag.debug('Inactivity detected');\n    if (this._activityTimeout) {\n      clearTimeout(this._activityTimeout);\n    }\n    this._activityTimeout = null;\n    this.sessionManager.endSessionSpanInternal('inactivity');\n  };\n\n  private readonly _onActivity = () => {\n    this._diag.debug('Activity detected');\n    if (this._activityTimeout) {\n      clearTimeout(this._activityTimeout);\n    }\n    // if there was no active session, start one\n    if (!this.sessionManager.getSessionId()) {\n      this.sessionManager.startSessionSpan({ reason: 'activity' });\n    }\n    this._activityTimeout = setTimeout(this._onInactivity, TIMEOUT_TIME);\n  };\n}\n"],"mappings":";;;;;;;;;;;;;AAoBA,IAAa,4CAAb,cAA+DA,+EAAAA,2BAA2B;CACxF;CACA;CAEA,YAAmB,EACjB,SACiD,EAAE,EAAE;AACrD,QAAM;GACJ,qBAAqB;GACrB,wBAAwB;GACxB;GACA,QAAQ,EAAE;GACX,CAAC;AACF,OAAK,mBAAmB;AACxB,OAAK,uBAAuBC,uBAAAA,SAC1B,KAAK,aACLC,qFAAAA,6BACD;AACD,MAAI,KAAK,QAAQ,QACf,MAAK,QAAQ;;CAIjB,gBAAuB;AACrB,gEAAA,wBAAwB;GACtB,QAAQ;GACR,QAAQC,qFAAAA;GACR,UAAU,KAAK;GAChB,CAAC;AACF,MAAI,KAAK,iBACP,cAAa,KAAK,iBAAiB;AAErC,OAAK,mBAAmB;;CAG1B,eAAsB;AACpB,0DAAA,qBAAqB;GACnB,QAAQ;GACR,QAAQA,qFAAAA;GACR,UAAU,KAAK;GAChB,CAAC;;CAGJ,sBAAuC;AACrC,OAAK,MAAM,MAAM,sBAAsB;AACvC,MAAI,KAAK,iBACP,cAAa,KAAK,iBAAiB;AAErC,OAAK,mBAAmB;AACxB,OAAK,eAAe,uBAAuB,aAAa;;CAG1D,oBAAqC;AACnC,OAAK,MAAM,MAAM,oBAAoB;AACrC,MAAI,KAAK,iBACP,cAAa,KAAK,iBAAiB;AAGrC,MAAI,CAAC,KAAK,eAAe,cAAc,CACrC,MAAK,eAAe,iBAAiB,EAAE,QAAQ,YAAY,CAAC;AAE9D,OAAK,mBAAmB,WAAW,KAAK,eAAeC,qFAAAA,aAAa"}