Skip to content

scoin/frontrunner.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FrontRunner.js

FrontRunner is a lightweight (only 874 bytes minified) wrapper on HTML5 Web Workers that implements a switching system and a number of other features.

Using FrontRunner lets you send and receive specific messages between a Worker and your script using familiar syntax.

FrontRunner also lets you spawn a worker by simply passing it a function, allowing you to write multi-threaded, client-side Javascript all in the same script.

##Usage

Include FrontRunner.js in your main HTML file, like you would any other client-side Javascript library.

        <script src="/FrontRunner.js"></script>

####Basic Usage

myscript.js

        //create a new worker pointing at your worker file
        var fr = FrontRunner("myworker.js"); 

        //send a "foo" message to the worker
        fr.send("foo", {"bar": "baz"}); 

        //listen for "qux" messages from worker
        fr.on("qux", function(data){
            doSomething(data);
        })

myworker.js

        //you must import FrontRunner to use it in a seperate worker file
        importScripts('FrontRunner.js'); 

        //create a Worker instance of FrontRunner
        var worker = FrontRunner();

        //listen for "foo" messages from script
        worker.on("foo", function(data){
            doSomething(data);
            //send a "qux" message to your script
            worker.send("qux", data);
        })

####Passing a Function

You can pass a function to FrontRunner instead of a script location. The contents of that function will be used to create the Worker, allowing you to have your code for both threads in the same script.

It is important to note that the function you pass will be entirely in it's own scope.

You do not need to import or instantiate FrontRunner in your worker function. It is made available for you as an argument.

        //create a new worker with a function

        var fr = FrontRunner(function(worker){

            worker.on("foo", function(data){
                doSomething(data);
                worker.send("bar", data);
            })

        })

        fr.send("foo", something);

        fr.on("bar", function(data){
            doSomething(data);
        })

####All Methods

######FrontRunner.send(event, data)

event : string
data: can be any datatype, including blobs

Sends a message between the Worker and the Script,containing data for listeners listening for that event.

######FrontRunner.on(event, callback)

event : string
callback: function(data)

Listens for specific message events sent between the Worker and the Script.

######FrontRunner.remove(event)

event : string

Stop listening for that event.

######FrontRunner.terminate()

Kill the worker thread. Can be called from the Script or the Worker.

##TODO

Support SharedWorkers and ServiceWorkers

Test Suite Makefile

More Tests

##Test The test suite is a simple express app running mocha and selenium.

To run the tests:

    cd test
    npm install
    java -jar selenium-server-standalone-2.46.0.jar
    npm start
    npm test

About

HTML5 Web Workers with custom events and messages.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published