OLD | NEW |
1 var port; | |
2 self.addEventListener('message', function(e) { | 1 self.addEventListener('message', function(e) { |
3 var message = e.data; | 2 var message = e.data; |
4 if ('port' in message) | 3 if (message.action === 'create') { |
5 doIndexedDBTest(message.port); | 4 e.waitUntil(deleteDB() |
| 5 .then(doIndexedDBTest) |
| 6 .then(function() { |
| 7 message.port.postMessage({ type: 'created' }); |
| 8 }) |
| 9 .catch(function(reason) { |
| 10 message.port.postMessage({ type: 'error', value: reason }); |
| 11 })); |
| 12 } else if (message.action === 'cleanup') { |
| 13 e.waitUntil(deleteDB() |
| 14 .then(function() { |
| 15 message.port.postMessage({ type: 'done' }); |
| 16 }) |
| 17 .catch(function(reason) { |
| 18 message.port.postMessage({ type: 'error', value: reason }); |
| 19 })); |
| 20 } |
6 }); | 21 }); |
7 | 22 |
| 23 function deleteDB() { |
| 24 return new Promise(function(resolve, reject) { |
| 25 var delete_request = indexedDB.deleteDatabase('db'); |
| 26 |
| 27 delete_request.onsuccess = resolve; |
| 28 delete_request.onerror = reject; |
| 29 }); |
| 30 } |
| 31 |
8 function doIndexedDBTest(port) { | 32 function doIndexedDBTest(port) { |
9 var delete_request = indexedDB.deleteDatabase('db'); | 33 return new Promise(function(resolve, reject) { |
10 delete_request.onsuccess = function() { | 34 var open_request = indexedDB.open('db'); |
11 var open_request = indexedDB.open('db'); | 35 |
12 open_request.onupgradeneeded = function() { | 36 open_request.onerror = reject; |
13 var db = open_request.result; | 37 open_request.onupgradeneeded = function() { |
14 db.createObjectStore('store'); | 38 var db = open_request.result; |
15 }; | 39 db.createObjectStore('store'); |
16 open_request.onsuccess = function() { | |
17 var db = open_request.result; | |
18 var tx = db.transaction('store', 'readwrite'); | |
19 var store = tx.objectStore('store'); | |
20 store.put('value', 'key'); | |
21 tx.oncomplete = function() { | |
22 port.postMessage('done'); | |
23 }; | 40 }; |
24 }; | 41 open_request.onsuccess = function() { |
25 }; | 42 var db = open_request.result; |
| 43 var tx = db.transaction('store', 'readwrite'); |
| 44 var store = tx.objectStore('store'); |
| 45 store.put('value', 'key'); |
| 46 |
| 47 tx.onerror = function() { |
| 48 db.close(); |
| 49 reject(tx.error); |
| 50 }; |
| 51 tx.oncomplete = function() { |
| 52 db.close(); |
| 53 resolve(); |
| 54 }; |
| 55 }; |
| 56 }); |
26 } | 57 } |
OLD | NEW |