2 Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/
7 <title>Indexed Database Test
</title>
9 <script type=
"text/javascript">
10 function report(result
) {
11 var message
= { source
: "iframe" };
12 message
.result
= result
;
13 window
.parent
.postMessage(message
, "*");
16 function runIndexedDBTest() {
19 // Create the data-store
20 function createDatastore() {
22 var request
= indexedDB
.open(window
.location
.pathname
, 1);
23 request
.onupgradeneeded = function(event
) {
24 event
.target
.result
.createObjectStore("foo");
26 request
.onsuccess = function(event
) {
27 db
= event
.target
.result
;
32 dump("EXCEPTION IN CREATION: " + e
+ "\n " + e
.stack
+ "\n");
37 function createAndStoreBlob() {
38 const BLOB_DATA
= ["fun ", "times ", "all ", "around!"];
39 var blob
= new Blob(BLOB_DATA
, { type
: "text/plain" });
40 var objectStore
= db
.transaction("foo", "readwrite").objectStore("foo");
41 objectStore
.add({ blob
}, 42).onsuccess
= refetchBlob
;
44 function refetchBlob() {
45 var foo
= db
.transaction("foo").objectStore("foo");
46 foo
.get(42).onsuccess
= fetchedBlobCreateWorkerAndSendBlob
;
49 function fetchedBlobCreateWorkerAndSendBlob(event
) {
50 var idbBlob
= event
.target
.result
.blob
;
51 var compositeBlob
= new Blob(["I like the following blob: ", idbBlob
],
52 { type
: "text/fancy" });
54 function workerScript() {
55 /* eslint-env worker */
56 onmessage = function(event
) {
57 // Save the Blob to the worker's global scope.
58 self
.holdOntoBlob
= event
.data
;
59 // Send any message so we can serialize and keep our runtime behaviour
61 postMessage("kung fu death grip established");
66 URL
.createObjectURL(new Blob(["(", workerScript
.toString(), ")()"]));
68 // Keep a reference to the worker on the window.
69 var worker
= window
.worker
= new Worker(url
);
70 worker
.postMessage(compositeBlob
);
71 worker
.onmessage
= workerLatchedBlobDeleteFromDB
;
74 function workerLatchedBlobDeleteFromDB() {
75 // Delete the reference to the Blob from the database leaving the worker
76 // thread reference as the only live reference once a GC has cleaned
77 // out our references that we sent to the worker. The page that owns
78 // us triggers a GC just for that reason.
79 var objectStore
= db
.transaction("foo", "readwrite").objectStore("foo");
80 objectStore
.delete(42).onsuccess
= closeDBTellOwningThread
;
83 function closeDBTellOwningThread() {
84 // Now that worker has latched the blob, clean up the database.
96 <body onload=
"runIndexedDBTest();">