{"version":3,"file":"SpanSessionTimeoutInstrumentation.cjs","names":["EmbraceInstrumentationBase","TIMEOUT_TIME"],"sources":["../../../../src/instrumentations/session/SpanSessionTimeoutInstrumentation/SpanSessionTimeoutInstrumentation.ts"],"sourcesContent":["import type { TimeoutRef } from '../../../utils/index.ts';\nimport { EmbraceInstrumentationBase } from '../../EmbraceInstrumentationBase/index.ts';\nimport { TIMEOUT_TIME } from './constants.ts';\nimport type { SpanSessionTimeoutInstrumentationArgs } from './types.ts';\n\n/**\n *  SpanSessionTimeoutInstrumentation will track how long has a session been active and end it after a certain amount of time.\n *  It will immediately start a new session after the previous one ends.\n **/\nexport class SpanSessionTimeoutInstrumentation extends EmbraceInstrumentationBase {\n  private _sessionTimeout: TimeoutRef | null;\n\n  public constructor({\n    diag,\n    perf,\n  }: SpanSessionTimeoutInstrumentationArgs = {}) {\n    super({\n      instrumentationName: 'SpanSessionTimeoutInstrumentation',\n      instrumentationVersion: '1.0.0',\n      diag,\n      perf,\n      config: {},\n    });\n    this._sessionTimeout = null;\n    if (this._config.enabled) {\n      this.enable();\n    }\n  }\n\n  public disable = () => {\n    if (this._sessionTimeout) {\n      clearTimeout(this._sessionTimeout);\n    }\n    this._sessionTimeout = null;\n  };\n\n  public enable = () => {\n    this._checkTimeout();\n  };\n\n  private readonly _onTimeout = () => {\n    this._diag.debug('Timeout detected');\n    // clear existing timeout\n    if (this._sessionTimeout) {\n      clearTimeout(this._sessionTimeout);\n    }\n    this.sessionManager.endSessionSpanInternal('timer');\n    this.sessionManager.startSessionSpan({ reason: 'timer' });\n    // set a new check in TIMEOUT_TIME for the session we just started\n    this._sessionTimeout = setTimeout(this._checkTimeout, TIMEOUT_TIME);\n  };\n\n  private readonly _checkTimeout = () => {\n    const currentSessionStartTime = this.sessionManager.getSessionStartTime();\n    // validate that there is an active session, as it may already been finished for other reasons.\n    if (currentSessionStartTime) {\n      // check how much time has passed since the session started\n      const timePassed = this.perf.millisSinceHRTime(currentSessionStartTime);\n      const remainingTime = TIMEOUT_TIME - timePassed;\n      // if the remaining time is 0 or less, the session has already timed out.\n      if (remainingTime <= 0) {\n        // the session has already timed out.\n        this._onTimeout();\n        return;\n      }\n      // if there is time remaining, set a new timeout for check again after it\n      if (this._sessionTimeout) {\n        clearTimeout(this._sessionTimeout);\n      }\n      this._sessionTimeout = setTimeout(this._checkTimeout, remainingTime);\n      return;\n    }\n    // if there is no currentSessionStartTime then there is no active session,\n    //  check again in TIMEOUT_TIME in case a new one is started.\n    if (this._sessionTimeout) {\n      clearTimeout(this._sessionTimeout);\n    }\n    this._sessionTimeout = setTimeout(this._checkTimeout, TIMEOUT_TIME);\n  };\n}\n"],"mappings":";;;;;;;;AASA,IAAa,oCAAb,cAAuDA,+EAAAA,2BAA2B;CAChF;CAEA,YAAmB,EACjB,MACA,SACyC,EAAE,EAAE;AAC7C,QAAM;GACJ,qBAAqB;GACrB,wBAAwB;GACxB;GACA;GACA,QAAQ,EAAE;GACX,CAAC;AACF,OAAK,kBAAkB;AACvB,MAAI,KAAK,QAAQ,QACf,MAAK,QAAQ;;CAIjB,gBAAuB;AACrB,MAAI,KAAK,gBACP,cAAa,KAAK,gBAAgB;AAEpC,OAAK,kBAAkB;;CAGzB,eAAsB;AACpB,OAAK,eAAe;;CAGtB,mBAAoC;AAClC,OAAK,MAAM,MAAM,mBAAmB;AAEpC,MAAI,KAAK,gBACP,cAAa,KAAK,gBAAgB;AAEpC,OAAK,eAAe,uBAAuB,QAAQ;AACnD,OAAK,eAAe,iBAAiB,EAAE,QAAQ,SAAS,CAAC;AAEzD,OAAK,kBAAkB,WAAW,KAAK,eAAeC,6EAAAA,aAAa;;CAGrE,sBAAuC;EACrC,MAAM,0BAA0B,KAAK,eAAe,qBAAqB;AAEzE,MAAI,yBAAyB;GAG3B,MAAM,gBAAgBA,6EAAAA,eADH,KAAK,KAAK,kBAAkB,wBAAwB;AAGvE,OAAI,iBAAiB,GAAG;AAEtB,SAAK,YAAY;AACjB;;AAGF,OAAI,KAAK,gBACP,cAAa,KAAK,gBAAgB;AAEpC,QAAK,kBAAkB,WAAW,KAAK,eAAe,cAAc;AACpE;;AAIF,MAAI,KAAK,gBACP,cAAa,KAAK,gBAAgB;AAEpC,OAAK,kBAAkB,WAAW,KAAK,eAAeA,6EAAAA,aAAa"}