signal-desktop/js/storage.js

131 lines
2.7 KiB
JavaScript
Raw Normal View History

/* global _ */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
let ready = false;
2018-10-18 01:01:21 +00:00
let items;
let callbacks = [];
reset();
async function put(key, value) {
if (value === undefined) {
throw new Error('Tried to store undefined');
}
if (!ready) {
window.log.warn('Called storage.put before storage is ready. key:', key);
}
const data = { id: key, value };
items[key] = data;
await window.Signal.Data.createOrUpdateItem(data);
if (_.has(window, ['reduxActions', 'items', 'putItemExternal'])) {
window.reduxActions.items.putItemExternal(key, value);
}
2018-10-18 01:01:21 +00:00
}
function get(key, defaultValue) {
if (!ready) {
window.log.warn('Called storage.get before storage is ready. key:', key);
}
const item = items[key];
if (!item) {
return defaultValue;
}
return item.value;
}
async function remove(key) {
if (!ready) {
2019-07-08 20:29:45 +00:00
window.log.warn(
'Called storage.remove before storage is ready. key:',
key
);
2018-10-18 01:01:21 +00:00
}
delete items[key];
await window.Signal.Data.removeItemById(key);
if (_.has(window, ['reduxActions', 'items', 'removeItemExternal'])) {
window.reduxActions.items.removeItemExternal(key);
}
2018-10-18 01:01:21 +00:00
}
function onready(callback) {
if (ready) {
callback();
} else {
callbacks.push(callback);
}
}
function callListeners() {
if (ready) {
callbacks.forEach(callback => callback());
callbacks = [];
}
}
async function fetch() {
this.reset();
const array = await window.Signal.Data.getAllItems();
for (let i = 0, max = array.length; i < max; i += 1) {
const item = array[i];
const { id } = item;
items[id] = item;
}
2018-04-27 21:25:04 +00:00
ready = true;
2018-10-18 01:01:21 +00:00
callListeners();
}
function getItemsState() {
const data = _.clone(items);
const ids = Object.keys(data);
ids.forEach(id => {
data[id] = data[id].value;
});
return data;
}
2018-10-18 01:01:21 +00:00
function reset() {
ready = false;
items = Object.create(null);
}
const storage = {
fetch,
put,
get,
getItemsState,
2018-10-18 01:01:21 +00:00
remove,
onready,
reset,
2018-04-27 21:25:04 +00:00
};
2018-10-18 01:01:21 +00:00
// Keep a reference to this storage system, since there are scenarios where
// we need to replace it with the legacy storage system for a while.
window.newStorage = storage;
2018-04-27 21:25:04 +00:00
window.textsecure = window.textsecure || {};
window.textsecure.storage = window.textsecure.storage || {};
2018-10-18 01:01:21 +00:00
window.installStorage = newStorage => {
window.storage = newStorage;
window.textsecure.storage.impl = newStorage;
};
window.installStorage(storage);
})();