Skip to content

Instantly share code, notes, and snippets.

@bemson
Forked from nolanlawson/index.html
Last active October 21, 2021 11:55
Show Gist options
  • Save bemson/57dffb89ee7b28d63a29 to your computer and use it in GitHub Desktop.
Save bemson/57dffb89ee7b28d63a29 to your computer and use it in GitHub Desktop.
IndexedDB with Web Worker Blobs
<html>
<body>
<span id="output"></span>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="main.js"></script>
</html>
var
workerBlob = new Blob(
[workerRunner.toString().replace(/^function .+\{?|\}$/g, '')],
{ type:'text/javascript' }
),
workerBlobUrl = URL.createObjectURL(workerBlob),
worker = new Worker(workerBlobUrl);
worker.onmessage = function(event) {
$('#output').text('Output is: ' + event.data);
};
worker.postMessage('foo');
function workerRunner() {
self.onmessage = function(event) {
self.postMessage('launched worker...');
var req = indexedDB.open('mydb', 1);
req.onupgradeneeded = function (e) {
self.postMessage('successfully upgraded db');
};
req.onsuccess = function (e) {
self.postMessage('successfully opened db');
};
req.onerror = function(e) {
self.postMessage('error');
};
}
};
@bemson
Copy link
Author

bemson commented Dec 17, 2015

Looks like I can't test workers via gist. IE complains about Blob-workers, and then attempts to use a relative gist url also fail. More investigation needed... Thankfully, the parent of this gist clearly works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment