OLD | NEW |
---|---|
(Empty) | |
1 window.indexedDB = window.indexedDB || window.webkitIndexedDB || | |
2 window.mozIndexedDB || window.msIndexedDB; | |
3 window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || | |
jsbell
2012/06/21 00:04:16
Shouldn't need to reference IDBTransaction directl
ericu
2012/07/11 18:04:35
Removed.
| |
4 window.mozIDBTransaction || window.msIDBTransaction; | |
5 | |
6 var automation = { | |
7 results: {} | |
8 }; | |
9 | |
10 automation.setDone = function() { | |
11 this.setStatus("Test complete."); | |
12 document.cookie = '__done=1; path=/'; | |
13 }; | |
14 | |
15 automation.addResult = function(name, result) { | |
16 result = "" + result; | |
17 this.results[name] = result; | |
18 var elt = document.getElementById('results'); | |
19 var div = document.createElement('div'); | |
20 div.innerText = name + ": " + result; | |
21 elt.appendChild(div); | |
22 }; | |
23 | |
24 automation.getResults = function() { | |
25 return this.results; | |
26 }; | |
27 | |
28 automation.setStatus = function(s) { | |
29 document.getElementById('status').innerText = s; | |
30 }; | |
31 | |
32 function assert(t) { | |
33 if (!t) { | |
34 var e = new Error("Assertion failed!"); | |
35 console.log(e.stack); | |
36 throw e; | |
37 } | |
38 } | |
39 | |
40 function onError(e) { | |
41 console.log(e); | |
42 throw new Error(e); | |
43 } | |
44 | |
45 var version = 2; // The version with our object stores. | |
46 var db; | |
47 | |
48 function createDatabase(name, objectStores, handler, errorHandler) { | |
49 var os = objectStores; | |
jsbell
2012/06/21 00:04:16
What's this extra object reference for?
ericu
2012/07/11 18:04:35
Removed; I was thinking I'd need it to get the clo
| |
50 var openRequest = indexedDB.open(name, version); | |
51 openRequest.onblocked = errorHandler; | |
52 function createObjectStores(db) { | |
53 for (var store in os) { | |
54 var name = os[store]; | |
55 assert(!db.objectStoreNames.contains(name)); | |
56 db.createObjectStore(name); | |
57 } | |
58 } | |
59 openRequest.onupgradeneeded = function(ev) { | |
60 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, | |
61 // and isn't yet tested, as this function won't currently be called. | |
62 assert(openRequest == ev.target); | |
63 db = openRequest.result; | |
64 createObjectStores(db); | |
65 // onsuccess will get called after this exits. | |
66 }; | |
67 openRequest.onsuccess = function(ev) { | |
68 assert(openRequest == ev.target); | |
69 db = openRequest.result; | |
70 db.onerror = function(ev) { | |
71 console.log("db error", arguments, openRequest.webkitErrorMessage); | |
72 errorHandler(); | |
73 } | |
74 if (db.version != version) { | |
75 // This is the current Chrome path. | |
76 var setVersionRequest = db.setVersion(version); | |
77 setVersionRequest.onfailure = errorHandler; | |
78 setVersionRequest.onsuccess = | |
79 function(e) { | |
80 assert(setVersionRequest == e.target); | |
81 createObjectStores(db); | |
82 var versionTransaction = setVersionRequest.result; | |
83 versionTransaction.oncomplete = function() {handler(db); }; | |
84 versionTransaction.onerror = onError; | |
85 } | |
86 } else { | |
87 handler(db); | |
88 } | |
89 } | |
90 } | |
91 | |
92 function getTransaction(db, objectStoreNames, mode, opt_handler) { | |
93 var transaction = db.transaction(objectStoreNames, mode); | |
94 transaction.onerror = onError; | |
jsbell
2012/06/21 00:04:16
FYI, transaction.onerror sees errors bubbled up fr
ericu
2012/07/11 18:04:35
I added a stopPropagation in onError. In generall
| |
95 transaction.onabort = onError; | |
96 if (opt_handler) { | |
97 transaction.oncomplete = opt_handler; | |
98 } | |
99 return transaction; | |
100 } | |
101 | |
102 function deleteDatabase(name, opt_handler) { | |
103 var deleteRequest = indexedDB.deleteDatabase(name); | |
104 deleteRequest.onerror = onError; | |
105 if (opt_handler) { | |
106 deleteRequest.onsuccess = opt_handler; | |
107 } | |
108 } | |
109 | |
110 function cleanUp(opt_handler) { | |
111 if (db) { | |
112 deleteDatabase(db, opt_handler); | |
113 db = null; | |
114 } | |
115 } | |
116 | |
117 function stringOfLength(n) { | |
118 assert(n > 0); | |
119 assert(n == Math.floor(n)); | |
120 return new Array(n + 1).join('0'); | |
121 } | |
OLD | NEW |