{"version":3,"file":"index-CCc4U3ZT.js","sources":["../../../app/assets/javascripts/app/local_storages/native_api_adapter/index.ts"],"sourcesContent":["\nimport _memoize from \"lodash/memoize\"\n\nconst test_val = \"__storage_test__\"\n\n/* eslint-disable no-unused-vars */\nexport enum GetDataOpResultTypes {\n DATA_RETRIEVED = \"DATA_RETRIEVED\",\n\n DATA_NOT_FOUND = \"DATA_NOT_FOUND\",\n FEATURE_DISABLED = \"FEATURE_DISABLED\",\n}\n/* eslint-enable no-unused-vars */\ninterface GetDataResult {\n data: string | null,\n result_type: GetDataOpResultTypes,\n}\n\n/* eslint-disable no-unused-vars */\nexport enum SetDataOpResultTypes {\n DATA_IS_SET = \"DATA_IS_SET\",\n\n FEATURE_DISABLED = \"FEATURE_DISABLED\",\n STORAGE_FULL = \"STORAGE_FULL\",\n UNKNOWN_ERROR = \"UNKNOWN_ERROR\",\n}\n/* eslint-enable no-unused-vars */\nexport interface SetDataResult {\n result_type: SetDataOpResultTypes,\n exception?: Error,\n}\n\n/* eslint-disable no-unused-vars */\nexport enum RemoveDataOpResultTypes {\n DATA_REMOVED = \"DATA_REMOVED\",\n\n FEATURE_DISABLED = \"FEATURE_DISABLED\",\n UNKNOWN_ERROR = \"UNKNOWN_ERROR\",\n}\n/* eslint-enable no-unused-vars */\ninterface RemoveDataResult {\n result_type: RemoveDataOpResultTypes,\n exception?: Error,\n}\n\n\nconst isStorageFull = (e: Error, storage: Storage): boolean => {\n if (storage == null) { return false }\n\n return (((e instanceof DOMException) && (\n // everything except Firefox\n e.code === 22 ||\n // Firefox\n e.code === 1014 ||\n // test name field too, because code might not be present\n // everything except Firefox\n e.name === \"QuotaExceededError\" ||\n // Firefox\n e.name === \"NS_ERROR_DOM_QUOTA_REACHED\") &&\n // acknowledge QuotaExceededError only if there's something already stored\n storage.length !== 0))\n}\n\n\n// Not just supported by browser or not\n// But also enabled by user\n//\n// https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API\nconst isLocalStorageEnabled = _memoize((): boolean => {\n let storage\n try {\n storage = window.localStorage\n }\n catch (e) {\n // If there is error accessing this object\n // Mostly SecurityError\n // Then it's disabled\n return false\n }\n\n if (storage == null) { return false }\n\n try {\n storage.setItem(test_val, test_val)\n storage.removeItem(test_val)\n return true\n }\n catch (e) {\n return (e instanceof DOMException) && (\n // everything except Firefox\n e.code === 22 ||\n // Firefox\n e.code === 1014 ||\n // test name field too, because code might not be present\n // everything except Firefox\n e.name === \"QuotaExceededError\" ||\n // Firefox\n e.name === \"NS_ERROR_DOM_QUOTA_REACHED\") &&\n // acknowledge QuotaExceededError only if there's something already stored\n storage.length !== 0\n }\n})\n\n\nconst getDataFromLocalStorage = (key: string): GetDataResult => {\n if (! isLocalStorageEnabled()) {\n return {\n data: null,\n result_type: GetDataOpResultTypes.FEATURE_DISABLED,\n }\n }\n\n const storage = window.localStorage\n const data = storage.getItem(key)\n if (data === null) {\n return {\n data: null,\n result_type: GetDataOpResultTypes.DATA_NOT_FOUND,\n }\n }\n // It's possible for us to set a `null` or `undefined` into the storage\n // So we handle those strange cases here\n if (data === \"null\" || data === \"undefined\") {\n return {\n data: null,\n result_type: GetDataOpResultTypes.DATA_NOT_FOUND,\n }\n }\n\n return {\n data: data,\n result_type: GetDataOpResultTypes.DATA_RETRIEVED,\n }\n}\n\nconst setDataToLocalStorage = (key: string, data: string | null): SetDataResult => {\n if (! isLocalStorageEnabled()) {\n return {\n result_type: SetDataOpResultTypes.FEATURE_DISABLED,\n }\n }\n\n if (typeof data === \"undefined\") {\n throw new Error(`invalid data \"undefined\" passed in for key <${key}>`)\n }\n\n if (data === null) {\n throw new Error(`invalid data \"null\" passed in for key <${key}>`)\n }\n\n const storage = window.localStorage\n try {\n storage.setItem(key, data)\n return {\n result_type: SetDataOpResultTypes.DATA_IS_SET,\n }\n }\n catch (e) {\n if (isStorageFull(e, storage)) {\n return {\n result_type: SetDataOpResultTypes.STORAGE_FULL,\n exception: e,\n }\n }\n return {\n result_type: SetDataOpResultTypes.UNKNOWN_ERROR,\n exception: e,\n }\n }\n}\n\nconst removeDataFromLocalStorage = (key: string): RemoveDataResult => {\n if (! isLocalStorageEnabled()) {\n return {\n result_type: RemoveDataOpResultTypes.FEATURE_DISABLED,\n }\n }\n\n const storage = window.localStorage\n try {\n storage.removeItem(key)\n return {\n result_type: RemoveDataOpResultTypes.DATA_REMOVED,\n }\n }\n catch (e) {\n return {\n result_type: RemoveDataOpResultTypes.UNKNOWN_ERROR,\n exception: e,\n }\n }\n}\n\n\nexport {\n isLocalStorageEnabled,\n\n getDataFromLocalStorage,\n\n setDataToLocalStorage,\n\n removeDataFromLocalStorage,\n}\n"],"names":["test_val","GetDataOpResultTypes","SetDataOpResultTypes","RemoveDataOpResultTypes","isStorageFull","e","storage","isLocalStorageEnabled","_memoize","getDataFromLocalStorage","key","data","setDataToLocalStorage","removeDataFromLocalStorage"],"mappings":"mCAGA,MAAMA,EAAW,mBAGL,IAAAC,GAAAA,IACVA,EAAA,eAAiB,iBAEjBA,EAAA,eAAoB,iBACpBA,EAAA,iBAAoB,mBAJVA,IAAAA,GAAA,CAAA,CAAA,EAaAC,GAAAA,IACVA,EAAA,YAAc,cAEdA,EAAA,iBAAoB,mBACpBA,EAAA,aAAoB,eACpBA,EAAA,cAAoB,gBALVA,IAAAA,GAAA,CAAA,CAAA,EAcAC,GAAAA,IACVA,EAAA,aAAe,eAEfA,EAAA,iBAAoB,mBACpBA,EAAA,cAAoB,gBAJVA,IAAAA,GAAA,CAAA,CAAA,EAaZ,MAAMC,EAAgB,CAACC,EAAUC,IAC3BA,GAAW,KAAe,GAEpBD,aAAa,eAErBA,EAAE,OAAS,IAEXA,EAAE,OAAS,MAGXA,EAAE,OAAS,sBAEXA,EAAE,OAAS,+BAEXC,EAAQ,SAAW,EAQjBC,EAAwBC,EAAS,IAAe,CAChD,IAAAF,EACA,GAAA,CACFA,EAAU,OAAO,mBAEZ,EAAG,CAID,MAAA,EAAA,CAGT,GAAIA,GAAW,KAAe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