web

Creating Web Workers From Functions (No File Needed)

So, you want to create a web worker from a function, the problem is all the examples examples show creating a web worker using a separate JavaScript file or from embedded JavaScript source.  There is a way to do it and that is using Blob URLs.  In the example below, I have taken the simple dedicated worker example from mdn and modified it appropriately.

First, we assume that we have a function that we want to serve as the source of the web worker.

var workerScript = function () {
    console.log("starting worker");

    this.onmessage = function(e) {
        console.log('Message received from main script');
        var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
        console.log('Posting message back to main script');
        postMessage(workerResult);
    }
}

Next, we use that function’s source to create the web worker.

// Get the source of the function we want to use and make the source
// where it will be interpreted as an iife.
var workerScriptIifeSource = workerScript.toSource() + "()";

// Create a blob of the script source, specify it as JavaScript.
var workerScriptBlob = new Blob([workerScriptIifeSource],
    {type: 'application/javascript'});

// Create a worker by creating a url from the blob.
var myWorker = new Worker(window.URL.createObjectURL(workerScriptBlob));

See this example in action on jsfiddle here: http://jsfiddle.net/chaddotson/jr5p3L7r/

Notes:

  • The createObjectURL function is experimental according to MDN, so that is a bit of a warning bell.  However, according to caniuse, it is mostly safe across all browsers.
  • This can be used to create shared web workers, but they will not be shareable between tabs or windows.
  • This makes it more difficult, but possible to assemble dependencies for the web worker.
  • This isn’t the officially supported way to create a web worker.
  • I think this approach to creating a worker should be used sparingly, if ever.

 

Posted by Chad Dotson in Programming, Software Engineering, Tips, 0 comments

First Impressions of Dart

Today I finished my first Dart tutorial.  For those that don’t know what Dart is, Wikipedia has a nice synopsis that I’ve included here.

Dart is an open-source Webprogramming language developed by Google. It was unveiled at the GOTO conference in Aarhus, October 10–12, 2011.[4] The goal of Dart is “ultimately to replace JavaScript as the lingua franca of web development on the open web platform”,[5] but Dart currently relies exclusively on its cross-compilation to JavaScript feature in order to run in mainstream browsers. Dart is intended to address issues with JavaScript that Google engineers felt could not be solved by evolving the language, while offering better performance.[5] Google works on Dart to help it build more complex, full-featured client-side Web applications.[6]

Dart is a class-based, single inheritance, object-oriented language with C-style syntax. It supports interfaces, abstract classes, reifiedgenerics, and optional typing. Static type annotations do not affect the runtime semantics of the code. Instead, the type annotations can provide documentation for tools like static checkers and dynamic run time checks.

–Wikipedia

While the tutorial was simple and designd to be completed in about an hour, I found it a worthwhile and informative endeavor.  It was just enough to give an introduction to the language and syntax.  As the above snippet from Wikipedia states, Dart cross-compiles into JavaScript and should be familiar to people who know another C-style language.

I have a high degree of experience with object-oriented design, C++, and JavaScript and Dart has left me with a good first impression.  I believe an important aspect of Dart is that it could be used to bridge the gap between C-style languages and JavaScript for programmers who do not have a lot of JavaScript experience.  I do not believe that Dart will ultimately replace JavaScript, as Google hopes.  JavaScript, while it has shortcomings, is ultimately very powerful and flexible.  Dart’s ultimate utility will be something akin to CoffeeScript that is making it easier and less tedious to write JavaScript.  In the coming weeks I may delve further into Dart, though up next I will probably do a tutorial or two on Rust.

Links: Dart 1-hour tutorial

Posted by Chad Dotson in Programming, 1 comment

Interesting Twitter Feeds

Here are some Twitter Feeds of note in case any Twitter users reading this haven’t found them yet.Google Reader Team: http://twitter.com/googlereader

UStream: http://twitter.com/UstreamTV

Alaska Mt Redoubt Feed: http://twitter.com/alaska_avo

Wil Wheaton: http://twitter.com/wilw

Brent Spiner: http://twitter.com/BrentSpiner

Cobra Commander: http://twitter.com/CobraCommander

Mars Rovers: http://twitter.com/MarsRovers

LeVar Burton: http://twitter.com/levarburton

Patrick Norton: http://twitter.com/patricknorton

Leo Laporte: http://twitter.com/LeoLaporte

Alex Albrecht: http://twitter.com/alexalbrecht

Kevin Rose: http://twitter.com/kevinrose

Posted by Chad Dotson in Ramblings, 0 comments