Git Product home page Git Product logo

blob-worker's Introduction

blobWorker v0.1

Making Web Workers work inline with Blob.

I was working on a personal site, with some pretty wacky JavaScript. While it runs perfectly well on Google Chrome, it suffers terribly on Mozilla Firefox. I looked around for ways to optimize, or speed up the JavaScript process, and I bumped into Web Workers.

While it works perfectly well, I find the need to have a separate JavaScript file for the Web Worker to work with, somewhat a hassle. Digging further, I found this interesting post on StackOverflow, which uses Blobs.

Needless to say, I started to work on making a script, and this is the result.


How To Use

Methods

.check() returns true if the browser supports Worker, URL and either Blob or BlobBuilder.

.prepare() takes in an object to initialize the blobWorker.

foo.prepare({
    fn:         /* function for the Web Worker to execute */,
    arg:        /* the argument to send (postMessage) to Web Worker */,
    success:    /* callback function when the Web Worker is done */,
    error:      /* OPTIONAL callback function when Web Worker encounters errors */,
    addJs:      /* OPTIONAL list of scripts to add into Web Worker (importScripts) */
});

.exec() calls the Web Worker to start working. (Essentially calling Worker.postMessage()) If an argument is supplied, the argument will be passed to the Web Worker, instead of the already-initialized argument in .prepare().

.kill() terminates the Web Worker and destroys the Blob containing the JavaScript. (Essentially calling Worker.terminate() and URL.revokeURLObject(blob))


Prepare

fn takes in a function, which is executed by the Web Worker.

arg takes in an object, which will be passed to the Web Worker to be executed.

success takes in a function, which is executed when the Web Worker posts back a message to the script.

error is an optional argument, which takes in a function that is executed when the Web Worker encounters errors.

addJs is an optional argument, which takes in an array of String that is added into the Web Worker via importScripts().

Please note that the functions and arguments are simply wrapped and passed to the Web Worker. You need to code the functions and arguments like how you would with a Web Worker. Please refer to the Example.

Example

var foo = new blobWorker();

if (foo.check()) {
    foo.prepare({
        fn: function (e) {
            postMessage('Worker here, I got (' + e.data + ') !');
        },
        arg: 'the argument to pass to Web Worker',
        success: function (e) {
            alert(e.data);
        }
    });
    
    foo.exec();
    
    foo.exec( 'NOPE' );
    
    foo.kill();
}
else {
    // a gracious fallback?
}

Browser Support

Ah yes. The most dreaded question. All data is based on CanIUse.com. Did not really made a thorough test myself.

  • No IE Support (Boo hoo ;_;)
  • Firefox 6.0
  • Chrome 8.0
  • Safari 6.0
  • Opera 12.1

Credits

License

MIT

blob-worker's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

kirahua

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.