Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created October 14, 2010 19:04
Show Gist options
  • Save rwaldron/626815 to your computer and use it in GitHub Desktop.
Save rwaldron/626815 to your computer and use it in GitHub Desktop.
// Declare an object literal as a namespace
var Utils = {};
// Define an sort of ES5 forEach (this is demo only, so it's fast and loose)
Utils.forEach = function(arr, callback) {
var iter = 0,
len = arr.length;
for ( ; iter < len; iter++ ) {
if ( arr[iter] ) {
callback.call(arr[iter], arr[iter], iter, arr);
}
}
};
<!doctype html>
<html>
<head>
<title>Workers via Blob</title>
<script src="worker-via-blob.js"></script>
</head>
<body>
<input type="button" id="button" value="message" />
<pre id="worker-blob">
// Throws: DOM Exception 12
// importScripts('worker-via-blob-import.js');
// would've been imported...
// Declare an object literal as a namespace
var Utils = {};
// Define an sort of ES5 forEach (this is demo only, so it's fast and loose)
Utils.forEach = function(arr, callback) {
var iter = 0,
len = arr.length;
for ( ; iter < len; iter++ ) {
if ( arr[iter] ) {
callback.call(arr[iter], arr[iter], iter, arr);
}
}
};
// fake data burn from YQL
var dataBurnObject = {
"item": [
{
"title": "Chile's textbook mine rescue brings global respect \n (AP)\n"
},
{
"title": "Banks seize 288K homes in Q3, but challenges await \n (AP)\n"
},
{
"title": "Afghan peace council chief: Taliban ready to talk \n (AP)\n"
},
{
"title": "Group says Air Force has stopped enforcing gay ban \n (AP)\n"
},
{
"title": "Applications for jobless benefits rise to 462K \n (AP)\n"
},
{
"title": "Report: AOL, buyout firms mulling bid for Yahoo \n (AP)\n"
},
{
"title": "Verizon to join AT&T in selling Apple's iPad \n (AP)\n"
},
{
"title": "Ready, set, snore! Spain holds siesta contest \n (AP)\n"
},
{
"title": "'VH1 Divas' announces lineup, tribute to military \n (AP)\n"
},
{
"title": "Halftime report is in on college football season \n (AP)\n"
},
{
"title": "Rescued Chile miners recover, face celebrity status \n (Reuters)\n"
},
{
"title": "U.S. seeks 19 to 24 years for elderly \"mini-Madoff\" \n (Reuters)\n"
},
{
"title": "Yahoo shares surge on prospect of buyout \n (Reuters)\n"
},
{
"title": "Afghan Peace Council flexible on Taliban talks terms \n (Reuters)\n"
},
{
"title": "Accused airline bomber waives right to fast U.S. trial \n (Reuters)\n"
},
{
"title": "U.S. growth outlook downgraded ahead of QE2: Reuters poll \n (Reuters)\n"
},
{
"title": "September home foreclosures top 100,000 for first time \n (Reuters)\n"
},
{
"title": "Jobless claims point to further Fed easing \n (Reuters)\n"
},
{
"title": "Chile basks in mine rescue glory \n (AFP)\n"
},
{
"title": "In Lebanon, Ahmadinejad foresees Israel's demise \n (AFP)\n"
}
]
};
self.addEventListener('message', function (event) {
var global = this;
global.postMessage({
renderer: event.data,
message: event.data.message + ' ' + "pong"
});
Utils.forEach(dataBurnObject.item, function (item, iter) {
global.postMessage(item);
})
}, false);
</pre>
</body>
</html>
document.addEventListener('DOMContentLoaded', function () {
var workerViaBlob = {
blob: '',
url: '',
worker: '',
init: function ( src, callback ) {
this.blob = new BlobBuilder();
this.blob.append(
document.getElementById(src).innerText
);
this.url = createBlobURL( this.blob.getBlob() );
this.worker = new Worker( this.url );
callback && callback.call(this, this.worker, this.url);
return this;
}
};
workerViaBlob.init('worker-blob', function (worker, url) {
worker.addEventListener('message', function () {
if ( "message" in event.data ) {
console.log(event.data, event.data.message);
return;
}
console.log(event.data);
}, false);
document.getElementById('button').addEventListener('click', function () {
worker.postMessage({
message: "ping"
});
}, false);
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment