2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-05-12 22:14:20 +00:00
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
var Item = Backbone.Model.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'items'
|
|
|
|
});
|
|
|
|
var ItemCollection = Backbone.Collection.extend({
|
|
|
|
model: Item,
|
|
|
|
storeName: 'items',
|
|
|
|
database: Whisper.Database,
|
|
|
|
});
|
|
|
|
|
|
|
|
var ready = false;
|
|
|
|
var items = new ItemCollection();
|
|
|
|
items.on('reset', function() { ready = true; });
|
|
|
|
window.storage = {
|
|
|
|
/*****************************
|
|
|
|
*** Base Storage Routines ***
|
|
|
|
*****************************/
|
|
|
|
put: function(key, value) {
|
2016-02-18 01:08:50 +00:00
|
|
|
if (value === undefined) {
|
2015-05-12 22:14:20 +00:00
|
|
|
throw new Error("Tried to store undefined");
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2016-02-26 21:11:00 +00:00
|
|
|
if (!ready) {
|
2016-03-18 04:04:12 +00:00
|
|
|
console.log('Called storage.put before storage is ready. key:', key);
|
2016-02-26 21:11:00 +00:00
|
|
|
}
|
2016-02-18 00:09:06 +00:00
|
|
|
var item = items.add({id: key, value: value}, {merge: true});
|
2017-08-30 16:32:16 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
item.save().then(resolve, reject);
|
|
|
|
});
|
2015-05-12 22:14:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get: function(key, defaultValue) {
|
|
|
|
var item = items.get("" + key);
|
2016-02-18 01:08:50 +00:00
|
|
|
if (!item) {
|
2015-05-12 22:14:20 +00:00
|
|
|
return defaultValue;
|
2016-02-18 01:08:50 +00:00
|
|
|
}
|
2015-05-12 22:14:20 +00:00
|
|
|
return item.get('value');
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function(key) {
|
|
|
|
var item = items.get("" + key);
|
|
|
|
if (item) {
|
|
|
|
items.remove(item);
|
2017-08-30 16:32:16 +00:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
item.destroy().then(resolve, reject);
|
|
|
|
});
|
2015-05-12 22:14:20 +00:00
|
|
|
}
|
2017-08-30 16:32:16 +00:00
|
|
|
return Promise.resolve();
|
2015-05-12 22:14:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onready: function(callback) {
|
|
|
|
if (ready) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
items.on('reset', callback);
|
|
|
|
}
|
2015-05-14 22:44:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetch: function() {
|
2018-03-30 20:54:19 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
items.fetch({reset: true})
|
|
|
|
.fail(() => reject(new Error('Failed to fetch from storage.' +
|
|
|
|
' This may be due to an unexpected database version.')))
|
|
|
|
.always(resolve);
|
2015-05-14 22:44:43 +00:00
|
|
|
});
|
2018-02-24 01:40:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
|
|
|
items.reset();
|
2015-05-12 22:14:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.storage = window.textsecure.storage || {};
|
|
|
|
window.textsecure.storage.impl = window.storage;
|
|
|
|
})();
|