-
Notifications
You must be signed in to change notification settings - Fork 3
/
indexedDbObservers.html
50 lines (45 loc) · 1.37 KB
/
indexedDbObservers.html
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
<!DOCTYPE html>
<script src="indexedDbObservers.js"></script>
<script>
var dbname = 'db' + Date.now();
var req = indexedDB.open(dbname);
var db1;
req.onupgradeneeded = function() {
db1 = req.result;
db1.createObjectStore('s1');
db1.createObjectStore('s2');
}
req.onsuccess = function() {
db1 = req.result;
var tx = db1.transaction(['s1', 's2'], 'readwrite');
var s1 = tx.objectStore('s1');
s1.addChangeListener(function(e) {
console.log('connection 1 saw changes: ' + JSON.stringify(e));
window.e = e;
});
tx.objectStore('s1').put('val1', 'k1');
tx.objectStore('s2').put('val2', 'k2');
tx.objectStore('s1').delete(IDBKeyRange.bound(0, 9999));
};
var req2 = indexedDB.open(dbname);
req2.onsuccess = function() {
var db = req.result;
var tx = db.transaction(['s1'], 'readwrite');
var s1 = tx.objectStore('s1');
s1.addChangeListener(function(e) {
console.log('connection 2 saw changes: ' + JSON.stringify(e));
window.e = e;
db.close();
// check that we don't see it again
var req = indexedDB.open(dbname);
req.onsuccess = function() {
var db = req.result;
var tx = db.transaction(['s1', 's2'], 'readwrite');
var s1 = tx.objectStore('s1');
tx.objectStore('s1').put('val1', 'k1');
tx.objectStore('s2').put('val2', 'k2');
tx.objectStore('s1').delete(IDBKeyRange.bound(0, 9999));
};
});
}
</script>