blob: 9add47683884c04fc22c34337714fd2a356dcac7 (
plain)
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
|
self.addEventListener('message', function(e) {
var message = e.data;
if (message.action === 'create') {
e.waitUntil(deleteDB()
.then(doIndexedDBTest)
.then(function() {
message.port.postMessage({ type: 'created' });
})
.catch(function(reason) {
message.port.postMessage({ type: 'error', value: reason });
}));
} else if (message.action === 'cleanup') {
e.waitUntil(deleteDB()
.then(function() {
message.port.postMessage({ type: 'done' });
})
.catch(function(reason) {
message.port.postMessage({ type: 'error', value: reason });
}));
}
});
function deleteDB() {
return new Promise(function(resolve, reject) {
var delete_request = indexedDB.deleteDatabase('db');
delete_request.onsuccess = resolve;
delete_request.onerror = reject;
});
}
function doIndexedDBTest(port) {
return new Promise(function(resolve, reject) {
var open_request = indexedDB.open('db');
open_request.onerror = reject;
open_request.onupgradeneeded = function() {
var db = open_request.result;
db.createObjectStore('store');
};
open_request.onsuccess = function() {
var db = open_request.result;
var tx = db.transaction('store', 'readwrite');
var store = tx.objectStore('store');
store.put('value', 'key');
tx.onerror = function() {
db.close();
reject(tx.error);
};
tx.oncomplete = function() {
db.close();
resolve();
};
};
});
}
|