2019-05-16 22:32:11 +00:00
|
|
|
/* global _ */
|
2018-07-07 00:48:14 +00:00
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2018-07-07 00:48:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2015-05-12 22:14:20 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
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);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
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);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
function getItemsState() {
|
2019-05-29 22:48:43 +00:00
|
|
|
const data = _.clone(items);
|
|
|
|
const ids = Object.keys(data);
|
|
|
|
ids.forEach(id => {
|
|
|
|
data[id] = data[id].value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
function reset() {
|
|
|
|
ready = false;
|
|
|
|
items = Object.create(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = {
|
|
|
|
fetch,
|
|
|
|
put,
|
|
|
|
get,
|
2019-05-16 22:32:11 +00:00
|
|
|
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);
|
2015-05-12 22:14:20 +00:00
|
|
|
})();
|