{"version":3,"file":"sessionInMemory.mjs","names":[],"sources":["../../../../src/domain/session/storeStrategies/sessionInMemory.ts"],"sourcesContent":["import { globalObject } from '../../../tools/globalObject'\nimport { Observable } from '../../../tools/observable'\nimport { shallowClone } from '../../../tools/utils/objectUtils'\nimport { SessionPersistence } from '../sessionConstants'\nimport type { SessionState } from '../sessionState'\nimport type { SessionStateOperation, SessionStoreStrategy, SessionStoreStrategyType } from './sessionStoreStrategy'\n\nexport const MEMORY_SESSION_STORE_KEY = '_DD_SESSION'\n\nexport interface MemorySession {\n  state?: SessionState\n  onChange?: (state: SessionState) => void\n}\n\nexport interface GlobalObjectWithSession {\n  [MEMORY_SESSION_STORE_KEY]?: MemorySession\n}\n\nexport function selectMemorySessionStoreStrategy(): SessionStoreStrategyType {\n  return { type: SessionPersistence.MEMORY }\n}\n\nexport function initMemorySessionStoreStrategy(): SessionStoreStrategy {\n  const globalObjectWithSession = globalObject as GlobalObjectWithSession\n\n  // Share the observable across SDK instances (RUM + Logs)\n  if (!globalObjectWithSession[MEMORY_SESSION_STORE_KEY]) {\n    globalObjectWithSession[MEMORY_SESSION_STORE_KEY] = {}\n  }\n  const memorySession = globalObjectWithSession[MEMORY_SESSION_STORE_KEY]\n\n  const sessionObservable = new Observable<SessionState>()\n\n  // Wire the local observable to the shared onChange callback so that\n  // multiple SDK instances (RUM + Logs) can observe each other's changes.\n  const previousOnChange = memorySession.onChange\n  memorySession.onChange = (state: SessionState) => {\n    previousOnChange?.(state)\n    sessionObservable.notify(state)\n  }\n\n  return {\n    async setSessionState(\n      fn: (sessionState: SessionState) => SessionState,\n      _operation: SessionStateOperation\n    ): Promise<void> {\n      const currentState = memorySession.state ?? {}\n      const newState = shallowClone(fn(currentState))\n      memorySession.state = newState\n      await Promise.resolve()\n      memorySession.onChange?.(newState)\n    },\n    sessionObservable,\n  }\n}\n"],"mappings":";;;;;AAOA,MAAa,2BAA2B;AAWxC,SAAgB,mCAA6D;CAC3E,OAAO,EAAE,MAAM,mBAAmB,OAAO;AAC3C;AAEA,SAAgB,iCAAuD;CACrE,MAAM,0BAA0B;CAGhC,IAAI,CAAC,wBAAA,gBACH,wBAAwB,4BAA4B,CAAC;CAEvD,MAAM,gBAAgB,wBAAwB;CAE9C,MAAM,oBAAoB,IAAI,WAAyB;CAIvD,MAAM,mBAAmB,cAAc;CACvC,cAAc,YAAY,UAAwB;EAChD,mBAAmB,KAAK;EACxB,kBAAkB,OAAO,KAAK;CAChC;CAEA,OAAO;EACL,MAAM,gBACJ,IACA,YACe;GAEf,MAAM,WAAW,aAAa,GADT,cAAc,SAAS,CAAC,CACA,CAAC;GAC9C,cAAc,QAAQ;GACtB,MAAM,QAAQ,QAAQ;GACtB,cAAc,WAAW,QAAQ;EACnC;EACA;CACF;AACF"}