forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.js
63 lines (58 loc) · 1.73 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* vim: ts=4:sw=4:expandtab
*/
;(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) {
if (value === undefined)
throw new Error("Tried to store undefined");
var item = items.add({id: key, value: value});
item.save();
},
get: function(key, defaultValue) {
var item = items.get("" + key);
if (!item)
return defaultValue;
return item.get('value');
},
remove: function(key) {
var item = items.get("" + key);
if (item) {
items.remove(item);
item.destroy();
}
},
onready: function(callback) {
if (ready) {
callback();
} else {
items.on('reset', callback);
}
},
fetch: function() {
return new Promise(function(resolve) {
items.fetch({reset: true}).always(resolve);
});
}
};
window.textsecure = window.textsecure || {};
window.textsecure.storage = window.textsecure.storage || {};
window.textsecure.storage.impl = window.storage;
})();