OLD | NEW |
(Empty) | |
| 1 var port; |
| 2 self.addEventListener('message', function(e) { |
| 3 var message = e.data; |
| 4 if ('port' in message) { |
| 5 port = message.port; |
| 6 doIndexedDBTest(); |
| 7 } |
| 8 }); |
| 9 function send(action, text) { |
| 10 if (port) port.postMessage({action: action, text: text}); |
| 11 } |
| 12 |
| 13 function evalAndLog(s) { |
| 14 send('log', s); |
| 15 try { |
| 16 return eval(s); |
| 17 } catch (ex) { |
| 18 send('fail', 'EXCEPTION: ' + ex); |
| 19 throw ex; |
| 20 } |
| 21 } |
| 22 |
| 23 function debug(s) { |
| 24 send('log', s); |
| 25 } |
| 26 |
| 27 function doIndexedDBTest() { |
| 28 debug('Preparing the database in the service worker'); |
| 29 var request = evalAndLog("indexedDB.deleteDatabase('db')"); |
| 30 request.onsuccess = function() { |
| 31 request = evalAndLog("indexedDB.open('db')"); |
| 32 request.onupgradeneeded = function() { |
| 33 db = request.result; |
| 34 evalAndLog("db.createObjectStore('store')"); |
| 35 }; |
| 36 request.onsuccess = function() { |
| 37 db = request.result; |
| 38 evalAndLog("tx = db.transaction('store', 'readwrite')"); |
| 39 evalAndLog("store = tx.objectStore('store')"); |
| 40 evalAndLog("store.put('value', 'key')"); |
| 41 tx.oncomplete = function() { |
| 42 send('quit'); |
| 43 }; |
| 44 }; |
| 45 }; |
| 46 } |
OLD | NEW |