signal-desktop/libtextsecure/storage.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-07-21 21:51:20 +00:00
/* global window, textsecure, localStorage */
2018-07-21 21:51:20 +00:00
// eslint-disable-next-line func-names
2018-05-02 16:51:22 +00:00
(function() {
2018-07-21 21:51:20 +00:00
/** **********************************************
2018-05-02 16:51:22 +00:00
*** Utilities to store data in local storage ***
2018-07-21 21:51:20 +00:00
*********************************************** */
2018-05-02 16:51:22 +00:00
window.textsecure = window.textsecure || {};
window.textsecure.storage = window.textsecure.storage || {};
// Overrideable storage implementation
window.textsecure.storage.impl = window.textsecure.storage.impl || {
2018-07-21 21:51:20 +00:00
/** ***************************
2018-05-02 16:51:22 +00:00
*** Base Storage Routines ***
2018-07-21 21:51:20 +00:00
**************************** */
put(key, value) {
2018-05-02 16:51:22 +00:00
if (value === undefined) throw new Error('Tried to store undefined');
2018-07-21 21:51:20 +00:00
localStorage.setItem(`${key}`, textsecure.utils.jsonThing(value));
2018-05-02 16:51:22 +00:00
},
2018-07-21 21:51:20 +00:00
get(key, defaultValue) {
const value = localStorage.getItem(`${key}`);
2018-05-02 16:51:22 +00:00
if (value === null) return defaultValue;
return JSON.parse(value);
},
2018-07-21 21:51:20 +00:00
remove(key) {
localStorage.removeItem(`${key}`);
2018-05-02 16:51:22 +00:00
},
};
2018-07-21 21:51:20 +00:00
window.textsecure.storage.put = (key, value) =>
textsecure.storage.impl.put(key, value);
window.textsecure.storage.get = (key, defaultValue) =>
textsecure.storage.impl.get(key, defaultValue);
window.textsecure.storage.remove = key => textsecure.storage.impl.remove(key);
})();