{"version":3,"file":"sessionInLocalStorage.mjs","names":[],"sources":["../../../../src/domain/session/storeStrategies/sessionInLocalStorage.ts"],"sourcesContent":["import { generateUUID } from '../../../tools/utils/stringUtils'\nimport { Observable } from '../../../tools/observable'\nimport { addEventListener } from '../../../browser/addEventListener'\nimport { SessionPersistence } from '../sessionConstants'\nimport type { SessionState } from '../sessionState'\nimport { isSessionInNotStartedState, toSessionString, toSessionState } from '../sessionState'\nimport type { SessionStateOperation, SessionStoreStrategy, SessionStoreStrategyType } from './sessionStoreStrategy'\nimport { SESSION_STORE_KEY, LEGACY_SESSION_STORE_KEY } from './sessionStoreStrategy'\n\nconst LOCAL_STORAGE_TEST_KEY = '_oo_test_'\n\nexport function selectLocalStorageStrategy(): SessionStoreStrategyType | undefined {\n  try {\n    const id = generateUUID()\n    const testKey = `${LOCAL_STORAGE_TEST_KEY}${id}`\n    localStorage.setItem(testKey, id)\n    const retrievedId = localStorage.getItem(testKey)\n    localStorage.removeItem(testKey)\n    return id === retrievedId ? { type: SessionPersistence.LOCAL_STORAGE } : undefined\n  } catch {\n    return undefined\n  }\n}\n\nexport function initLocalStorageStrategy(): SessionStoreStrategy {\n  const sessionObservable = new Observable<SessionState>(\n    (observable) =>\n      addEventListener(window, 'storage', (event) => {\n        if (event.key === SESSION_STORE_KEY && event.storageArea === localStorage) {\n          observable.notify(toSessionState(event.newValue))\n        }\n      }).stop\n  )\n\n  let isFirstCall = true\n\n  return {\n    async setSessionState(\n      fn: (sessionState: SessionState) => SessionState,\n      _operation: SessionStateOperation\n    ): Promise<void> {\n      let currentState = toSessionState(localStorage.getItem(SESSION_STORE_KEY))\n\n      if (isFirstCall && isSessionInNotStartedState(currentState)) {\n        currentState = toSessionState(localStorage.getItem(LEGACY_SESSION_STORE_KEY))\n      }\n      isFirstCall = false\n\n      const newState = fn(currentState)\n      localStorage.setItem(SESSION_STORE_KEY, toSessionString(newState))\n      await Promise.resolve()\n      sessionObservable.notify(newState)\n    },\n    sessionObservable,\n  }\n}\n"],"mappings":";;;;;;;AASA,MAAM,yBAAyB;AAE/B,SAAgB,6BAAmE;CACjF,IAAI;EACF,MAAM,KAAK,aAAa;EACxB,MAAM,UAAU,GAAG,yBAAyB;EAC5C,aAAa,QAAQ,SAAS,EAAE;EAChC,MAAM,cAAc,aAAa,QAAQ,OAAO;EAChD,aAAa,WAAW,OAAO;EAC/B,OAAO,OAAO,cAAc,EAAE,MAAM,mBAAmB,cAAc,IAAI,KAAA;CAC3E,QAAQ;EACN;CACF;AACF;AAEA,SAAgB,2BAAiD;CAC/D,MAAM,oBAAoB,IAAI,YAC3B,eACC,iBAAiB,QAAQ,YAAY,UAAU;EAC7C,IAAI,MAAM,QAAA,cAA6B,MAAM,gBAAgB,cAC3D,WAAW,OAAO,eAAe,MAAM,QAAQ,CAAC;CAEpD,CAAC,CAAC,CAAC,IACP;CAEA,IAAI,cAAc;CAElB,OAAO;EACL,MAAM,gBACJ,IACA,YACe;GACf,IAAI,eAAe,eAAe,aAAa,QAAQ,iBAAiB,CAAC;GAEzE,IAAI,eAAe,2BAA2B,YAAY,GACxD,eAAe,eAAe,aAAa,QAAQ,wBAAwB,CAAC;GAE9E,cAAc;GAEd,MAAM,WAAW,GAAG,YAAY;GAChC,aAAa,QAAQ,mBAAmB,gBAAgB,QAAQ,CAAC;GACjE,MAAM,QAAQ,QAAQ;GACtB,kBAAkB,OAAO,QAAQ;EACnC;EACA;CACF;AACF"}