Created
September 3, 2013 04:36
-
-
Save wookiehangover/6419759 to your computer and use it in GitHub Desktop.
Turn a function into an inline web worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var Blob = this.Blob; | |
var URL = this.URL; | |
var Worker = this.Worker; | |
this.createWorker = function(src){ | |
src = src.toString(); | |
var srcLines = src.split('\n'); | |
// Remove the outer function. There's probably a better way. | |
if( /^f.+\{/.test(srcLines[0]) ){ | |
srcLines.shift(); | |
} | |
if( /\}/.test(srcLines[srcLines.length - 1]) ){ | |
srcLines.pop(); | |
} | |
var blob = new Blob([ srcLines.join('\n') ], { type: "application/javascript" }); | |
var blobUrl = URL.createObjectURL(blob); | |
var worker = new Worker( blobUrl ); | |
return worker; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment