1 | {"version":3,"sources":["../../src/util/simplePersistence.js"],"names":["NamespacedLocalStorage","constructor","localStorage","key","_makeKey","getItem","name","setItem","value","removeItem","BrowserPersistence","storage","window","KEY","now","Date","item","undefined","ttl","timeStored","JSON","parse","stringify"],"mappings":";;AAAA;;;AAIA,MAAMA,sBAAN,CAA6B;AACzBC,EAAAA,WAAW,CAACC,YAAD,EAAeC,GAAf,EAAoB;AAC3B,SAAKD,YAAL,GAAoBA,YAApB;AACA,SAAKC,GAAL,GAAWA,GAAX;AACH;;AACDC,EAAAA,QAAQ,CAACD,GAAD,EAAM;AACV,WAAQ,GAAE,KAAKA,GAAI,KAAIA,GAAI,EAA3B;AACH;;AACDE,EAAAA,OAAO,CAACC,IAAD,EAAO;AACV,WAAO,KAAKJ,YAAL,CAAkBG,OAAlB,CAA0B,KAAKD,QAAL,CAAcE,IAAd,CAA1B,CAAP;AACH;;AACDC,EAAAA,OAAO,CAACD,IAAD,EAAOE,KAAP,EAAc;AACjB,WAAO,KAAKN,YAAL,CAAkBK,OAAlB,CAA0B,KAAKH,QAAL,CAAcE,IAAd,CAA1B,EAA+CE,KAA/C,CAAP;AACH;;AACDC,EAAAA,UAAU,CAACH,IAAD,EAAO;AACb,WAAO,KAAKJ,YAAL,CAAkBO,UAAlB,CAA6B,KAAKL,QAAL,CAAcE,IAAd,CAA7B,CAAP;AACH;;AAhBwB;;AAmB7B,eAAe,MAAMI,kBAAN,CAAyB;AAEpCT,EAAAA,WAAW,GAAG;AACV,SAAKU,OAAL,GAAe,IAAIX,sBAAJ,CACXY,MAAM,CAACV,YADI,EAEX,KAAKD,WAAL,CAAiBY,GAAjB,IAAwBH,kBAAkB,CAACG,GAFhC,CAAf;AAIH;;AACDR,EAAAA,OAAO,CAACC,IAAD,EAAO;AACV,UAAMQ,GAAG,GAAGC,IAAI,CAACD,GAAL,EAAZ;AACA,UAAME,IAAI,GAAG,KAAKL,OAAL,CAAaN,OAAb,CAAqBC,IAArB,CAAb;;AACA,QAAI,CAACU,IAAL,EAAW;AACP,aAAOC,SAAP;AACH;;AACD,UAAM;AAAET,MAAAA,KAAF;AAASU,MAAAA,GAAT;AAAcC,MAAAA;AAAd,QAA6BC,IAAI,CAACC,KAAL,CAAWL,IAAX,CAAnC;;AACA,QAAIE,GAAG,IAAIJ,GAAG,GAAGK,UAAN,GAAmBD,GAAG,GAAG,IAApC,EAA0C;AACtC,WAAKP,OAAL,CAAaF,UAAb,CAAwBH,IAAxB;AACA,aAAOW,SAAP;AACH;;AACD,WAAOG,IAAI,CAACC,KAAL,CAAWb,KAAX,CAAP;AACH;;AACDD,EAAAA,OAAO,CAACD,IAAD,EAAOE,KAAP,EAAcU,GAAd,EAAmB;AACtB,UAAMC,UAAU,GAAGJ,IAAI,CAACD,GAAL,EAAnB;AACA,SAAKH,OAAL,CAAaJ,OAAb,CACID,IADJ,EAEIc,IAAI,CAACE,SAAL,CAAe;AACXd,MAAAA,KAAK,EAAEY,IAAI,CAACE,SAAL,CAAed,KAAf,CADI;AAEXW,MAAAA,UAFW;AAGXD,MAAAA;AAHW,KAAf,CAFJ;AAQH;;AACDT,EAAAA,UAAU,CAACH,IAAD,EAAO;AACb,SAAKK,OAAL,CAAaF,UAAb,CAAwBH,IAAxB;AACH;;AAlCmC;;gBAAnBI,kB,SACJ,8B","sourcesContent":["/**\n * Persistence layer with expiration based on localStorage.\n */\n\nclass NamespacedLocalStorage {\n constructor(localStorage, key) {\n this.localStorage = localStorage;\n this.key = key;\n }\n _makeKey(key) {\n return `${this.key}__${key}`;\n }\n getItem(name) {\n return this.localStorage.getItem(this._makeKey(name));\n }\n setItem(name, value) {\n return this.localStorage.setItem(this._makeKey(name), value);\n }\n removeItem(name) {\n return this.localStorage.removeItem(this._makeKey(name));\n }\n}\n\nexport default class BrowserPersistence {\n static KEY = 'M2_VENIA_BROWSER_PERSISTENCE';\n constructor() {\n this.storage = new NamespacedLocalStorage(\n window.localStorage,\n this.constructor.KEY || BrowserPersistence.KEY\n );\n }\n getItem(name) {\n const now = Date.now();\n const item = this.storage.getItem(name);\n if (!item) {\n return undefined;\n }\n const { value, ttl, timeStored } = JSON.parse(item);\n if (ttl && now - timeStored > ttl * 1000) {\n this.storage.removeItem(name);\n return undefined;\n }\n return JSON.parse(value);\n }\n setItem(name, value, ttl) {\n const timeStored = Date.now();\n this.storage.setItem(\n name,\n JSON.stringify({\n value: JSON.stringify(value),\n timeStored,\n ttl\n })\n );\n }\n removeItem(name) {\n this.storage.removeItem(name);\n }\n}\n"],"file":"simplePersistence.js"} |